comparing lines from 2 files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting comparing lines from 2 files
# 8  
Old 05-21-2007
Bug comparing lines from 2 files

Dear anbu23,

suppose i have a file FILE A

xxx|yyy|zzz|rrr
rrr|ttt|yyy|uuu

another file i have FILE B

bbb|ppp|eee
hhh|jjj|kkk

like this.
i just want to compare 1st field of FILE A with 1st field of FILE B...
like that
if the all fields are matches it wiil print a success msg else it will throw an error...

Thanks
Regards,
# 9  
Old 05-21-2007
Quote:
Originally Posted by panknil
Dear anbu23,

suppose i have a file FILE A

xxx|yyy|zzz|rrr
rrr|ttt|yyy|uuu

another file i have FILE B

bbb|ppp|eee
hhh|jjj|kkk

like this.
i just want to compare 1st field of FILE A with 1st field of FILE B...
like that
if the all fields are matches it wiil print a success msg else it will throw an error...

Thanks
Regards,
Code:
awk -F"|" ' NR == FNR { arr[$1]=1; next } !arr[$1] { print "Errorr" ;exit } END { print "Success" } ' filea fileb

# 10  
Old 05-21-2007
comparing lines from 2 files

Thank you very much anbu23 for your reply but my requirement is something different.
its like

FILE A

223|xls file pattern|ABCD|UUU
245|yyy file mmm|TRHJ|ZZZ

FILE B

disa|yyy|555223
asdf|uuu|627245

now look the 1st field of FILE A is matching with the last 3 number of 3rd field of FILE B...
this is the thing i want to do...

223 == 555223
245 == 627245
i want to match like this...

Thanks
Regards,
Pankaj
# 11  
Old 05-21-2007
Quote:
Originally Posted by panknil
Thank you very much anbu23 for your reply but my requirement is something different.
its like

FILE A

223|xls file pattern|ABCD|UUU
245|yyy file mmm|TRHJ|ZZZ

FILE B

disa|yyy|555223
asdf|uuu|627245

now look the 1st field of FILE A is matching with the last 3 number of 3rd field of FILE B...
this is the thing i want to do...

223 == 555223
245 == 627245
i want to match like this...

Thanks
Regards,
Pankaj
Code:
awk -F"|" ' NR == FNR { arr[$1]=$1; next }; $3 !~ arr[$1]"$" { print;exit } END { print "Success" } ' filea fileb

# 12  
Old 05-21-2007
Power comparing lines from 2 files

dear anbu23,
thanks a lot...
but can you please explain me your code?

Thankx,
Regards,
# 13  
Old 05-22-2007
Quote:
Originally Posted by panknil
dear anbu23,
thanks a lot...
but can you please explain me your code?

Thankx,
Regards,
Above code doesn't work. Try this
Code:
awk -F"|" ' NR == FNR { arr[$1]=1; next }; 
	    !arr[substr($3, length($3)-2)] { print "Error" ;exit } 
	    END { print "Success" } 
' filea fileb

NR == FNR { arr[$1]=1; next } Keep first field as index and assign one to it. This block is executed till the end of first file.

!arr[substr($3, length($3)-2)] { print "Error" ;exit } Get the last three digit of third field and check whether it is an index in array arr. If not exit the processing.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparing multiple lines in same file

Hello, I would like to write a /bin/ksh script to manipulate a file and compare its contexts. Comparing lines 1 & 2, 3 & 4, 5 & 6, and so forth until the end of the file. This is what I would like the script to compare (using line 1 & 2 as an example): 1. Verify if the last column in line 1 is... (10 Replies)
Discussion started by: seekryts15
10 Replies

2. UNIX for Dummies Questions & Answers

Comparing lines within a word list

Hello all- New to this forum, and relatively new to using grep at the Terminal command line to work with regular expressions. I've got a background in math and some programming experience, so it's not been too difficult to learn the basics of searching through my word lists for particular types of... (13 Replies)
Discussion started by: dtalvacchio
13 Replies

3. Shell Programming and Scripting

Comparing 2 text files & downloading a file if the last lines are different

Hello I'm having a little difficulty in writing a shell script for a few simple tasks. First I have two files "file1.txt" and "file2.txt" and I want to read and compare the last line of each file. The files look like this. File1.txt File2.txt After comparing the two lines I would... (2 Replies)
Discussion started by: RustikGaming
2 Replies

4. UNIX for Dummies Questions & Answers

Comparing lines of data

Total UNIX Rookie, but I'm learning. I have columns of integer data separated by spaces, and I'm using a Mac terminal. What I want to do: 1. Compare "line 1 column 2" (x) to "line 2 column 2" (y); is y-x>=100? 2. If yes, display difference and y's line number 3. If no, increment x and y by... (9 Replies)
Discussion started by: markymarkg123
9 Replies

5. Shell Programming and Scripting

Comparing lines of two different files

Hello, Please help me with this problem if you have a solution. I have two files: <file1> : In each line, first word is an Id and then other words that belong to this Id piMN-1 abc pqr xyz py12 niLM y12 FY4 pqs fiRLym F12 kite red <file2> : same as file1, but can have extra lds... (3 Replies)
Discussion started by: mira
3 Replies

6. UNIX for Dummies Questions & Answers

Comparing two files and count number of lines that match

Hello all, I always found help for my problems using the search option, but this time my request is too specific. I have two files that I want to compare. File1 is the index and File2 contains the data: File1: chr1 protein_coding exon 500 600 . + . gene_id "20532";... (0 Replies)
Discussion started by: DerSeb
0 Replies

7. Shell Programming and Scripting

comparing lines in file

i have 2 files and i want to compare i currently cat the files and awk print $1, $2 and doing if file1=file2 then fail, else exit 0 what i want to do is compare values, with column 1 being a reference i want to compare line by line and then still be able to do if then statement to see if worked... (1 Reply)
Discussion started by: sigh2010
1 Replies

8. Shell Programming and Scripting

Omit Blank Lines while comparing two files.

Hello All, I am writting file comparison Utility and I have encountered such a senario where there are 2 files such as follows- 1#!/usr/local/bin/python 2 import gsd.scripts.admin.control.gsdPageEscalate 3.gsd.scripts.admin.control.gsdPageEscalate.main() 1 #!/usr/local/bin/python... (10 Replies)
Discussion started by: Veenak15
10 Replies

9. Shell Programming and Scripting

Comparing 2 files and return the unique lines in first file

Hi, I have 2 files file1 ******** 01-05-09|java.xls| 02-05-08|c.txt| 08-01-09|perl.txt| 01-01-09|oracle.txt| ******** file2 ******** 01-02-09|windows.xls| 02-05-08|c.txt| 01-05-09|java.xls| 08-02-09|perl.txt| 01-01-09|oracle.txt| ******** (8 Replies)
Discussion started by: shekhar_v4
8 Replies

10. Shell Programming and Scripting

Comparing two files and appending only missing lines.

Hi All, I am a newbie to Shell scripting. Please help me with the Following problem, 1. I have two files with the same name in different locations in the same machine. Eg: /root/testfolder/a ---- location 1 /tmp/testfolder/a ----- location 2 2. I want to compare the files in... (5 Replies)
Discussion started by: Karthick333031
5 Replies
Login or Register to Ask a Question