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
# 8  
Old 01-29-2015
A normalized version of the original script presented in post #1 in this thread with the corrections needed is:
Code:
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:
Code:
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.
This User Gave Thanks to Don Cragun For This Post:
# 9  
Old 01-29-2015
@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 Smilie
Thanks,
# 10  
Old 01-29-2015
What - beyond what Don Cragun suggested - do you want modified? If I remember correctly, the "extended" record WAS printed after the "07" record.
# 11  
Old 01-29-2015
Quote:
Originally Posted by High-T
@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 Smilie
Thanks,
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?
# 12  
Old 01-31-2015
Awk - Rename output file when it is processed same as input file

Thanks a lot Don.
Code in Reply #8 worked for me...
I wasnt testing it properly.
Bundle of thanks.
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