Compare two text files and output difference


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare two text files and output difference
# 8  
Old 08-24-2012
Hi Don, thanks again for the help...

The following is what I currently have :

Code:
#!/bin/sh
# Shell Script to compare file 1 and file 2 and print differences to file 3


# Tool number
toolnr=`uname -n | cut -c2-`

# Log Directory Path
LOG_path="/usr/asm/atl.1001/user_data/error_logs"

# Input file names
file1="ASML_LOGBOOK.TXT"
file2="ASML_LOGBOOK_2.TXT"

diff ${file1} ${file2} |awk '!/^</ {next} {sub(/^../,"");print}' > ${LOG_path}/LOG1001.TXT

When I run this i get a message saying
awk: syntax error near line 1
awk: illegal statement near line 1

Am I running this in the wrong shell?

I tried changing the first line to
Code:
#!bin/awk

But that gives error messages stating the same as above but for line 6, so I remove that variable line from the code and just write the output file name in by hand at the end but then I get an error same as above for line 10.

Im guessing I have one fundamental issue here that someone with any kind of knowledge in scripting would avoidSmilie!

M
# 9  
Old 08-24-2012
Quote:
Originally Posted by martin0852
Hi Don, thanks again for the help...

The following is what I currently have :

Code:
#!/bin/sh
# Shell Script to compare file 1 and file 2 and print differences to file 3


# Tool number
toolnr=`uname -n | cut -c2-`

# Log Directory Path
LOG_path="/usr/asm/atl.1001/user_data/error_logs"

# Input file names
file1="ASML_LOGBOOK.TXT"
file2="ASML_LOGBOOK_2.TXT"

diff ${file1} ${file2} |awk '!/^</ {next} {sub(/^../,"");print}' > ${LOG_path}/LOG1001.TXT

When I run this i get a message saying
awk: syntax error near line 1
awk: illegal statement near line 1

Am I running this in the wrong shell?
I tried this script on my system (running OS X) using #!/bin/sh, #!/bin/bash, and #!/bin/ksh and all three worked just fine (after changing the setting of LOG_path to a directory that exists on my system. It won't work with csh or tcsh.

Quote:
Originally Posted by martin0852
I tried changing the first line to
Code:
#!bin/awk

But that gives error messages stating the same as above but for line 6, so I remove that variable line from the code and just write the output file name in by hand at the end but then I get an error same as above for line 10.

Im guessing I have one fundamental issue here that someone with any kind of knowledge in scripting would avoidSmilie!

M
This is a shell command language script and awk is not a shell command language interpreter, so (as you have found) using /bin/awk as your command interpreter doesn't work.

What operating system are you using? I.e., what is the output from the command uname -a on your system.
# 10  
Old 08-25-2012
Hi Don, just checked the output of
Code:
uname -a

and the following is what I get:

SunOS mxxxx 5.10 where mxxxx is the machine number.

So i looked up the awk command on solaris and i replaced the awk command with /usr/xpg4/bin/awk and it seems to work perfectly!!

the following is my code that does exactly what I want to:

Code:
#!/bin/sh
# Shell Script to compare file 1 and file 2 and print differences to file 3


# Tool number
toolnr=`uname -n | cut -c2-`

# Log Directory Path
LOG_path="/usr/asm/atl.1001/user_data/error_logs"

# Input file names
file1="ASML_LOGBOOK.TXT"
file2="ASML_LOGBOOK_2.TXT"

diff ${file1} ${file2} |/usr/xpg4/bin/awk '!/^</ {next} {sub(/^../,"");print}' > ${LOG_path}/LOG${toolnr}.TXT

I'm pretty sure this will do exactly what I want.

Thanks very much for the help!
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 compare 2 files and prints difference as output sidebyside

Hi All, Am trying script to compare 2 files and print the difference found from old file to new file on line by line basis on side by side display. Basically line by line comparision and files may contain blank line as well I know we have compare/diff commands but i don't how to make... (10 Replies)
Discussion started by: Optimus81
10 Replies

2. Shell Programming and Scripting

Comparing text in 2 files and output difference in another file.

I have 2 files of almost same text apart from 2,3 ending lines. Now I want to get that difference in another file. e.g file1.txt is Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_livecd-lv_root 18G 2.4G 15G 14% / tmpfs 504M ... (12 Replies)
Discussion started by: kashif.live
12 Replies

3. Shell Programming and Scripting

compare multiple files and get the difference

Hi all, i have 50 files .data should be same in these 50 files , so my task is to find the difference. i need a logic , which finds difference between all files and print in output file with file name where it found that difference . i tried below logic , but its not giving me what i want. let... (2 Replies)
Discussion started by: deepakiniimt
2 Replies

4. Shell Programming and Scripting

Compare two files and output difference, by first field using awk.

It seems like a common task, but I haven't been able to find the solution. vitallog.txt 1310,John,Hancock 13211,Steven,Mills 122,Jane,Doe 138,Thoms,Doe 1500,Micheal,May vitalinfo.txt 12122,Jane,Thomas 122,Janes,Does 123,Paul,Kite **OUTPUT** vitalfiltered.txt 12122,Jane,Thomas... (2 Replies)
Discussion started by: charles33
2 Replies

5. Shell Programming and Scripting

Compare 2 files and output only the different text.

I know the diff does this but it does output more info than just the different text (e.g. $ diff file1 file2 29a30 > /home/alex/Pictures/hello.jpg 1694a1696 > /home/alex/Pictures/hi.jpg ) How can I make it output only /home/alex/Pictures/hello.jpg /home/alex/Pictures/hi.jpg ? thank... (2 Replies)
Discussion started by: hakermania
2 Replies

6. UNIX for Dummies Questions & Answers

compare / difference between sub-sections of files

Hi there, I'm sure this question has been asked many times but I can't find any posts with information. How can I check the differences between say lines 20 - 200 in file1 and lines 420 - 600 in file2? Thanks in advance for any help! js (2 Replies)
Discussion started by: js8765
2 Replies

7. Shell Programming and Scripting

Compare two columns in two files and print the difference

one file . . importing table employee 119 . . importing table jobs 1 2nd file . . importing table employee 120 . . importing table jobs 1 and would like... (2 Replies)
Discussion started by: jhonnyrip
2 Replies

8. Shell Programming and Scripting

Compare two files and print the two lines with difference

I have two files like this: #FILE 1 ABCD 4322 26485 JMTJ 5311 97248 XMPJ 4321 58978 #FILE 2 ABCD 4321 26485 JMTJ 5311 97248 XMPJ 4321 68978 What to do: Compare the two files and find those lines that doesn't match. And have a new file like this: #FILE 3 "from file 1" ABCD 4322 26485... (11 Replies)
Discussion started by: kingpeejay
11 Replies

9. UNIX for Dummies Questions & Answers

Compare Files and Output Difference

I have to compare two files for any differences, then output the lab and question number for any differences. This is what I currently have: diff lab2.txt lab2answer.txt > lab2compare.txt Though the output doesn't have to be sent to a .txt (or any sort of log), I found that easier, at least... (2 Replies)
Discussion started by: Joesgrrrl
2 Replies

10. Shell Programming and Scripting

to compare two files and to print the difference

suppose one file P1168S P2150L P85L Q597R R1097C Another file P2150L P85L Q597R R1097C R1379C R1587K Then output shud be R1379C R1587K thanks (5 Replies)
Discussion started by: cdfd123
5 Replies
Login or Register to Ask a Question