LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 09: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, 10: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, 10:45 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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, 11:32 AM   #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, 12:02 PM   #5
ksaad
LQ Newbie
 
Registered: Nov 2011
Posts: 7

Original Poster
Blog Entries: 1

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



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 11:52 AM
Manipulating IMG Files jimmy512 Linux - Newbie 2 05-24-2007 05:06 AM
manipulating files tableyou Linux - Newbie 8 11-08-2005 02:38 AM
Editing/manipulating text files winchester169 Linux - Software 3 08-18-2005 06:01 PM
Manipulating Files in vi Baldorg Linux - General 1 09-19-2003 07:25 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:13 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
Open Source Consulting | Domain Registration