UNcommon lines between files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers UNcommon lines between files
# 1  
Old 02-01-2011
UNcommon lines between files

I have two files, file1.txt and file2.txt
I want to find UNcommon between two files
with
Code:
while read line
do
grep -v $line file2.txt
done<file1.txt

I get the reverse grep for every line it reads
# 2  
Old 02-01-2011
diff perhaps?
# 3  
Old 02-01-2011
Actually the content of the lines in both files is more complicated, so I would rather ask for 'the lines that have no common strings'
I want exactly what
Code:
grep -v

does, i just do not want to do this every time in the loop
# 4  
Old 02-01-2011
Can you give short examples of your input files, and what output should be produced?

Otherwise I'd say try grep -vf file1.txt file2.txt
# 5  
Old 02-01-2011
Assume file1.txt is
Code:
name1 num1 num2 num3
name2 num5 num6 num7
name3 num8 num9 num1
name4 num1 num2 num3
name5 num4 num5 num5

and file2.txt is
Code:
name1 str1 str2 str3
name2 str5 str6 str7
name3 str8 str9 str1

I only care about the name, not num or str
The desired output would be
Code:
name4
name5

# 6  
Old 02-01-2011
Code:
awk ' FILENAME=="file2.txt" {arr[$1]++}
        FILENAME=="file1.txt" {if ($1 in arr) {next} else {print $0}  }' file2.txt file1.txt

Note: file2.txt should be the first parameter
This User Gave Thanks to jim mcnamara For This Post:
# 7  
Old 02-01-2011
Code:
cat file1.txt file2.txt | awk '{print $1}' | sort | uniq -u

name4
name5

This User Gave Thanks to methyl For This Post:
 
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

From 2 files create 3rd file with uncommon data

Hi All, I have two files. File1 and File2. Want to create another file with all the records of File1 those are not present in File2. Please guide. Thanks in advanced. Anupam (3 Replies)
Discussion started by: Anupam_Halder
3 Replies

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

4. Shell Programming and Scripting

Merge files based on both common and uncommon rows

Hi, I have two files A (2190 rows) and file B (1100 rows). I want to merge the contents of two files based on common field, also I need the unmatched rows from file A file A: ABC XYZ PQR file B: >LMN|chr1:11000-12456: >ABC|chr15:176578-187678: >PQR|chr3:14567-15866: output... (3 Replies)
Discussion started by: Diya123
3 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

comparing 2 files and creating third file with uncommon content

I want to compare 2 files and create third file with uncommon content. e.g. file1 ajay suhas tom nisha vijay mahish file2 ajay suhas tom nisha expected output file content vijay mahish Is it possible in single command ? Thanks, Ajay (6 Replies)
Discussion started by: ajaypatil_am
6 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. Shell Programming and Scripting

HELP: I need to sort a text file in an uncommon manner, can't get desired results

Hi All I have a flat text file. Each line in it contains a "/full path/filename". The last three columns are predictable, but directory depth of each line varies. I want to sort on the last three columns, starting from the last, 2nd last and 3rd last. In that order. The last three columns... (6 Replies)
Discussion started by: JakeKatz
6 Replies

9. Post Here to Contact Site Administrators and Moderators

program to find the uncommon numbers between two files.

Hi, I need to extract the uncommon numbers from file1 and file2 For Example: File1 1 2 3 4 5 File2 1 2 3 4 5 6 (2 Replies)
Discussion started by: salaathi
2 Replies

10. Shell Programming and Scripting

extracting uncommon part between two files

Hi, I need to extract the uncommon (better say incremental) part from 2 files say file_1 and file_2. file_2 contains everything that is in file_1. That is file_2 has been created internally somehow : cat file_1 temp_file > file_2 My objective is to extract the temp_file part from... (2 Replies)
Discussion started by: sabyasm
2 Replies
Login or Register to Ask a Question