comparing column of two different files and print the column from in order of 2nd file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting comparing column of two different files and print the column from in order of 2nd file
# 8  
Old 08-23-2011
Code:
awk 'NR==FNR{a[$1]=$5;next}{print a[$0]}' file1 file2

# 9  
Old 08-23-2011
Thanks Bartus,

Sorry!!!! that i couldn't explain my question properly....

I actually want to ask that if my first file is like:
Quote:
fds 21 sdf 32 6432
kjh 54 lkj 45 4567
lkj 78 uio 56 5342
bnm 56 hfv 67 456
and second file is

Quote:
lkj
bnm
sas
kjh
fds
gfd
then the output must be :

Quote:
5342
456
ABSENT
4567
6432
ABSENT
and if first file contains all the rows present in second file it will print the complete 5th row in order as that in 2nd file....

I apologize for my silly mistake.....
# 10  
Old 08-23-2011
I'm having real trouble following your logic there. How does the order get reversed? Why does it print 5342 when 78, uio, and 56 are all absent? Do you really mean the 5th line and only the 5th line?
# 11  
Old 08-23-2011
Quote:
Originally Posted by CAch
Thanks Bartus,

Sorry!!!! that i couldn't explain my question properly....

I actually want to ask that if my first file is like:

and second file is

then the output must be :



and if first file contains all the rows present in second file it will print the complete 5th row in order as that in 2nd file....

I apologize for my silly mistake.....
Try:
Code:
awk 'NR==FNR{a[$1]=$5;next}{print a[$0]?a[$0]:"ABSENT"}' file1 file2

This User Gave Thanks to bartus11 For This Post:
# 12  
Old 08-23-2011
I think I get it...

Code:
awk -v DATA1="datafile1" 'BEGIN {
        COMPLETE=1
        # Read lines like 'fds 21 sdf 32 6432', so A["21"]="6432" etc.
        while(getline <DATA1)
        {
                for(N=1; N<NF; N++)     A[$N]=$NF
                # Save the fifth line.
                if((++L) == 5) LINE=$0;
        }
}

{
        if(A[$1])       print A[$1];
        else
        {
                COMPLETE=0; # Remember if any fields were missing.
                print "ABSENT";
        }
}

END {
        # If no fields were missing, print the complete fifth line.
        if(COMPLETE)    print LINE;
}' < datafile2

This User Gave Thanks to Corona688 For This Post:
# 13  
Old 08-24-2011
Hey corona......Smilie

Thanks a lot....it works well Smilie

But, I didnt understand that which statement is storing the 5th column of first file.....although u mentioned "# Save the fifth line"....... I tried by replacing 5 by 3 or 2 in
Quote:
if((++L) == 3) LINE=$0;
....but it is still giving the same output as in
Quote:
if((++L) == 5) LINE=$0;
, I need to store 2nd and 5th column data in separate files.

Also, my secondfile has 20 rows of strings, whereas in everyoutput, it is showing 21 rows.....last row is always "ABSENT", all above 20 rows are in desired way..... can u plz figure it out. Smilie

---------- Post updated at 12:20 PM ---------- Previous update was at 10:57 AM ----------

Hey Bartus Smilie

ur code also works well.......

But, this is also showing the same problem of printing one extra last row printed ABSENT.

i.e, the second file has 20 rows, and first file has 20 or less rows.....so expected outcome is 20 rows only...but instead of that it is showing 21 rows and every time last row is showing "ABSENT"
# 14  
Old 08-24-2011
Can you post output of
Code:
cat -e file2

?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare 1st column from 2 file and if match print line from 1st file and append column 7 from 2nd

hi I have 2 file with more than 10 columns for both 1st file apple,0,0,0...... orange,1,2,3..... mango,2,4,5..... 2nd file apple,2,3,4,5,6,7... orange,2,3,4,5,6,8... watermerlon,2,3,4,5,6,abc... mango,5,6,7,4,6,def.... (1 Reply)
Discussion started by: tententen
1 Replies

2. Shell Programming and Scripting

Need awk or Shell script to compare Column-1 of two different CSV files and print if column-1 matche

Example: I have files in below format file 1: zxc,133,joe@example.com cst,222,xyz@example1.com File 2 Contains: hxd hcd jws zxc cst File 1 has 50000 lines and file 2 has around 30000 lines : Expected Output has to be : hxd hcd jws (5 Replies)
Discussion started by: TestPractice
5 Replies

3. Shell Programming and Scripting

Locate the files in the first column and copy the files in 2nd column

#cat data.txt file1 folder1 file2 thisforfile2 file3 thisfolderforfile3 lata4 folder4 step 1: create the folder first in column 2 for i in `awk '{print $2}' data.txt` do mkdir /home/data/$i done step 2: locate the files in column1 and stored them into a file for i in... (17 Replies)
Discussion started by: kenshinhimura
17 Replies

4. Linux

Print the 1st column and the value in 2nd or 3rd column if that is different from the values in 1st

I have file that looks like this, DIP-17571N|refseq:NP_651151 DIP-17460N|refseq:NP_511165|uniprotkb:P45890 DIP-17571N|refseq:NP_651151 DIP-19241N|refseq:NP_524261 DIP-19241N|refseq:NP_524261 DIP-17151N|refseq:NP_524316|uniprotkb:O16797 DIP-19588N|refseq:NP_731165 ... (2 Replies)
Discussion started by: Syeda Sumayya
2 Replies

5. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

6. UNIX for Dummies Questions & Answers

Comparing the 2nd column in two different files and printing corresponding 9th columns in new file

Dear Gurus, I am very new to UNIX. I appreciate your help to manage my files. I have 16 files with equal number of columns in it. Each file has 9 columns separated by space. I need to compare the values in the second column of first file and obtain the corresponding value in the 9th column... (12 Replies)
Discussion started by: Unilearn
12 Replies

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

8. Shell Programming and Scripting

Print rows in reverse order if values decrease along the column

Hi, Guys. Please help me to find solution to this problem using shell scripting. I have an INPUT file with 4 columns separated by tab. Each block of records is separated by ----- ----- Sample1 5402 6680 Pattern01 Sample2 2216 2368 Pattern02... (6 Replies)
Discussion started by: sam_2921
6 Replies

9. Shell Programming and Scripting

Writing out 2nd column into one file from multiple files

I have several files that are being generated every 20 minutes. Each file contains 2 columns. The 1st column is Text, 2nd column is Data. I would like to generate one single file from all these files as follows: One instance of 1st column Text, followed by 2nd column Data separated by... (5 Replies)
Discussion started by: subhap
5 Replies

10. Shell Programming and Scripting

How to manipulate first column and reverse the line order in third and fourth column?

How to manipulate first column and reverse the line order in third and fourth column as follws? For example i have a original file like this: file1 0.00000000E+000 -1.17555359E-001 0.00000000E+000 2.00000000E-002 -1.17555359E-001 0.00000000E+000 ... (1 Reply)
Discussion started by: Max Well
1 Replies
Login or Register to Ask a Question