Comparing two one-line files and selecting what does not match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparing two one-line files and selecting what does not match
# 8  
Old 08-15-2018
How about
Code:
awk 'NR == 1 {for (n=split($0, T); n; n--) F1[T[n]]; next} !($1 in F1)' file[12]
10
10
200
200
50

This User Gave Thanks to RudiC For This Post:
# 9  
Old 08-15-2018
diff reports differences between two files and what has to happen to change the first file into the second. If a record appears in the first file but not the second, diff reports the line(s) on the first file a d and the line where they used to be on the 2nd file

A record on the 2nd file but not the first, a a reports the line number of the first file and the records added.

File 1 contains (actually 1 per line) 1 3 5 9 10 11 12 23 48 and
File 2 contains (actually 1 per line) 2 4 6 8 9 yy 10, the output from diff will be
Code:
1,3c1,4
< 1
< 3
< 5
---
> 2
> 4
> 6
> 8
4a6
> yy
6,9d7
< 11
< 12
< 23
< 48

Only lines starting with ">" or "<" appear in one file but not the other. You should never have a change where you have both symbols and a "---" line.
# 10  
Old 08-16-2018
@wbport thanks for the explanation, but this solution would require more data handling so I'll go with the other one

@RudiC, this works but may I ask if I understood the code well:
  • NR == 1 ... while it is reading the first line of the first file do everything in the curly brackets

  • The for loop changes the value of n from the total number of pieces resulting from split to 0

  • F1 is an associative array containing different pieces from array T as it goes through the loop, i.e. all the numbers from file1 that I need

  • next tells it go to the next line, which ends the NR == 1 condition, and starts reading file2 since there is only one line in file1

  • It then reads file2 where it checks for every line if it does not match any of the elements of array F1
# 11  
Old 08-16-2018
  • NR == 1 ... while it is reading the first line of the first file do everything in the curly brackets - YES
  • The for loop changes the value of n from the total number of pieces resulting from split to 0 - YES
  • F1 is an associative array containing different pieces from array T as it goes through the loop, i.e. all the numbers from file1 that I need - YES - in its index
  • next tells it go to the next line, which ends the NR == 1 condition, and starts reading file2 since there is only one line in file1 - YES
  • It then reads file2 where it checks for every line if it does not match any of the elements of array F1 - YES
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Selecting section and removing match

I have a file with contents as shown in file.texi Would like to keep only the sections that have inlineifset till the empty line is reached. Finally replace the following string with a space @inlineifset{mrg, @opar{@bullet{} I had written the following command but it messed my file ... (6 Replies)
Discussion started by: Danette
6 Replies

2. UNIX for Beginners Questions & Answers

Comparing two files and list the difference with common first line content of both files

I have two file as given below which shows the ACL permissions of each file. I need to compare the source file with target file and list down the difference as specified below in required output. Can someone help me on this ? Source File ************* # file: /local/test_1 # owner: own #... (4 Replies)
Discussion started by: sarathy_a35
4 Replies

3. Shell Programming and Scripting

Comparing two columns in two files and printing a third based on a match

Hello all, First post here. I did not notice a previous post to help me down the right path. I am looking to compare a column in a CSV file against another file (which is not a column match one for one) but more or less when a match is made, I would like to append a third column that contains a... (17 Replies)
Discussion started by: dis0wned
17 Replies

4. Shell Programming and Scripting

Match string from two files and print line

Hi, I have been trying to find help with my issue and I'm thinking awk may be able to do it. I have two files eg file1.txt STRING1 230 400 0.36 STRING2 400 230 -0.13 STRING3 130 349 1 file2.txt CUFFFLINKS 1 1394 93932 . + STRING1 CUFFFLINKS ... (9 Replies)
Discussion started by: zward
9 Replies

5. Shell Programming and Scripting

Selecting nearest pattern match

I'm looking to match an error code against a list of possible codes and get the nearest match. The code would be a 6 character hexadecimal string. I have a file of error codes all of which have a specific first 3 characters, however, after that the last 3 characters may be specific or generic as... (3 Replies)
Discussion started by: dazedandconfuse
3 Replies

6. Shell Programming and Scripting

Comparing two files line by line

Hi All, I want to compare two files using shell script. One file will be input file and each line of input file will be compared against the other file. for e.g. File 1 10.3.242.170 saquatch Tesr.adc.unix.com jndi_p1 jndi_p1.unix.com 10.3.242.171 ness... (10 Replies)
Discussion started by: sharsour
10 Replies

7. UNIX for Dummies Questions & Answers

Comparing two test files and printing out the values that do not match

Hi, I have two text files with matching first columns. Some of the values in the second column do not match. I want to write a script to print out the rows (only the first column) where the values in the second column do not match. Example: Input 1 A 1 B 2 C 3 D 4 Input 2 A 2 B 2... (6 Replies)
Discussion started by: evelibertine
6 Replies

8. UNIX for Dummies Questions & Answers

Comparing two text files by a column and printing values that do not match

I have two text files where the first three columns are exactly the same. I want to compare the fourth column of the text files and if the values are different, print that row into a new output file. How do I go about doing that? File 1: 100 rs3794811 0.01 0.3434 100 rs8066551 0.01... (8 Replies)
Discussion started by: evelibertine
8 Replies

9. Shell Programming and Scripting

Comparing two files and printing 2nd column if match found

Hi guys, I'm rather new at using UNIX based systems, and when it comes to scripting etc I'm even newer. I have two files which i need to compare. file1: (some random ID's) 451245 451288 136588 784522 file2: (random ID's + e-mail assigned to ID) 123888 xc@xc.com 451245 ... (21 Replies)
Discussion started by: spirm8
21 Replies

10. UNIX for Dummies Questions & Answers

Comparing two files and count number of lines that match

Hello all, I always found help for my problems using the search option, but this time my request is too specific. I have two files that I want to compare. File1 is the index and File2 contains the data: File1: chr1 protein_coding exon 500 600 . + . gene_id "20532";... (0 Replies)
Discussion started by: DerSeb
0 Replies
Login or Register to Ask a Question