awk - Print lines if only matching key is found


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers awk - Print lines if only matching key is found
# 8  
Old 02-03-2015
So have you dropped the requirement to update File1.txt? Or, are you just waiting to add that request again after you get code that gives you what you now want in File3.txt?

Moderator's Comments:
Mod Comment In the future, instead of going back and editing your earlier posts with updated requirements, add new posts that clarify your earlier requests. (This allows people who will read these threads in the future to understand the questions asked in posts that now make no sense since you have modified history by changing your posts.)
# 9  
Old 02-04-2015
Hi RudiC and Don,

Following script worked for me.
Thanks for the guidance.

#!/bin/ksh
> File3.txt
> Unmatched.txt
awk '
BEGIN {
OFS="\t"
out_m = "File3.txt"
out_nm = "Unmatched.txt"
}
NR == FNR {
if(NF) {
a[$1]=$0
}
next
}
function print_77_99() {
out = (mc ? out_m : out_nm)
for(i = 1; i <= lc; i++)
print l[i] > out
for(i = 1; i <= mc; i++)
print m[i] > out
lc = mc = 0
}
$1 == "01" {
if(FNR > 1)
print_77_99()
key = $4 $3 $2
}
$1 == "07" && (key $NF) in a {
m[++mc] = "77\t" a[key $NF]
}
{ l[++lc] = $0
}
END { print_77_99()
}' File2.txt File1.txt

Last edited by High-T; 02-11-2015 at 12:42 AM..
# 10  
Old 02-04-2015
I'm happy to see that you're making progress, but I'm surprised that you think the code shown in post #18 does what you specified earlier. When I run it with the sample File1.txt and File2.txt you provided in your latest revision of post #1, File3.txt is left unchanged and three empty lines are added to the end of File4.txt.

The following script seems to do what you said you wanted in one of your earlier posts:
Code:
#!/bin/ksh
> File3.txt
> Unmatched.txt
awk '
BEGIN {
	OFS="\t"
	out_m = "File3.txt"
	out_nm = "Unmatched.txt"
}
NR == FNR {
	if(NF) {
		a[$1]=$0
	}
	next
}
function print_77_99() {
	out = (mc ? out_m : out_nm)
	for(i = 1; i <= lc; i++)
		print l[i] > out
	for(i = 1; i <= mc; i++)
		print m[i] > out
	lc = mc = 0
}
$1 == "01" {
	if(FNR > 1)
		print_77_99()
	key = $4 $3 $2
}
$1 == "07" && (key $NF) in a {
	m[++mc] = "77\t" a[key $NF]
}
{	l[++lc] = $0
}
END {	print_77_99()
}' File2.txt File1.txt

and, with the data you provided in your latest revision of post #1 in this thread, sets the contents of File3.txt to:
Code:
 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        ///

and the contents of Unmatched.txt to:
Code:
    01  44  73  8800
    02  44  73
    04  44  73   02
    03  44  73
    06  44  73
    07  44  11  RT  0789

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print matching lines in files that meet critera

In the tab delimited files below I am trying to match $2 in file1 to $2 of file2. If a match is found the awk checks $3 of file2 and if it is greater than 40% and $4 of file2 is greater than 49, the line in file1 is printed. In the desired output line3 of file1 is not printed because $3 off file2... (9 Replies)
Discussion started by: cmccabe
9 Replies

2. Shell Programming and Scripting

Find key pattern and print selected lines for each record

Hi, I need help on a complicated file that I am working on. I wanted to extract important info from a very huge file. It is space delimited file. I have hundred thousands of records in this file. An example content of the inputfile as below:- ## ID Ser402 Old; 23... (2 Replies)
Discussion started by: redse171
2 Replies

3. UNIX for Dummies Questions & Answers

awk - (URGENT!) Print lines sort and move lines if match found

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 ... (1 Reply)
Discussion started by: High-T
1 Replies

4. Shell Programming and Scripting

How to print few lines before and after matching word is found suing grep?

Hi, here are few lines present in the logs. I want to grep on Error and print few lines before and after Error word is found line1 Line2 Line3 Error Line4 Line5 Line6 Line7 I want the output to be Line2 Line3 Error Line5 (1 Reply)
Discussion started by: arghadeep adity
1 Replies

5. Shell Programming and Scripting

Compare file1 for matching line in file2 and print the difference in matching lines

Hello, I have two files file 1 and file 2 each having result of a query on certain database tables and need to compare for Col1 in file1 with Col3 in file2, compare Col2 with Col4 and output the value of Col1 from File1 which is a) not present in Col3 of File2 b) value of Col2 is different from... (2 Replies)
Discussion started by: RasB15
2 Replies

6. Shell Programming and Scripting

awk to print all lines after a pattern is found

Is there a way with aw to print all lines after a string is found There is a file like this ....... ........ 2012/19/11 :11.58 PM some data lne no date 2012/19/11 :11.59 PM some other data 2012/20/11 :12.00 AM some other data some line without dates some more lines without dates... (8 Replies)
Discussion started by: swayam123
8 Replies

7. Shell Programming and Scripting

awk print non matching lines based on column

My item was not answered on previous thread as code given did not work I wanted to print records from file2 where comparing column 1 and 16 for both files find rows where column 16 in file 1 does not match column 16 in file 2 Here was CODE give to issue ~/unix.com$ cat f1... (0 Replies)
Discussion started by: sigh2010
0 Replies

8. Shell Programming and Scripting

print lines from a file containing key word

i have a file containing over 1 million records,and i want to print about 300,000 line containing a some specific words. file has content. eg 1,rrt,234 3,fgt,678 4,crf,456 5,cde,drt 6,cfg,123 and i want to print the line with the word fgt,crf this is just an example,my file is so... (2 Replies)
Discussion started by: tomjones
2 Replies

9. Shell Programming and Scripting

Print lines matching value(s) in other file using awk

Hi, I have two comma separated files. I would like to see field 1 value of File1 exact match in field 2 of File2. If the value matches, then it should print matched lines from File2. I have achieved the results using cut, paste and egrep -f but I would like to use awk as it is efficient way and... (7 Replies)
Discussion started by: SBC
7 Replies

10. Shell Programming and Scripting

AIX equivalent to GNU grep's -B and -A [print lines after or before matching lines]

Hi folks I am not allowed to install GNU grep on AIX. Here my code excerpt: grep_fatal () { /usr/sfw/bin/gegrep -B4 -A2 "FATAL|QUEUE|SIGHUP" } Howto the same on AIX based machine? from manual GNU grep ‘--after-context=num’ Print num lines of trailing context after... (4 Replies)
Discussion started by: slashdotweenie
4 Replies
Login or Register to Ask a Question