match 2 files using nawk command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting match 2 files using nawk command
# 1  
Old 12-01-2010
match 2 files using nawk command

i am matching two files.
the files are in the format

file_1.txt
Code:
1|_|X|_|
2|_|W|_|
3|_|Y|_|
4|_|Z|_|
5|_|U|_|


file_2.txt
Code:
W|_|A|_|
Z|_|C|_|
V|_|B|_|
X|_|D|_|sdff|_|
Y|_|


file_3.txt should be in the format
Code:
2|_|W|_|B|_|
4|_|Z|_|C|_|

all the parameters are seprated by |_|
(parameter1|_|parameter2|_|)
the matching should be done only for those records which have 2 parameters in file_1.txt and file_3.txt


the command below command will compare the two files based on the matching parameter
Code:
nawk -F "|" 'NR==FNR{a[$2]=$0;next}a[$1]{print a[$1]$2"|_|"}' file_1.txt file_2.txt > file_3.txt


I need to check that only 2 parameters are there in file_1.txt and file_2.txt before comparison.

need suggestions????

Last edited by Franklin52; 12-01-2010 at 08:55 AM.. Reason: Please use code tags
# 2  
Old 12-01-2010
Code:
awk -F "|" 'NR==FNR{if(NF==5)a[$1]=$0;next}a[$3] && NF==5{print $1 FS $2 FS a[$3]}' file2 file1

# 3  
Old 12-01-2010
Code:
$ awk -F "|" 'NR==FNR{a[$3]=$0;next} $1 in a && NF==5{ print a[$1],"_",$3}' OFS="|" file1.txt file2.txt
2|_|W|_||_|A
4|_|Z|_||_|C


Last edited by Franklin52; 12-01-2010 at 09:31 AM.. Reason: Please use code tags, thank you
This User Gave Thanks to dhiren1 For This Post:
# 4  
Old 12-01-2010
Thanks Dhiren

Suppose a particular record in the file_2.txt or file_1.txt is not in the format
Code:
2|_|X|_|

then how to handle such cases???

Suppose now the file_2.txt records are in the format
Code:
W|_|A|_|
X|_B|_|
Y|_|C|_|
1234567890435 (it can be any thing )
Z|_|D|_|

can we remove such records from file_1.txt or file_2.txt and create new file_2(new).txt if needed??? or can we handle them in one nawk statement????

How to remove such corrupt records??????

Last edited by Scott; 12-01-2010 at 10:07 AM.. Reason: Please use code tags
# 5  
Old 12-01-2010
Quote:
Originally Posted by dhiren1
Code:
2|_|W|_||_|A
4|_|Z|_||_|C

No go @dhiren, the OP need
Quote:
Originally Posted by centurion_13
file_3.txt should be in the format
Code:
2|_|W|_|B|_|
4|_|Z|_|C|_|

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Data match 2 files based on first 2 columns matching only and join if match

Hi, i have 2 files , the data i need to match is in masterfile and i need to pull out column 3 from master if column 1 and 2 match and output entire row to new file I have tried with join and awk and i keep getting blank outputs or same file is there an easier way than what i am... (4 Replies)
Discussion started by: axis88
4 Replies

2. UNIX for Beginners Questions & Answers

UNIX Command to Match columns from two csv files

I am joining two CSV files based on 'Server_Name' column, 1st column of first file and 2nd column of second file. If matches, output 1st and 2nd column from first file, 3rd,4th,5th,6th columns from second file. I am expecting output CSV file as below. Could you please send me help me with... (6 Replies)
Discussion started by: Anadmbt
6 Replies

3. Shell Programming and Scripting

awk to match field between two files and use conditions on match

I am trying to look for $2 of file1 (skipping the header) in $2 of file2 (skipping the header) and if they match and the value in $10 is > 30 and $11 is > 49, then print the line from file1 to a output file. If no match is foung the line is not printed. Both the input and output are tab-delimited.... (3 Replies)
Discussion started by: cmccabe
3 Replies

4. Shell Programming and Scripting

Nawk match regex of bash variable

Using a bash for loop to pass variables into a nawk loop to capture a string in an sftp log. Tried several different syntax methods to have the variable treated as a regex so the loop will capture the string. for i in `cat /tmp/dar3.out.2` do nawk -vst=$i '$5 ~ /$st/ && /closed/ && /user/... (3 Replies)
Discussion started by: smenago
3 Replies

5. Shell Programming and Scripting

Help with nawk (Exact Match)

I have a file with the contents below "lets say the name of the file is abcxyz" shown at the end of this. I am using nawk to find the exact ip address and the 6 lines after the match is found using the following nawk statement /usr/bin/nawk "/111.46.14.107/,printed==6 { ++printed; print; }"... (7 Replies)
Discussion started by: knijjar
7 Replies

6. Shell Programming and Scripting

Linux Command Error for nawk command

Hi All We are migrating our projects from unix environment to linux. I tried running a install script which sets up my project, i.e. the directory structure and all. But in the middle of the script i started receiveing following error : nawk: command not found . So i need to know which... (1 Reply)
Discussion started by: vee_789
1 Replies

7. Shell Programming and Scripting

Use match() in nawk to find digits in number

Hi, I just need to check whether number of digits in a phone number is 10 or not. If I am not wrong regex will be: {9} I have to use this inside nawk as this is a small portion of a big program. nawk ' BEGIN { RS="";FS=";"; regex="{9}"; } { for (i=1;i<=NF;i++) { if... (6 Replies)
Discussion started by: shekhar2010us
6 Replies

8. Shell Programming and Scripting

Help to use NAWK command

Hi, I am using NAWK command to scan a file (test.txt) and to create new files based on the length of lines in the file test.txt. My requirement is I need to create test_good with records having line lenght less than 4 and to create test_bad with records having line lenght more than 4. I... (4 Replies)
Discussion started by: sbhuvana20
4 Replies

9. Shell Programming and Scripting

NAWK - looping with match() function

i'm trying to use the "before" output from the match() function as part of the results of each Regex match... but... My input data: (from an input file) i only show the first record in my file.. all other records are similar. mds_ar/bin/uedw92wp.ksh:cat $AI_SQL/wkly_inqry.sql... (2 Replies)
Discussion started by: danmauer
2 Replies

10. UNIX for Dummies Questions & Answers

if within nawk command

I am trying to nawk through a file and if the 24.25.26 charachters in the file are "000" then print a line to a new file in one format, otherwise print a line in a different format. So far I have the following: nawk '/^1/ { if(substr($0,24,3) != 000)... (1 Reply)
Discussion started by: kshelluser
1 Replies
Login or Register to Ask a Question