Comparing columns in 2 text files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparing columns in 2 text files
# 1  
Old 12-09-2014
Comparing columns in 2 text files

Hi

i have 2 files

file1.txt

Code:
XX,ZZ,XC,EE,RR,BB
XC,CF,FG,RG,GH,GH

File2.txt
Code:
DF,GH,MH,FR,FG,GH,NOTOK
XX,ZZ,XC,EE,RR,BB,OK


result

Code:
XX,ZZ,XC,EE,RR,BB  OK

look for column1 , XX and if it matches in File2.txt , retrieve the 7 th field from File2 and print in 3 rd file ,
also on next day same comparision but result appened on next column

Code:
XX,ZZ,XC,EE,RR,BB  OK,OK


Last edited by Don Cragun; 12-09-2014 at 05:19 PM.. Reason: Add CODE tags.
# 2  
Old 12-09-2014
Any attempts or ideas from your side?
# 3  
Old 12-13-2014
yes below is idea grep the first column in file1 and compare with
first column in file2 and awk the 7 th column value and print result

Code:
for n in `cat /tmp/file1.txt |awk -F "," '{print $1}'`
do
exec 3< /tmp/file1.txt  while read -u3 line ; do
r=`cat /tmp/file2.txt |grep -i $n |awk -F "," '{print $7}'`
s1=`cat /tmp/file1.txt|grep -i $n |awk -F "," '{print $1}'`                                                                           
s2=`cat /tmp/file1.txt|grep -i $n |awk -F "," '{print $2}'`
s3=`cat /tmp/file1.txt|grep -i $n |awk -F "," '{print $3}'`
s4=`cat /tmp/file1.txt|grep -i $n |awk -F "," '{print $4}'`
echo $s1"," $s2 "," $s3 "," $s4 "," $r ","
done

also for appending in next day , i got a idea to use awk NF

Code:
awk -F, '{$(NF+1)=++i;}1' OFS=, file

---------- Post updated at 01:17 PM ---------- Previous update was at 12:37 PM ----------

also i seen using awk FNR and NR

Code:
awk -f vlookup.awk file2.txt file1.txt 
cat vlookup.awk
FNR==NR{
  a[$1]=$1
  next
}
{ if ($1 in a) {print $1 $2 $3 $4, a[$3]} else {print $1 $2 $3 $4 , "NA"}  }

But really didn't understand this code , just tried after reading blogs

Last edited by Don Cragun; 12-13-2014 at 04:30 PM.. Reason: Add CODE tags again.
# 4  
Old 12-13-2014
I don't understand your "next day" requirement , for the first part you can try

Code:
 
 awk -F',' 'NR==FNR{a[$1]=$0;next} $1 in a {print a[$1]" "$7}' file1 file2 > file3

If you save this result in file 3, and again repeat the same command "next day" with a comma

Code:
 
 awk -F',' 'NR==FNR{a[$1]=$0;next} $1 in a {print a[$1]","$7}'  file3 file2

Here is the same workflow with data...

Code:
 
 $ cat > tmp1
 XX,ZZ,XC,EE,RR,BB
 XC,CF,FG,RG,GH,GH
  
 $ cat > tmp2
 DF,GH,MH,FR,FG,GH,NOTOK
 XX,ZZ,XC,EE,RR,BB,OK
  
  
 $ awk -F',' 'NR==FNR{a[$1]=$0;next} $1 in a {print a[$1]" "$7}' tmp1 tmp2
 XX,ZZ,XC,EE,RR,BB OK
  
 $ awk -F',' 'NR==FNR{a[$1]=$0;next} $1 in a {print a[$1]" "$7}' tmp1 tmp2 > tmp3
  
 $ awk -F',' 'NR==FNR{a[$1]=$0;next} $1 in a {print a[$1]","$7}' tmp3 tmp2
 XX,ZZ,XC,EE,RR,BB OK,OK


Last edited by senhia83; 12-13-2014 at 02:31 PM..
This User Gave Thanks to senhia83 For This Post:
# 5  
Old 12-13-2014
okay first command works

if i want to add NA

Code:
XX,ZZ,XC,EE,RR,BB,OK 
XC,CF,FG,RG,GH,GH,NA

file1 is base template , is that possible to print NA if the first column is not found in file2 as NA in result

this is repeated task , so next day same command if i execute the result should append after last column
Code:
                                                    Day1,Day2
XX,ZZ,XC,EE,RR,BB,OK,NA
 XC,CF,FG,RG,GH,GH,NA,OK

rather redirecting in 3rd file

Last edited by Don Cragun; 12-14-2014 at 04:30 PM.. Reason: Add CODE tags again.
# 6  
Old 12-13-2014
If you want to get help from this forum, you need to explain clearly what you have and what you want, as examples , put them in code tags and refrain from changing requirements every time. Please read the forum rules carefully before posting further.
# 7  
Old 12-13-2014
ok

file1.txt
Code:
 XX,ZZ,XC,EE,RR,BB 
XC,CF,FG,RG,GH,GH

File2.txt

Code:
 DF,GH,MH,FR,FG,GH,NOTOK 
XX,NM,XC,EE,RT,BB,OK

Result

Day1

Code:
XX,ZZ,XC,EE,RR,BB,OK -- > since xx in both file1,file2 in first column matches 
XC,CF,FG,RG,GH,GH,NA --->since XC in file1 is not having entry as first column in f, ile 2 so how to display as NA

if i rerun same command in second day , how to append result a column after Day1 result

Day2

Code:
XX,ZZ,XC,EE,RR,BB,OK,OK
XC,CF,FG,RG,GH,GH,NA,NA


Last edited by Don Cragun; 12-13-2014 at 04:36 PM.. Reason: Add CODE tags again!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Need help in comparing multiple columns from two files.

Hi all, I have two files as below. I need to compare field 2 of file 1 against field 1 of file 2 and field 5 of file 1 against filed 2 of file 2. If both matches , then create a result file 1 with first file data and if not matches , then create file with first fie data. Please help me in... (12 Replies)
Discussion started by: sivarajb
12 Replies

2. Shell Programming and Scripting

Comparing two columns from two different files

Hi, I have a single-column file1 having records like: 00AB01/11 43TG22/00 78RC09/34 ...... ...... and a second file , file 2 having two columns like 78RC09/34 1 45FD11/11 2 00AB01/11 3 43TG22/00 4 ...... ...... (8 Replies)
Discussion started by: amarn
8 Replies

3. UNIX Desktop Questions & Answers

COMPARING COLUMNS IN A TEXT FILE

Hi, Good day. I currently have this data called database.txt and I would like to check if there are no similar values (all unique) on an entire row considering the whole column data is unique. the data is as follows cL1 cL2 cL3 cL4 a12 c13 b13 c15 b11 a15 c19 b11 c15 c17 b13 f14 with... (1 Reply)
Discussion started by: whitecross
1 Replies

4. Shell Programming and Scripting

comparing two columns from two different files

Hello, I have two files as 1.txt and 2.txt with number as columns. 1.txt 0 53.7988 1 -30.0859 2 20.1632 3 14.2135 4 14.6366 5 -37.6258 . . . 31608 -8.57333 31609 -2.58554 31610 -24.2857 2.txt (1 Reply)
Discussion started by: AKD
1 Replies

5. UNIX for Dummies Questions & Answers

Comparing columns in two files

Hi, I have two files. File1.txt has 2 columns and looks like: 458739 122345 4456 122657 34200 122600 File2.txt has many columns with column 1 the same as column2 of File1.txt, but with lot more rows: 122786 abcdefg user1@email 122778 uuhjeufh user2@email... (1 Reply)
Discussion started by: ursaan
1 Replies

6. UNIX for Dummies Questions & Answers

Comparing 2 columns from 2 files

Hi, I have two files with the same number of columns. Basically I want to print the 2 columns that match between the two files. File1 looks like this: dr12 12 6 abn dr14 12 7 abn File2 looks something like this: dr12 12 8 abn dr12 14 7 abn So basically if the first... (1 Reply)
Discussion started by: kylle345
1 Replies

7. Shell Programming and Scripting

comparing 2 columns from 2 files

Hey, I have 2 files that have a name and then a number: File 1: dog 21 dog 24 cat 33 cat 27 dog 76 cat 65 File 2: dog 109 dog 248 cat 323 cat 207 cat 66 (2 Replies)
Discussion started by: dcfargo
2 Replies

8. Shell Programming and Scripting

comparing the columns in two files

I have two files file1 and file 2 both are having multiple coloumns.i want to select only two columns. i used following code to get the desired columns,with ',' as delimiter cut -d ',' -f 1,2 file1 | sort > file1.new cut -d ',' -f 1,2 file2 | sort > file2.new I want to get the coloums... (1 Reply)
Discussion started by: bab123
1 Replies

9. Shell Programming and Scripting

Comparing Columns of two FIles

Dear all, I have two files in UNIX File1 and File2 as below: File1: 1,1234,.,67.897,,0 1,4134,.,87.97,,4 0,1564,.,97.8,,1 File2: 2,8798,.,67.897,,0 2,8879,.,77.97,,4 0,1564,.,97.8,,1 I want to do the following: (1) Make sure that both the files have equal number of columns and if... (4 Replies)
Discussion started by: ggopal
4 Replies

10. UNIX for Advanced & Expert Users

Comparing Columns of two FIles

Dear all, I have two files in UNIX File1 and File2 as below: File1: 1,1234,.,67.897,,0 1,4134,.,87.97,,4 0,1564,.,97.8,,1 File2: 2,8798,.,67.897,,0 2,8879,.,77.97,,4 0,1564,.,97.8,,1 I want to do the following: (1) Make sure that both the files have equal number of columns and if... (1 Reply)
Discussion started by: ggopal
1 Replies
Login or Register to Ask a Question