Compare 2 files and extract the data which is present in other file - awk is not working


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare 2 files and extract the data which is present in other file - awk is not working
# 1  
Old 08-08-2018
Compare 2 files and extract the data which is present in other file - awk is not working

file2 content
Code:
f1

file2 content
Code:
f1,1,2,3,4,5
f1,2,4,6,8,10
f10,1,2,3,4,5
f10,2,4,6,8,10
f5,1,2,3,4,5
f5,2,4,6,8,10

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

output
Code:
f10,1,2,3,4,5
f10,2,4,6,8,10
f5,1,2,3,4,5
f5,2,4,6,8,10

Code:
awk 'FNR==NR{a[$1];next}; ($1 in a)' file2 file1

output
Code:
nothing displaying

any help, I am using ksh, thanks!

Last edited by RudiC; 08-08-2018 at 07:05 PM.. Reason: QUOTE --> CODE
# 2  
Old 08-08-2018
I think you want awk to recognize comma-separated fields, so set the field delimiter to a comma
Code:
awk -F, ...

# 3  
Old 08-08-2018
And, reverse the order of the input files...
# 4  
Old 08-08-2018
Thank you for quick support, it is working fine after giving:

Code:
awk -F, 'FNR==NR{a[$1];next}; ($1 in a)' f1 f2

output
Code:
f1,1,2,3,4,5
f1,2,4,6,8,10

Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, sample output, and code segments.

Last edited by Don Cragun; 08-09-2018 at 12:06 AM.. Reason: Add CODE tags again.
# 5  
Old 08-09-2018
Note that in post #1 in this thread you show us the contents of two files named file2 and you use, but do not show us the contents of a file named file1.

In post #4 you use files named f1 and f2, but have never shown us what is in either of those files.

I would expect that you got the results you wanted from the code you showed us in post #4 if the first file you showed us in post #1 was named f1 and the second file you showed us was named f2. Is that what you intended?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare two files and write data to second file using awk

Hi Guys, I wanted to compare a delimited file and positional file, for a particular key files and if it matches then append the positional file with some data. Example: Delimited File -------------- Byer;Amy;NONE1;A5218257;E5218257 Byer;Amy;NONE1;A5218260;E5218260 Positional File... (3 Replies)
Discussion started by: Ajay Venkatesan
3 Replies

2. Shell Programming and Scripting

Compare two numbers which present in two different files

Hi, I need to compare the two floating or integer numbers which present in two different files, Ex: File 1: col1 col2 col3 11 ssa 13.60 12 ssb 11.00 13 ssc 754.00 File 2:col1 col2 col3 11 sa 12.75 12 sb 11.00 13 sc 763.00 here i have to compare 3 column if 1st column match,... (8 Replies)
Discussion started by: Shenbaga.d
8 Replies

3. Shell Programming and Scripting

Compare files & extract column awk

I have two tab delimited files as given below: File_1: PV16 E1 865 2814 1950 PV16 E2 2756 3853 1098 PV16 E4 3333 3620 288 PV16 E5 3850 4101 252 PV16 E6 83 559 477 PV16 E7 562 858 297 PV16 L2 4237 5658 ... (10 Replies)
Discussion started by: vaibhavvsk
10 Replies

4. Shell Programming and Scripting

compare 2 files and extract the data which is not present in other file with condition

I have 2 files whose data's are as follows : fileA 00 lieferungen 00 attractiop 01 done 02 forness 03 rasp 04 alwaysisng 04 funny 05 done1 fileB alwayssng dkhf fdgdfg dfgdg sdjkgkdfjg funny rasp (7 Replies)
Discussion started by: rajniman
7 Replies

5. Shell Programming and Scripting

FNR>2 is not working and unable to extract data from a file

Hi, I have a file in windows environment and moved to UNIX through FTP (ASCII format). The file is having with tab delimited file. awk ‘FNR>2' file_cust*.txt >>filnal.txt I have the same file in production; it is working fine with the same procedure. Once we receive the file in windows... (1 Reply)
Discussion started by: onesuri
1 Replies

6. Shell Programming and Scripting

Extract data with awk and write to several files

Hi! I have one file with data that looks like this: 1 data data data data 2 data data data data 3 data data data data . . . 1 data data data data 2 data data data data 3 data data data data . . . I would like to have awk to write each block to a separate file, like this: 1... (3 Replies)
Discussion started by: LinWin
3 Replies

7. Shell Programming and Scripting

extract data with awk from html files

Hello everyone, I'm new to this forum and i am new as a shell scripter. my problem is to have html files in a directory and I would like to extract from these some data that lies between two different lines Here's my situation <td align="default"> oxidizability (mg / l): data_to_extract... (6 Replies)
Discussion started by: sbobotex
6 Replies

8. UNIX for Dummies Questions & Answers

AWK, extract data from multiple files

Hi, I'm using AWK to try to extract data from multiple files (*.txt). The script should look for a flag that occurs at a specific position in each file and it should return the data to the right of that flag. I should end up with one line for each file, each containing 3 columns:... (8 Replies)
Discussion started by: Liverpaul09
8 Replies

9. Shell Programming and Scripting

sed or awk to extract data from Xml file

Hi, I want to get data from Xml file by using sed or awk command. I want to get the following result : mon titre 1;Createur1;Dossier1 mon titre 1;Createur1;Dossier1 and save it in cvs file (fichier.cvs). FROM this Xml file (test.xml): <playlist version="1"> <trackList> <track>... (1 Reply)
Discussion started by: yeclota
1 Replies

10. Shell Programming and Scripting

Compare data in 2 files and delete if file exist

Hi there, I have written a script called "compare" (see below) to make comparison between 2 files namely test_put.log and Output_A0.log #!/bin/ksh while read file do found="no" while read line do echo $line | grep $file > /dev/null if then echo $file found found="yes" break fi... (3 Replies)
Discussion started by: lweegp
3 Replies
Login or Register to Ask a Question