Help with comparing contents of a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with comparing contents of a file
# 1  
Old 06-18-2015
Help with comparing contents of a file

I have a script that outputs a file in the below format

Code:
group1:user1,user2,user3, user4
group2:user2, user4, user5
group3: user1, user6, user7

If there is a change in /etc/passwd with addition/deletion of users, then the next run of the script will output the file with the changes.

How can I have my script compare the 2 versions of the files and also show the changes(addition/deletion of users) in the new file

Last edited by Don Cragun; 06-18-2015 at 07:25 PM.. Reason: Add CODE and ICODE tags.
# 2  
Old 06-18-2015
Please use code tags as reuqired by forum rules!

Before we start guessing please provide a sample (in code tags) of the output you desire.
# 3  
Old 06-22-2015
I tried using the diff command, it tells me the files have been changed. But, it does not give me the exact changes when I use them in my script
Code:
diff final_op.txt final_op.txt_prev` 2>/dev/null
if [ $? -eq 0 ]
then
  echo " Files unchanged"
else
  echo "File contents have changed"
fi

How can I get the exact changes when I use diff in a script? Thanks for your help
# 4  
Old 06-22-2015
Quote:
Originally Posted by Newbie2015
I tried using the diff command, it tells me the files have been changed. But, it does not give me the exact changes when I use them in my script
Code:
diff final_op.txt final_op.txt_prev` 2>/dev/null
if [ $? -eq 0 ]
then
  echo " Files unchanged"
else
  echo "File contents have changed"
fi

How can I get the exact changes when I use diff in a script? Thanks for your help
That marked red '`' is a mistype?

The logic of the script is giving you a result that it is being interpreted erroneously. Whether the files are equal or different the exit status of diff is going to be successful (0). If the files compares equal, there's not output and your code is going to say: echo " Files unchanged"

---------- Post updated at 09:18 AM ---------- Previous update was at 08:45 AM ----------

By the way, the default of diff is that if both files are identical nothing is output to the stdout. If there are differences, then it outputs showing what changes needs to be done to make them equal.
diff -s: -s flag will report with output as well if files are equal.

man diff: does show more details of what can be done with it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script (sh file) logic to compare contents of one file with another file and output to file

Shell script logic Hi I have 2 input files like with file 1 content as (file1) "BRGTEST-242" a.txt "BRGTEST-240" a.txt "BRGTEST-219" e.txt File 2 contents as fle(2) "BRGTEST-244" a.txt "BRGTEST-244" b.txt "BRGTEST-231" c.txt "BRGTEST-231" d.txt "BRGTEST-221" e.txt I want to get... (22 Replies)
Discussion started by: pottic
22 Replies

2. Shell Programming and Scripting

Folder contents getting appended as strings while redirecting file contents to a variable

Hi one of the output of the command is as below # sed -n "/CCM-ResourceHealthCheck:/,/---------/{/CCM-ResourceHealthCheck:/d;/---------/d;p;}" Automation.OutputZ$zoneCounter | sed 's/$/<br>/' Resource List : <br> *************************** 1. row ***************************<br> ... (2 Replies)
Discussion started by: vivek d r
2 Replies

3. Shell Programming and Scripting

Need Help with Bash - comparing directory contents with list of files

Hey guys, need help with a script I'm trying to write. Basically I need to compare the contents of a folder called "profiles" with a list of files called "template". when the file matches the contents of the folder it needs to set a variable called "checked" to "1" Cookies to anyone... (4 Replies)
Discussion started by: Scriporium
4 Replies

4. UNIX for Advanced & Expert Users

How to find duplicates contents in a files by comparing other files?

Hi Guys , we have one directory ...in that directory all files will be set on each day.. files must have header ,contents ,footer.. i wants to compare the header,contents,footer ..if its same means display an error message as 'files contents same' (7 Replies)
Discussion started by: Venkatesh1
7 Replies

5. Shell Programming and Scripting

Replace partial contents of file with contents read from other file

Hi, I am facing issue while reading data from a file in UNIX. my requirement is to compare two files and for the text pattern matching in the 1st file, replace the contents in second file by the contents of first file from start to the end and write the contents to thrid file. i am able to... (2 Replies)
Discussion started by: seeki
2 Replies

6. Shell Programming and Scripting

I want to delete the contents of a file which are matching with contents of other file

Hi, I want to delete the contents of a file which are matching with contents of other file in shell scripting. Ex. file1 sheel,sumit,1,2,3,4,5,6,7,8 sumit,rana,2,3,4,5,6,7,8,9 grade,pass,2,3,4,5,6,232,1,1 name,sur,33,1,4,12,3,5,6,8 sheel,pass,2,3,4,5,6,232,1,1 File2... (3 Replies)
Discussion started by: ranasheel2000
3 Replies

7. UNIX for Dummies Questions & Answers

compare 2 file contents , if same delete 2nd file contents

Give shell script....which takes two file names as input and compares the contents, is both are same delete second file's contents..... I try with "diff"...... but confusion how to use "diff" with if ---else Thanking you (5 Replies)
Discussion started by: krishnampkkm
5 Replies

8. Shell Programming and Scripting

Comparing contents of files

Hi, I hav two files a1.txt and a2.txt, a1.txt contains: --------------- asdev ebcdev .... a2.txt contains: --------------- asdev ebcdev prod .... a1.txt will be updated by a process,.. now i want to compare two files and i want to see data which is not in a1.txt am i clear....?? ... (3 Replies)
Discussion started by: rrs
3 Replies

9. Shell Programming and Scripting

comparing files to contents of a file

Hi I have a problem trying to run a while statement. I have files under one directory that i need to compare to a value in filex and update that file with the result files in the directory are DFC1. DFC5. DFC345. DFC344. DFC9. The program i am trying to run will take the number... (3 Replies)
Discussion started by: SummitElse
3 Replies

10. Shell Programming and Scripting

Creating file contents using contents of another file

Hi, I am not sure how to start doing this so I hope to get some advice as to how to start. I have 2 files. The source file contains data that I needed is in columns delimited by ";". For example, in this format: "CONTINENT","COUNTRY","CITY","ID" "asia","japan","tokyo","123"... (21 Replies)
Discussion started by: ReV
21 Replies
Login or Register to Ask a Question