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
# 22  
Old 02-25-2009
Hi All thanks for all your help.
In continuation to that i have one more problem
In the final out put file i have to insert a string (EG: FAILED)

i have tried all possible ways
1) (echo '0a'; echo 'FAILED '; echo '.'; echo 'wq') | ed -s /tmp/myfile
but it is not working when i am ruunning the script
a ? is coming.

i have tried other way also
2)sed 's/^.*$/FAILED;&/'

but it is showing command garbled

3) i have tried this also

sed '1i FAILED' /tmp/myfile

but it is also not working.

Can anyone help me out??


Thanks in advance
# 23  
Old 02-25-2009
Code:
awk 'NR==FNR{a[$0];next}!($1 in a)END{print "FAILED"}' file2 file1

Regards
# 24  
Old 02-26-2009
Thanks a lot..!!
# 25  
Old 02-26-2009
Hi Franklin52,
Thanks for your ur update,but i guess this does not fill the requirement and hence i am getting it wrong.

The line ==>> nawk 'NR==FNR{a[$0];next}!($1 in a)' file2 file1

what this line will do it will each string on each line from file two and compare it with strings present in file1 and print all the string which are present in file1.
Now my requirement is that i have collected all the strings in some third file and if there are some strings in the third file then only i want to insert a string at the beginning of the file.Because later i would be testing whether any string got collected in the third file.


Eg:
nawk 'NR==FNR{a[$0];next}!($1 in a)' file2 file1 >> /tmp/file3

if [ -s /tmp/file3 ] ; then
==== here it is checking whether there is any content in file3 and if this condition is true then i want to insert a string (EG:FAILED) at the beginning of the file /tmp/file3

How can i do it.

Because as franklin said if i am doing
awk 'NR==FNR{a[$0];next}!($1 in a)END{print "FAILED"}' file2 file1 >> /tmp/file3

and if file1 and file2 contents are same then /tmp/file3 file will be empty and there i dont need to insert any string but with this line :
awk 'NR==FNR{a[$0];next}!($1 in a)END{print "FAILED"}' file2 file1 >> /tmp/file3

it is inserting that string event to the empty file.


i hope i have made my piont clear,if not please let me know. Please help..!!

Thanks
# 26  
Old 02-26-2009
Something like this?

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

Regards
# 27  
Old 02-26-2009
Data

Hi Franklin
Thanks for the reply but when i am executing that line:

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


i am getting this:
nawk: syntax error at source line 1
context is
NR==FNR{a[$0];next}!($1 in >>> a{ <<<
nawk: bailing out at source line 1

Cau you help??
# 28  
Old 02-26-2009
Sorry, I made a typo:

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

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