File comparision line by line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File comparision line by line
# 8  
Old 06-20-2013
Let me try one more time: I understand that your files' names change daily. I understand that you can't hard code the name of the files to be compared because you want to select a different pair of files to be compared each day. I assume that there could be lots of files in the /tmp/test directory.

Please explain how you select the two files you want to compare out of all of the files that are present in /tmp/test.
# 9  
Old 06-20-2013
Quote:
Originally Posted by rkrish123
The files can be any date, it may contain today day or y'day....

i can't hardcode the filenames as filename changes daily with new time stamps...

Aggr_New_06_15_2013_1944.txt
Aggr_New_06_19_2013_1944.txt


These 2 files are in /tmp/test folder, i have to compare and get difference in other file like Aggr_New.txt in same folder or any other folder.

Aggr_New_06_15_2013_1944.txt has below records

ACCOUNT|497|1108000-new|Jun|2045.00
ACCOUNT|497|Mmnfy-new|Jun|1903.00
ACCOUNT|497|922857-new|Jun|2045.00


Aggr_New_06_19_2013_1944.txt has below records

ACCOUNT|497|1108000-new|Jun|2045.00
ACCOUNT|497|Mmnfy-new|Jun|1903.00
ACCOUNT|497|922857-new|Jun|2045.00
ACCOUNT|497|922865-new|Jun|4509.00
ACCOUNT|497|922987-new|Jun|3249.00
ACCOUNT|497|1|Jun|867.00

Output:

ACCOUNT|497|922865-new|Jun|4509.00
ACCOUNT|497|922987-new|Jun|3249.00
ACCOUNT|497|1|Jun|867.00
I have a question.
e.g.
Assuming that there are 10 lines in the file_A and 5 lines in the file_B, and the content shown below:
file_A
Quote:
A00
A01
A02
A03
A04
A05
A06
B01
B02
B03
B04
file_B
Quote:
B01
B02
B03
B04
B05
please tell me what's your desired output?
# 10  
Old 06-20-2013
Don: i have only 2 files in that folder...these 2 files need to compare and generate output to other folder.

Frank: with that example, i have to get only "B05" as output.
# 11  
Old 06-20-2013
Code:
diff -c file1 file2 | gawk '/^+/ {print $2}' >> file_diff

However, I feel it's wired. Maybe I have no idea about your real need.
# 12  
Old 06-20-2013
Quote:
Originally Posted by rkrish123
The files can be any date, it may contain today day or y'day....

i can't hardcode the filenames as filename changes daily with new time stamps...

Aggr_New_06_15_2013_1944.txt
Aggr_New_06_19_2013_1944.txt


These 2 files are in /tmp/test folder, i have to compare and get difference in other file like Aggr_New.txt in same folder or any other folder.

Aggr_New_06_15_2013_1944.txt has below records

ACCOUNT|497|1108000-new|Jun|2045.00
ACCOUNT|497|Mmnfy-new|Jun|1903.00
ACCOUNT|497|922857-new|Jun|2045.00


Aggr_New_06_19_2013_1944.txt has below records

ACCOUNT|497|1108000-new|Jun|2045.00
ACCOUNT|497|Mmnfy-new|Jun|1903.00
ACCOUNT|497|922857-new|Jun|2045.00
ACCOUNT|497|922865-new|Jun|4509.00
ACCOUNT|497|922987-new|Jun|3249.00
ACCOUNT|497|1|Jun|867.00

Output:

ACCOUNT|497|922865-new|Jun|4509.00
ACCOUNT|497|922987-new|Jun|3249.00
ACCOUNT|497|1|Jun|867.00
If Aggr_New_06_15_2013_1944.txt and Aggr_New_06_19_2013_1944.txt are the only two files in this directory with names ending in .txt, running the following grep command:
Code:
grep -vF -f *.txt

produces the output you want. You can redirect the output to go wherever you want it. Of course if the two files had names that sorted in the opposite order, the results would be completely different; and, you still haven't specified how to determine which file comes first in the comparison.

Hope this helps,
Don
This User Gave Thanks to Don Cragun For This Post:
# 13  
Old 06-21-2013
Thanks Don, its working...but its taking long time compare to "comm" command.
till now we are using manually comm -2 -3 file1 file2 > file3
This command working faster than grep...our file size is large.
# 14  
Old 06-21-2013
Quote:
Originally Posted by rkrish123
Thanks Don, its working...but its taking long time compare to "comm" command.
till now we are using manually comm -2 -3 file1 file2 > file3
This command working faster than grep...our file size is large.
I'm very glad that comm is working for you. And, you're very lucky that it is. The behavior of comm is only defined if both input files are sorted in collating order in the current locale. Neither of your sample input files are sorted, so if comm is producing the output you wanted, it is coincidence.

If you had said that your input files were large and sorted, I would have considered comm before fgrep; but since your sample input files were small and unsorted, I used grep -F instead of sorting both input files and then using comm on the resulting sorted files.

You still haven't specified how you decide which of the files in your directory is supposed to be "file1" and which is supposed to be "file2" in your example above. So, it is also just luck that *.txt happened to give the results you wanted when the results would have been very different if the order of the operands had been reversed.

So, with your real input files, is the command sequence:
Code:
for i in *.txt
do      sort -o $i $i
done
comm -2 -3 *.txt > SomeFile_In_SomeOtherDirectory

still faster than:
Code:
grep -vFf *.txt > SomeFile_In_SomeOtherDirectory

Both of the above command sequences should produce the same output.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

How to read file line by line and compare subset of 1st line with 2nd?

Hi all, I have a log file say Test.log that gets updated continuously and it has data in pipe separated format. A sample log file would look like: <date1>|<data1>|<url1>|<result1> <date2>|<data2>|<url2>|<result2> <date3>|<data3>|<url3>|<result3> <date4>|<data4>|<url4>|<result4> What I... (3 Replies)
Discussion started by: pat_pramod
3 Replies

3. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

4. Shell Programming and Scripting

perl: comparision of field line by line in two files

Hi everybody, First I apologize if my question seems demasiad you silly, but it really took 4 days struggling with this, I looked at books, forums ... And Also ask help to a friend that is software developer and he told me that it is a bad idea do it by perl... but this is my problem. I moved to... (8 Replies)
Discussion started by: Thelost
8 Replies

5. Shell Programming and Scripting

Read file line by line and process the line to generate another file

Hi, i have file which contains data as below(Only sample shown, it may contain more data similar to the one shown here) i need to read this file line by line and generate an output file like the one below i.e based on N value the number of MSISDNs will vary, if N=1 then the following... (14 Replies)
Discussion started by: aemunathan
14 Replies

6. Shell Programming and Scripting

[Solved] Problem in reading a file line by line till it reaches a white line

So, I want to read line-by-line a text file with unknown number of files.... So: a=1 b=1 while ; do b=`sed -n '$ap' test` a=`expr $a + 1` $here do something with b etc done the problem is that sed does not seem to recognise the $a, even when trying sed -n ' $a p' So, I cannot read... (3 Replies)
Discussion started by: hakermania
3 Replies

7. Shell Programming and Scripting

Line by Line Comparision of 2 files and print only the difference

Hi, I am trying to find an alternative way to do tkdiff. In tkdiff the gui compares each line and highlights only the differences. for eg: John works at McDonalds s(test) He was playing guitar tywejk John works in McDonalds 9908 He was playing guitar I am... (1 Reply)
Discussion started by: naveen@
1 Replies

8. UNIX for Advanced & Expert Users

how do you parse 1 line at a time of file1 ie. line(n) each line into new file

File 1 <html>ta da....unique file name I want to give file=>343...</html> <html>da ta 234 </html> <html>pa da 542 </html> and so on... File 2 343 234 542 and so on, each line in File 1 one also corresponds with each line in File 2 I have tried several grep, sed, while .. read, do,... (4 Replies)
Discussion started by: web_developer
4 Replies

9. Shell Programming and Scripting

file comparision by line

i have two files and i want to compare these two it shoud print those lines which are not in 2nd file a.txt 1236,AB,0 2345,CD,1 5679,EF,1 9123,AA,1 9223,AA,0 b.txt 1234,AB,0 2345,CD,1 5678,EF,1 9123,AA,0 o/p 1236,AB,0 5679,EF,1 9123,AA,1 9223,AA,0 (6 Replies)
Discussion started by: aaysa123
6 Replies
Login or Register to Ask a Question