write a script to compare two files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting write a script to compare two files
# 29  
Old 02-26-2009
Hey franklin Thanks for your reply
But
nawk 'NR==FNR{a[$0];next}!($1 in a){print;c++}END{if(c){print "FAILED"}}' file2 file1 >> /tmp/file3

is adding the string FAILED at the end of the file after all the other strings,is there any way to add this string at the beginning of the file.This is very important because after this line of code
i will be checking the condition
if [ -s /tmp/file3 ]

and if there are some string which came as the output of comparison then i have to include FAILED as the starting string.


Currently the code is:
nawk 'NR==FNR{a[$0];next}!($1 in a){print;c++}END{if(c){print "FAILED"}}' file2 file1 >> /tmp/file3
if [ -s /tmp/file3] ; then
echo "FAILED on today " >> /tmp/file3

and it is giving the output as:

text1
text2
text4
FAILED
FAILED on today

but my requirement is

the output should come as:
FAILED text1
FAILED text2
FAILED text4
FAILED on today



Could you please help??

Thanks
# 30  
Old 02-27-2009
With the given solution you should be able to fit the code. Is this what you're looking for?

Code:
nawk 'NR==FNR{a[$0];next}!($1 in a){print "FAILED " $0;c++}END{if(c){print "FAILED on today"}}' file2 file1 >> /tmp/file3

Regards
# 31  
Old 02-27-2009
finally got what i wanted...Thanks..!!
# 32  
Old 03-01-2009
Hi all,
i have a doubt related to above problem only

the line
nawk 'NR==FNR{a[$0];next}!($1 in a){print "FAILED " $0;c++}END{if(c){print "FAILED on today"}}' file2 file1 >> /tmp/file3

it will check whether all the strings in file1 and file2 are same or not and it will print all the strings in file1 which are not present in file2
but now i have encountered a problem
suppose file2 is completely empty then the above line should print all the strings in file1
as file2 is empty, but it is not happening.
Can anyone throw some light what is the problem with the above line of code??

Thanks in advance.
# 33  
Old 03-01-2009
Test the file first:

Code:
[ ! -s file2 ] && echo "file file2 is empty"

Regards
# 34  
Old 03-01-2009
Hi Franklin,Thanks for your reply but i guess i didnt explain my problem well.

See i have two files file1 and file2

file1 will contain some data
like
file1
text1
text2
text3
text4
text5


and file i have obtained by making some changes in some other file.And file two always changes dynamically
like sometimes it may contain

file2
text1
text2
text3



sometimes it may contain
text1
text2
text5

and sometimes it may contain nothing
Now i have to compare these two files and redirect the output to a third file eg :file3which will contain the strings which are present in file1 and not in file2
and also i have to insert a string at the beginning and at the end on all the lines in the output file3.

After that i am using an if condition on the file3 to check whether there is some data in file3.

now my doubt is

nawk 'NR==FNR{a[$0];next}!($1 in a){print "FAILED " $0;c++}END{if(c){print "FAILED on today"}}' file2 file1 >> /tmp/file3

this above line is comparing file1 and file2 and printing all the strings present in file1 but not in file2

so if file1 is

text1
text2
text3

and file2 is empty i.e it does not contain any data then my output file i.e file3

should contain
FAILED text1
FAILED text2
FAILED text3
Timestamp

but my problem is
the given line is not doing that



currently my script is
nawk 'NR==FNR{a[$0];next}!($1 in a){print "FAILED " $0;c++}END{if(c){print "Timestamp"}}' file2 file1 >> /tmp/file3
today=`date`
if [ -s /tmp/file3 ] ; then
echo "FAILED for above servergroups on $today " >> /tmp/file3

else
echo "Successful for $e_host on $today " >> /tmp/file3
fi

Please suggest some change in the line
nawk 'NR==FNR{a[$0];next}!($1 in a){print "FAILED " $0;c++}END{if(c){print "Timestamp"}}' file2 file1 >> /tmp/file3


so that if file2 is completely empty then also i should get all the strings present in file1 with the extra strings inserted at the beginning and at the end.

Thanks in advance.
# 35  
Old 03-01-2009
It don't work if file2 is empty, you must have at least a newline in it.
Place this command before the awk command to place a newline in the file if the file is empty:

Code:
[ ! -s file2 ] && echo > file2

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to open files and write into new one

Hello! I am a real beginner in scripting, so I am struggling with a really easy task! I want to write a script to concatenate several text files onto each other and generate a new file. I wanted the first argument to be the name of the new file, so: ./my_script.sh new_file file1.txt... (5 Replies)
Discussion started by: malajedala
5 Replies

2. Shell Programming and Scripting

Compare two files and write data to second file using awk

Hi Guys, I wanted to compare a delimited file and positional file, for a particular key files and if it matches then append the positional file with some data. Example: Delimited File -------------- Byer;Amy;NONE1;A5218257;E5218257 Byer;Amy;NONE1;A5218260;E5218260 Positional File... (3 Replies)
Discussion started by: Ajay Venkatesan
3 Replies

3. Shell Programming and Scripting

Request to check: compare two files , match same entries, write data before it

Hi all, I have 2 files:Column1 of first file has to be matched with column 3 of second file first file contain DATA like this in 2 columns one with gene name second with whether CAD,HT,RA T2Dor any one column 1 column2 ARFGEF2 CAD DDEF2 CAD PSCD3 CAD PSCD4 CAD CAMK1... (5 Replies)
Discussion started by: manigrover
5 Replies

4. UNIX for Dummies Questions & Answers

Not sure how to write a script to delete certain files?

I'm a total *nix newb so I really could use some help! :o I plan on running a cron job on my server that deletes some files and then backs up and emails the website for archival purposes. Before I get in too deep I'd like to remove some unnecessary files that phpThumb creates. I'm not sure... (4 Replies)
Discussion started by: Sinistral
4 Replies

5. Shell Programming and Scripting

write script for more then 600 files

Hi, i have 600 or more file on my server and from this 600 files i want to make one file. i have written this script SPATH=/etlstg/DAT TPATH=/etlstg/DAT/DAT_PROCESSED FILE_DATE=`TZ=CST+24 date +20%y%m%d` a1=0 while do a2=0 while do a3=0 while ... (1 Reply)
Discussion started by: prasson_ibm
1 Replies

6. Shell Programming and Scripting

Write a new file from 2 files as input to the script

Hi- I am hoping someone can give me some pointers to get me started. I have a file which contains some dn's .e.g file 1 cn=bob,cn=user,dc=com cn=kev,cn=user,dc=com cn=john,cn=user,dc=com I have a second file e.g. file.template which looks something like :- dn: <dn> objectclass:... (5 Replies)
Discussion started by: sniper57
5 Replies

7. Shell Programming and Scripting

to write a script to compare the file size in the current directory and previous dir

hi, i am new to this site. i want to write a script to compare the file size of the files in the current dir with the files in the previous directory. the files name will be same, but the filename format will be as xyzddddyymm.txt. the files will arrive with the month end date(i want to... (5 Replies)
Discussion started by: tweety
5 Replies

8. Shell Programming and Scripting

Need to compare two csv files values and write into another csv file

Hi all, Am new to scripting. So i just need your ideas to help me out. Here goes my requirement. I have two csv files 1.csv 2.csv abc,1.24 abc,1 def,2.13 def,1 I need to compare the first column of 1.csv with 2.csv and if matches then need to compare... (2 Replies)
Discussion started by: chinnahyd
2 Replies

9. Shell Programming and Scripting

script to compare files

HI i wil get input from sql query and that too i can get a list o f files or just one. i have to pick up a file from another directory which hads prefix to this prefix.x.x.x.x.x. And we have to discard prefix and use that file name. we have to compare this file name(no need... (0 Replies)
Discussion started by: pulse2india
0 Replies

10. Shell Programming and Scripting

Script with read/write Files

Hello, I am a Newbie in ksh Unix Script. So I must write a ksh/sh script who read character at a position in a File. So also it must read all the lines who belongs at these characters , then write these lines in a another File. Can you help me , or give little councils to advance with my... (5 Replies)
Discussion started by: steiner
5 Replies
Login or Register to Ask a Question