LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-08-2011, 10:58 AM   #1
ksaad
LQ Newbie
 
Registered: Nov 2011
Posts: 7
Blog Entries: 1

Rep: Reputation: Disabled
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
 
Old 11-08-2011, 11:06 AM   #2
Andrew Benton
Senior Member
 
Registered: Aug 2003
Location: Birkenhead/Britain
Distribution: Linux From Scratch
Posts: 2,073

Rep: Reputation: 64
Why don't you write a script to process the files?
 
Old 11-08-2011, 11:45 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Code:
awk '{ x[$1] = x[$1] $2 " " } END { for ( y in x ) print y , x[y] } ' File1 File2 | sort -n
 
1 members found this post helpful.
Old 11-08-2011, 12:32 PM   #4
berbae
Member
 
Registered: Jul 2005
Location: France
Distribution: Arch Linux
Posts: 540

Rep: Reputation: Disabled
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
 
Old 11-08-2011, 01:02 PM   #5
ksaad
LQ Newbie
 
Registered: Nov 2011
Posts: 7
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
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!!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
manipulating files in bash congos Linux - Newbie 2 10-07-2011 12:52 PM
Manipulating IMG Files jimmy512 Linux - Newbie 2 05-24-2007 06:06 AM
manipulating files tableyou Linux - Newbie 8 11-08-2005 03:38 AM
Editing/manipulating text files winchester169 Linux - Software 3 08-18-2005 07:01 PM
Manipulating Files in vi Baldorg Linux - General 1 09-19-2003 08:25 PM


All times are GMT -5. The time now is 11:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration