LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 08-18-2010, 10:14 AM   #1
secondchanti
LQ Newbie
 
Registered: Jul 2010
Posts: 24

Rep: Reputation: 15
How to cut multiple columns at a time.


Friends,

I am having a souce file (say SOURCEFILE) as below with 5 columns and many rows.

pol name status loan address
123 aaa 21 100 RJY
234 bbb 31 200 RMY
345 ccc 21 300 SJY
456 ddd 41 400 pqr

i want to compare the file (say COMPAREFILE ) which is below :

123
345

I need the following output file (sayOUTPUTFILE) as below :
( ie for file COMPAREFILE i need column 2, column 4 ie name and loan from source file )

ie output file required is to be like below :
123 aaa 100
345 ccc 400

Till now iam using the following method.
1. cutting column 2nd column
2. again cutting 4th column
3. in third, step merging the 2nd and 4 th column.

Friends, pl tell me method of writting single line command or any awk programme to do this in one step.

Thanks

Rao



I need output file (say OUTPUTFILE) as below
 
Old 08-18-2010, 10:30 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
If its delimetered nicely then awk is your friend
Something like:
Code:
awk '{if(FNR == NR)a[$0]++;else if($1 in a)print $1,$2,$4}' COMPAREFILE SOURCEFILE > OUTPUTFILE
 
  


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
how to cut two columns in data file and print file secondchanti Linux - Newbie 6 07-16-2010 06:01 AM
AWK - combining multiple columns AlexYZ Programming 5 02-24-2010 07:09 AM
printing multiple columns with awk kdelover Programming 16 12-16-2009 09:10 AM
sort by multiple columns wakatana Linux - Newbie 5 10-18-2009 03:35 PM

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

All times are GMT -5. The time now is 06:38 PM.

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