Awk - Match and print out in order - Remove lines if match found


Recommended Posts

URGENT HELP IS NEEDED!!

I am looking to move matching lines (01 - 07) from File1 and 77 tab the matching string from File2, to File3.txt. I am almost done but

- Currently, script is not printing lines to File3.txt in order.

- Also the matching lines are not moving out of File1.txt

Thanks a lot.

Any help is appreciated.

Script I am using:

awk '

BEGIN {

OFS="\t"

out = "File3.txt" }

NR == FNR {

if(NF)

a[substr($0,1,14)]=$0;next

next

}

function print_77_99() {

if(key in a)

print "77", a[key] > out

}

$1 == "01" {

if (FNR > 1) print_77_99()

lines = ""

}

$1=="01" &&

FNR>1 {keyfound?"77\t" keyfound:"99"}

$1=="01" {PK=$4 $3 $2

keyfound=""

}

$1=="07" {key=PK $4}

key in a {keyfound=a[key]}

{ if(key in a)

print $0 > out

lines = lines $0 "\n"

}

END { print_77_99()

}' File2.txt File1.txt

File1.txt

01 89 68 5000

02 83 11

04 83 9 02

03 83 00

06 83 00

07 83 11 RT0429

07 83 88 FS0547

01 44 73 8800

02 44 73

04 44 73 02

03 44 73

06 44 73

07 44 11 RT 0789

File2.txt

50006889RT0429 NARD /3010 /E /C A87545457 / // ///11 ///

51002387 NARD /3000 /E /S N054896334IV / // ///11 ///

Current Output (File3.txt)

07 83 11 RT0429

77 50006889RT0429 NARD /3010 /E /C A87545457 / // ///11 ///

01 44 73 8800

02 44 73

04 44 73 02

03 44 73

06 44 73

Desired Output (File3.txt)

I need complete set from line 01 to 07 including any addition 07 lines in case the key is matched from File1, followed by 77 tab and matched string from File2.

01 89 68 5000

02 83 11

04 83 9 02

03 83 00

06 83 00

07 83 11 RT0429

07 83 88 FS0547

77 50006889RT0429 NARD /3010 /E /C A87545457 / // ///11 ///

Desired Format(File1.txt)

I need the matched records out of the File1.txt (remove) so the following is not matched record that would stay in File1.txt till a match is found.

01 44 73 8800

02 44 73

04 44 73 02

03 44 73

06 44 73

07 44 11 RT 0789

Link to comment
Share on other sites

This topic is now closed to further replies.