awk - Print whole string ending with a Tab if key matched

Hi ,

I am looking to print the whole string from file2.txt but it is only printing 77 but not the whole matched string from File2.txt Any help is appreciated.
Thanks,

Script

awk '
  BEGIN {
    OFS="\t"
    out = "a.txt"}
NR==FNR && NF {a[substr($0,1,8)]=$0; next}
function print_65_11() {
    if (key in a) 
      print "65", line[key] > out
}
 $1 == "01" {
    if (FNR > 1) print_66_11()
    key = $4 $3 $2
 lines = ""  
}

  { print > out
    lines = lines $0 "\n"  }  END {print_66_11()}
' b.txt c.txt

b.txt

01  89  68  5000
02  83  11
04  83  9   02
03  83  00
06  83  00
07  83  11  RT0429
01  44  73  8800
02  44  73
04  44  73   02
03  44  73
06  44  73
07  44  11  RT  0789

c.txt

50006889RT0429 NARD /3010  /E    

51002387 NARD /3000  /E 

b.txt (Current Output)

01  89  68  5000
02  83  11
04  83  9   02
03  83  00
06  83  00
07  83  11  RT0429
65	
01  44  73  8800
02  44  73
04  44  73   02
03  44  73
06  44  73
07  44  11  RT  0789

Desired Output

01  89  68  5000
02  83  11
04  83  9   02
03  83  00
06  83  00
07  83  11  RT0429
65	50006889RT0429 NARD /3010  /E 
01  44  73  8800
02  44  73
04  44  73   02
03  44  73
06  44  73
07  44  11  RT  0789

What in http://www.unix.com/302933028-post3.html doesn't provide your desired solution?

Hi RudiC,
What it is missing, is that since File2.txt in this thread and B.txt in the other thread contain one or more blank lines, the output files include unwanted copies of those lines.

To get rid of that problem, the line in your script in the other thread:

awk     'FNR==NR && NF  {T[$1]=$0

needs to change to:

awk     'FNR==NR        {if(NF) T[$1]=$0

The same problem also appears in the High-T's code in this thread.

I'm ashamed to say how long it took me to find where the extra output was coming from. It is past my bedtime... Good night (or morning as the case may be).

1 Like

Rats - you're absolutely right! That one single empty line went past me as well ...

Still the requestor complains about the file2 record not being printed together with the "77" . I can see why now: The script populates array a with the lines from file2, but in the function it prints array line which is empty...

Yes, I noticed that too, but you had already fixed that in the other thread (which provides a superset of the functionality required for this thread). So I didn't see any need to post a complete script for this thread.

Thanks Don and Rodic for sharing your great knowledge and advise.
Actually I have to use this script because this part has been extracted from a major script and logics are being used in other parts of script from this part.
Is it possible if we can adjust the same script and declare FNR==NR first and print the string after line 07? In case the key is matched?
Thanks for your help.

In fact FNR==NR is not a declaration but a test pattern used to identify all lines in the first file in the input stream.
And, the searched for record is printed after 07 . In your first thread, there were multiple 07 records, and you seemed to want your output after the last one. The reliable indicator therefor is $1=="01" .

A normalized version of the original script presented in post #1 in this thread with the corrections needed is:

awk '
BEGIN {	OFS="\t"
	out = "File3.txt"
}
NR == FNR {
	if(NF)
		a[substr($0,1,8)]=$0
	next
}
function print_77_99() {
	if(key in a)
		print "77", a[key] > out
}
$1 == "01" {
	if(FNR > 1)
		print_77_99()
	key = $4 $3 $2
}
{	print > out
}
END {	print_77_99()
}' File2.txt File1.txt

The primary changes are marked in red. All references to line have been removed since they were not used.

The only substantive difference between this code and the code RudiC posted in the other thread is that RudiC's script has an else clause on the if in print_77_99() because the specification in this thread doesn't produce any output if there is no matching key in File2.txt .

With the sample input files shown in the 1st post in this thread, the above script writes the following text into File3.txt :

01  89  68  5000
02  83  11
04  83  9   02
03  83  00
06  83  00
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
07  44  11  RT  0789

If someone wants to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk .

1 Like

@RudiC I tried modifying the script but it is still not working. Is it possible if you can modify my script? I want to know what exactly you meant :slight_smile:
Thanks,

What - beyond what Don Cragun suggested - do you want modified? If I remember correctly, the "extended" record WAS printed after the "07" record.

Have you looked at message #8 in this thread?

I started with your code, and I thought I made it do exactly what you said you wanted it to do. What is the code I suggested in that post doing wrong?

Thanks a lot Don.
Code in Reply #8 worked for me...
I wasnt testing it properly.
Bundle of thanks.