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
# 1  
Old 08-21-2011
comparing column of two different files and print the column from in order of 2nd file

Hi friends,

My file is like:

Quote:
A
D
F
E
B

Second file is :

Quote:
A
B
C
D
E
F
G
I need to print the rows present in file one, but in order present in second file....I used
Code:
 
while read gh;do
awk ' $1=="' $gh'" {print >> FILENAME"output"} ' cat listoffirstfile
done < secondfile

but the output I am getting is:
Quote:
A
B
D
E
F
Whereas, i want to print it like:

PHP Code:
A
B
ABSENT
D
E
F
ABSENT 
Can any one help me in using the above awk command such that it prints the required output.
# 2  
Old 08-21-2011
Try this

Code:
awk 'NR==FNR{_1[$0]++;next} {if(!_1[$0]){$0="ABSENT"} print }' file1 file2

regards,
Ahamed
# 3  
Old 08-23-2011
Hi Ahamed,

that doesn't work.....
it is showing ABSENT for all the rows Smilie

whereas, i want to print it like:

Quote:
A
B
ABSENT
D
E
F
ABSENT
# 4  
Old 08-23-2011
Code:
awk 'NR==FNR{a[$0]++;next}{if ($0 in a){print $0}else{print "ABSENT"}}' file1 file2

# 5  
Old 08-23-2011
Code:
$ awk 'BEGIN { while(getline a<"file1") ARR[a]=1       }
        {       (ARR[$0]) || $0="ABSENT"        } 1' < file2
A
B
ABSENT
D
E
F
ABSENT
$

If file1 isn't in order, you have to store the whole thing to tell if lines are absent or present.
# 6  
Old 08-23-2011
Using basic unix shell commands:
Code:
#!/usr/bin/ksh
while read mStr; do
  mCnt=$(egrep -c ${mStr} File1)
  if [[ "${mCnt}" = "0" ]]; then
    echo "ABSENT"
  else
    echo ${mStr}
  fi
done < File2

# 7  
Old 08-23-2011
Hi all,

Thanx for the help.....

it is giving the correct output.... Smilie

Furthermore, what should I do if my first file is having multiple columns, like:

Quote:
fds 21 sdf 32 6432
kjh 54 lkj 45 4567
lkj 78 uio 56 5342
bnm 56 hfv 67 456
and i want to arrange my data as in second file:

Quote:
lkj
bnm
kjh
fds
and want to print the 5th row of first file, in the order present in second file

required output should be:

Quote:
5342
456
4567
6432
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