comparing lines from 2 files


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

Hi Friends,
I have 2 files A and B . I want to compare the 3rd line of file A and B .
(I dont want to compare the 2 files, using diff or cmp). I just want to know whether 3rd line of A matches the 3 rd line of B. Can anybody share their knowledge on the same?

Thanks ,
Vijaya
# 2  
Old 05-21-2007
Code:
third_lne_A=`sed -n '3{p;q;}' fileA'
third_lne_B=`sed -n '3{p;q;}' fileB'
if [ "$third_lne_A" = "third_lne_B" ]; then
      echo "Same..."
fi

# 3  
Old 05-21-2007
Bug comparing lines from 2 files

Dear anbu23,
can you please mention that in this line
third_lne_A=`sed -n '3{p;q;}' $FILENAME1`

what does {p;q;} mean?
the code is working properly.but please let me know {p;q;}

Thankx
Regards,
Pankaj
# 4  
Old 05-21-2007
Quote:
Originally Posted by panknil
Dear anbu23,
can you please mention that in this line
third_lne_A=`sed -n '3{p;q;}' $FILENAME1`

what does {p;q;} mean?
the code is working properly.but please let me know {p;q;}

Thankx
Regards,
Pankaj
3 - line number
p - print command
q - quit command

3{p;q;} If the current line is third line in the file, then print that line and quit sed command. For example if your file has 50,000 lines and if you dont provide quit command, sed will process all the lines in the file. If you use quit command, it will save time.
# 5  
Old 05-21-2007
Power comparing lines from 2 files

Thanx anbu for your quick reply and a nice explanation.

i also want to know that how can i compare field by fileld of two files, like File A and File B?
as you have compared the lines i want to compare the each and every fields of each line of both the file...

Thanks
Regards,
Pankaj
# 6  
Old 05-21-2007
Quote:
Originally Posted by panknil
Thanx anbu for your quick reply and a nice explanation.

i also want to know that how can i compare field by fileld of two files, like File A and File B?
as you have compared the lines i want to compare the each and every fields of each line of both the file...

Thanks
Regards,
Pankaj
If you want to compare all the fields which means comparing whole line with other. If not can you give sample input and output required?
# 7  
Old 05-21-2007
Quote:
Originally Posted by vijaya2006
Hi Friends,
I have 2 files A and B . I want to compare the 3rd line of file A and B .
(I dont want to compare the 2 files, using diff or cmp). I just want to know whether 3rd line of A matches the 3 rd line of B. Can anybody share their knowledge on the same?

Thanks ,
Vijaya
Code:
awk 'FNR == 3 && NR == FNR { line = $0; nextfile } 
     FNR == 3 { 
                 if (line==$0){
		     print "Line equal"
		 }
		 else {
		     print "Line not equal"
		 }                 
		}' file1 file2

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