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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - Print whole string ending with a Tab if key matched
# 1  
Old 01-29-2015
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

Code:
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
Code:
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

Code:
50006889RT0429 NARD /3010  /E    

51002387 NARD /3000  /E

b.txt (Current Output)
Code:
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

Code:
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


Last edited by High-T; 02-11-2015 at 12:39 AM..
# 2  
Old 01-29-2015
What in https://www.unix.com/302933028-post3.html doesn't provide your desired solution?
# 3  
Old 01-29-2015
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:
Code:
awk     'FNR==NR && NF  {T[$1]=$0

needs to change to:
Code:
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).
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 01-29-2015
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...
# 5  
Old 01-29-2015
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.
# 6  
Old 01-29-2015
Need to use this script as this is only a part of a big script.

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.
# 7  
Old 01-29-2015
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".
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Match Fields between two files, print portions of each file together when matched in ([g]awk)'

I've written an awk script to compare two fields in two different files and then print portions of each file on the same line when matched. It works reasonably well, but every now and again, I notice some errors and cannot seem to figure out what the issue may be and am turning to you for help. ... (2 Replies)
Discussion started by: jvoot
2 Replies

2. UNIX for Beginners Questions & Answers

Match Strings between two files, print portions of each file together when matched ([g]awk)

I have two files and desire to use the strings from $1 of file 1 (file1.txt) as search criteria to find matches in $2 of file 2 (file2.txt). If matches are found I want to output the entire line of file 2 (file2.txt) followed by fields $2-$11 of file 1 (file1.txt). I can find the matches, I cannot... (7 Replies)
Discussion started by: jvoot
7 Replies

3. Shell Programming and Scripting

How do i replace a word ending with "key" using awk excpet for one word?

echo {mbr_key,grp_key,dep_key,abc,xyz,aaa,ccc} | awk 'gsub(/^|abc,|$/,"") {print}' Required output {grp_key,xyz,aaa,ccc} (5 Replies)
Discussion started by: 100bees
5 Replies

4. Shell Programming and Scripting

awk - how to print specific field if a string is matched

hi gurus, I would like to be able to use awk to process 1 file as such: abc 1 2 3 4 5 6 7 8 9 10 flags 1 2 4 flags 1 2 5 abc 2 3 4 5 6 7 8 9 10 11 flags 1 2 3 abc 4 5 6 7 8 9 6 7 78 89 flags 1 2 3 flags 1 2 4 flags 1 2 3 4 I would like to be able to print field 1 and 5 when the... (4 Replies)
Discussion started by: revaroo
4 Replies

5. Shell Programming and Scripting

print the whole row in awk based on matched pattern

Hi, I need some help on how to print the whole data for unmatched pattern. i have 2 different files that need to be checked and print out the unmatched patterns into a new file. My sample data as follows:- File1.txt Id Num Activity Class Type 309 1.1 ... (5 Replies)
Discussion started by: redse171
5 Replies

6. Shell Programming and Scripting

Print only matched string instead of entire line

Hi, I have a file whose lines are something like Tchampionspsq^@~^@^^^A^@^@^@^A^A^Aÿð^@^@^@^@^@^@^@^@^@^@^A^@^@^@^@^?ð^@^@^@^@^@^@^@?ð^@^@^@^@^@^@pppsq^@~^@#@^@^@^@^@^@^Hw^H^@^@^@^K^@^@^@^@xp^At^@^FTtime2psq^@ ~^@^^^A^@^@^@^B^A I need to extract all words matching T*psq from the file. Thing is... (4 Replies)
Discussion started by: shekhar2010us
4 Replies

7. Shell Programming and Scripting

AWK Print Line If Specific Character Is Matched

Hello, I have a file as such: FFFFFFF6C000000 225280 225240 - - rwxs- FFFFFFFF79C00000 3240 3240 - - rwxs- FFFFFFFF7A000000 4096 4096 - - rwxs- FFFFFFFF7A400000 64 64 ... (3 Replies)
Discussion started by: PointyWombat
3 Replies

8. Shell Programming and Scripting

Print a key with its all values using awk/others

input COL1 a1 b1 c1 d1 e1 f1 C1 10 10 10 100 100 1000 C2 20 20 200 200 200 2000 output C1 a1 10 1 C1 b1 10 1 C1 c1 10 1 C1 d1 100 2 C1 e1 100 2 C1 f1 1000 3 C2 ... (12 Replies)
Discussion started by: ruby_sgp
12 Replies

9. Shell Programming and Scripting

print only matched string instead lines in grep

frnd, Is there any way to print only the string that matched the pattern instead printing the whole line? thanks in advance. (3 Replies)
Discussion started by: clx
3 Replies

10. Shell Programming and Scripting

sed, grep, awk, regex -- extracting a matched substring from a file/string

Ok, I'm stumped and can't seem to find relevant info. (I'm not even sure, I might have asked something similar before.): I'm trying to use shell scripting/UNIX commands to extract URLs from a fairly large web page, with a view to ultimately wrapping this in PHP with exec() and including the... (2 Replies)
Discussion started by: ropers
2 Replies
Login or Register to Ask a Question