LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   manipulating field of two files (https://www.linuxquestions.org/questions/linux-newbie-8/manipulating-field-of-two-files-912484/)

ksaad 11-08-2011 09:58 AM

manipulating field of two files
 
Hi, I have two files:

File1:

site Id vlan Id
723 841
479 326
192 288
149 682
649 1471
1050 480
1094 752

File2:

site id ip
149 10.247.98.234
1050 10.247.98.250
1094 10.247.99.12
192 10.247.110.10
479 10.247.97.13
723 10.247.99.130
649 10.247.99.20

I need the output to be:

Site vlan ip
723 841 10.247.99.130
479 326 10.247.97.13
192 288 10.247.110.10
149 682 10.247.98.234
649 1471 10.247.99.20
1050 480 10.247.98.250
1094 752 10.247.99.12


matched 1st field of file1 with 1st field of file2 and prints 2nd field of file2 that corresponding to the matched ones. Thanks

Andrew Benton 11-08-2011 10:06 AM

Why don't you write a script to process the files?

druuna 11-08-2011 10:45 AM

Code:

awk '{ x[$1] = x[$1] $2 " " } END { for ( y in x ) print y , x[y] } ' File1 File2 | sort -n

berbae 11-08-2011 11:32 AM

Nice one liner druuna!

The pipe can be integrated in the awk script:
Code:

awk '{ x[$1] = x[$1] $2 " " } END { for ( y in x ) print y , x[y] | "sort -n"} ' File1 File2

ksaad 11-08-2011 12:02 PM

I trield:

awk 'BEGIN{IGNORECASE=1}FNR == NR{a[$1]=$2;next}{for(i in a)if($1 ~ i)print $1,a[i],$2}' ips vlans > out1

works fine

I need Info. refreshing on paste command:

paste '$1 file1' out1 Not sure!!


All times are GMT -5. The time now is 01:12 PM.