Comparing two files line by line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparing two files line by line
# 1  
Old 06-17-2013
Comparing two files line by line

Hi All,

I want to compare two files using shell script. One file will be input file and each line of input file will be compared against the other file.

for e.g.

File 1

Code:
10.3.242.170       saquatch Tesr.adc.unix.com jndi_p1 jndi_p1.unix.com 
10.3.242.171       ness ness.atldc.unix.com Admin_p2 Admin_p2.unix.com
10.3.242.172       grem grem.adc.unix.com jndi_p2 jndi_p2.unix.com

Input file

Code:
 
10.3.242.172       grem grem.adc.unix.com jndi_p2 jndi_p2.unix.com

If any input file entry is not matching it should show that line entry to console or write it to the log file.
# 2  
Old 06-17-2013
You could do this with awk instead of shell script:
Code:
awk 'NR==FNR{A[$1]=$2;next}($2!=A[$1])' input file

This User Gave Thanks to Yoda For This Post:
# 3  
Old 06-17-2013
Thanks Yoda for quick reply.

I dint understand where to give first file in awk command.
I have to compare the First file with the input file.
# 4  
Old 06-17-2013
This is a strict line match, is that what you are after?
Code:
grep -vxFf file2 file1

It would produce:
Code:
10.3.242.170       saquatch Tesr.adc.unix.com jndi_p1 jndi_p1.unix.com 
10.3.242.171       ness ness.atldc.unix.com Admin_p2 Admin_p2.unix.com

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 06-17-2013
Thanks Scrutinizer

I am looking for the strict match from the input file. If the input file entry matches with the file 1 than it should be ok else it should give the entry name in the log file or on console
# 6  
Old 06-17-2013
Scrutinizer approach is correct

Code:
grep -vxFf datafile inputfile

---------- Post updated at 03:16 AM ---------- Previous update was at 12:24 AM ----------

Last edited by rajamadhavan; 06-17-2013 at 10:45 AM..
# 7  
Old 06-17-2013
Thanks Raja,

Let me check the above logic. Will get back to you shortly with the results.

---------- Post updated at 05:18 AM ---------- Previous update was at 04:37 AM ----------

Hi Raj

Logic is working but i have to use the KSH instead of bash in my script.
Can i do the similar thing using #! /bin/bash instead of using ksh
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Comparing two files and list the difference with common first line content of both files

I have two file as given below which shows the ACL permissions of each file. I need to compare the source file with target file and list down the difference as specified below in required output. Can someone help me on this ? Source File ************* # file: /local/test_1 # owner: own #... (4 Replies)
Discussion started by: sarathy_a35
4 Replies

2. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

3. Shell Programming and Scripting

Comparing two one-line files and selecting what does not match

I have two files. One is consisting of one line, with data separated by spaces and each number appearing only once. The other is consisting of one column and multiple lines which can have some numbers appearing more than once. It looks something like this: file 1: 20 700 15 30 file2: 10... (10 Replies)
Discussion started by: maya3
10 Replies

4. Shell Programming and Scripting

Multi line log files to single line format

I want to read the log file which was generate from other command . And the output was having multi line in log files for job name and server name. But i need to make all the logs on one line Source file 07/15/2018 17:02:00 TRANSLOG_1700 Server0005_SQL ... (2 Replies)
Discussion started by: ranjancom2000
2 Replies

5. Shell Programming and Scripting

Perl command line option '-n','-p' and multiple files: can it know a file name of a printed line?

I am looking for help in processing of those options: '-n' or '-p' I understand what they do and how to use them. But, I would like to use them with more than one file (and without any shell-loop; loading the 'perl' once.) I did try it and -n works on 2 files. Question is: - is it possible to... (6 Replies)
Discussion started by: alex_5161
6 Replies

6. Shell Programming and Scripting

How to read a two files, line by line in UNIX script and how to assign shell variable to awk ..?

Input are file and file1 file contains store.bal product.bal category.bal admin.bal file1 contains flip.store.bal ::FFFF:BADC:CD28,::FFFF:558E:11C5,6,8,2,1,::FFFF:81C8:CA8B,::FFFF:BADC:CD28,1,0,0,0,::FFFF:81C8:11C5,2,1,0,0,::FFFF:81DC:3111,1,0,1,0 store.bal.... (2 Replies)
Discussion started by: veeruasu
2 Replies

7. Shell Programming and Scripting

Reading text file, comparing a value in a line, and placing only part of the line in a variable?

I need some help. I would like to read in a text file. Take a variable such as ROW-D-01, compare it to what's in one line in the text file such as PROD/VM/ROW-D-01 and only input PROD/VM into a variable without the /ROW-D-01. Is this possible? any help is appreciated. (2 Replies)
Discussion started by: xChristopher
2 Replies

8. Shell Programming and Scripting

Sed Comparing Parenthesized Values In Previous Line To Current Line

I am trying to delete lines in archived Apache httpd logs Each line has the pattern: <ip-address> - - <date-time> <document-request-URL> <http-response> <size-of-req'd-doc> <referring-document-URL> This pattern is shown in the example of 6 lines from the log in the code box below. These 6... (1 Reply)
Discussion started by: Proteomist
1 Replies

9. Shell Programming and Scripting

how to read the contents of two files line by line and compare the line by line?

Hi All, I'm trying to figure out which are the trusted-ips and which are not using a script file.. I have a file named 'ip-list.txt' which contains some ip addresses and another file named 'trusted-ip-list.txt' which also contains some ip addresses. I want to read a line from... (4 Replies)
Discussion started by: mjavalkar
4 Replies

10. Shell Programming and Scripting

linux shell script to take variables from two different files line by line

Friends I need to have a shell script which will feed variables from two different files line-by-line. For example, I have two files - permission and file_name. Contents of permission is - 644 755 .... contents of file_name /file1 /file2 ..... Now I want 644 permission will be... (4 Replies)
Discussion started by: atanubanerji
4 Replies
Login or Register to Ask a Question