Search for different lines in 2 files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Search for different lines in 2 files
# 1  
Old 12-09-2008
Search for different lines in 2 files

Hi guys

I need help in comparing 2 files

for example

file1:

asdf|gdddd|acx
asdf|vcxzz|eww
avxc|bvvce|qwe

file2:
qwer|asdfg|sss
avxc|bvvce|eww
aass|weews|llk

I need to get the lines that are present in file1 but not found in file2.

so in these case
file3 will contain the lines which are present in file1 but not in file2

file3:
asdf|gdddd|acx
avxc|bvvce|qwe

I tried using sdiff but i cant understand the results.

Help me please.
# 2  
Old 12-09-2008
if your files are sorted, look into 'man comm'.

Otherwise:
Code:
nawk 'NR==FNR {a[$0];next} !($0 in a)' file2 file1

# 3  
Old 12-09-2008
Hammer & Screwdriver Here is a way to get the lines unique to file1

Most (all ?) commands to find unique or common lines want the files sorted.
Code:
> sort file101 >file101.s
> sort file102 >file102.s

Then, I display the files to show them in sorted order. Done only to see, not needed as part of a 'solution'.
Code:
> cat file101.s
asdf|gdddd|acx
asdf|vcxzz|eww
avxc|bvvce|qwe
> cat file102.s
aass|weews|llk
avxc|bvvce|eww
qwer|asdfg|sss

The comm command can show what is in common, or what exists in only one of the files - review the manpages for more info
Code:
> comm -23 file101.s file102.s
asdf|gdddd|acx
asdf|vcxzz|eww
avxc|bvvce|qwe

Unlike your example output, all lines are unique to file101, thus all print. Review the third part of you lines and you will notice the differences. However, this should demonstrate the method to solve your problem.
# 4  
Old 12-09-2008
sorry bout the wrong inputs
The files that i am really going to compare consists of 7000+ lines i think they are already sorted
for my sample:
file1:

asdf|gdddd|acx
asdf|vcxzz|eww
avxc|bvvce|qwe

file2:
qwer|asdfg|sss
avxc|bvvce|qwe
aass|weews|llk

output file:
asdf|gdddd|acx
avxc|bvvce|qwe
# 5  
Old 12-09-2008
Hammer & Screwdriver With those revised input files...

Code:
> sort file101 >file101.s
> sort file102 >file102.s

> comm -23 file101.s file102.s
asdf|gdddd|acx
asdf|vcxzz|eww

So, two entries are unique to the first file, although they are not the sample lines you show.
# 6  
Old 12-09-2008
ok thanks

i tried using sdiff ang use grep to search for the lines with '<' which means unique to the first file then i tried using comm command which is simplier how come the results differ? which is more accurate? When i use sdiff it produces 3569 lines while it only produces 3540 lines when i use comm -23 command.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to print lines from a files with specific start and end patterns and pick only the last lines?

Hi, I need to print lines which are matching with start pattern "SELECT" and END PATTERN ";" and only select the last "select" statement including the ";" . I have attached sample input file and the desired input should be as: INPUT FORMAT: SELECT ABCD, DEFGH, DFGHJ, JKLMN, AXCVB,... (5 Replies)
Discussion started by: nani2019
5 Replies

2. Shell Programming and Scripting

Search multi lines in to multiple files

how to find multiple lines in to multiple files if matched lines then do f1 f2 f3 are files ,lines contain matched lines f1 asdasdasdasd asdasdasdasd abc def Business date cannot be as of date ghi sdasdasdasda asdasdasdada f2 (1 Reply)
Discussion started by: Kalia
1 Replies

3. Shell Programming and Scripting

Search pattern on logfile and search for day/dates and skip duplicate lines if any

Hi, I've written a script to search for an Oracle ORA- error on a log file, print that line and the .trc file associated with it as well as the dateline of when I assumed the error occured. In most it is the first dateline previous to the error. Unfortunately, this is not a fool proof script.... (2 Replies)
Discussion started by: newbie_01
2 Replies

4. Shell Programming and Scripting

Print n lines from top and n lines from bottom of all files with .log extenstion

Oracle Linux 6.4 In a directory I have more than 300 files with the extension .log I want the first 5 and last 5 lines of these .log files to be printed on screen with each file's name. Expected output : Printing first 5 and last 5 lines of FX_WT_Feb8_2014.log !! Authentication... (7 Replies)
Discussion started by: kraljic
7 Replies

5. UNIX for Advanced & Expert Users

SQL script with 86000 lines: new files with only 10000 lines (per file)

Hi this is my SQL script $ wc -l insert_into_customers.sql 85601 insert_into_customers.sqlI wish to cut this file into 9 files each 10000 lines (the last one less) $ wc -l insert_into_customers_00*.sql 10000 insert_into_customers_001.sql 10000 insert_into_customers_002.sql ... (1 Reply)
Discussion started by: slashdotweenie
1 Replies

6. Shell Programming and Scripting

search and replace, when found, delete multiple lines, add new set of lines?

hey guys, I tried searching but most 'search and replace' questions are related to one liners. Say I have a file to be replaced that has the following: $ cat testing.txt TESTING AAA BBB CCC DDD EEE FFF GGG HHH ENDTESTING This is the input file: (3 Replies)
Discussion started by: DeuceLee
3 Replies

7. Shell Programming and Scripting

merging two .txt files by alternating x lines from file 1 and y lines from file2

Hi everyone, I have two files (A and B) and want to combine them to one by always taking 10 rows from file A and subsequently 6 lines from file B. This process shall be repeated 40 times (file A = 400 lines; file B = 240 lines). Does anybody have an idea how to do that using perl, awk or sed?... (6 Replies)
Discussion started by: ink_LE
6 Replies

8. SuSE

Search all files based on first and in all listed files search the second patterns

Hello Linux Masters, I am not a linux expert therefore i need help from linux gurus. Well i have a requirement where i need to search all files based on first patterns and after seraching all files then serach second pattern in all files which i have extracted based on first pattern.... (1 Reply)
Discussion started by: Black-Linux
1 Replies

9. UNIX for Advanced & Expert Users

Search files with specfic extention and later search content

Hi, I would appriciate if somebody can help me figure out how to search for all the *.xml file under a specific directory and subdirectroies (/home/username) and later search of content "<start>" inside the xml file returned by search. -Lovin.V (2 Replies)
Discussion started by: lovi_v
2 Replies

10. UNIX for Advanced & Expert Users

Search a list of lines in file into files

I have a file (file1) which is like host1 host2 host3 host4 the list goes on............ Now I want the above lines in files to be compared with files under /opt/new/ File names are as below: Dev Prod QA And suppose host1 from file1 is found under Dev(file under /opt/new)... (2 Replies)
Discussion started by: sriram003
2 Replies
Login or Register to Ask a Question