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
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).
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 :
@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
Thanks,