Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 06-24-2014, 05:55 AM   #1
LQ Newbie
Registered: Mar 2011
Posts: 11

Rep: Reputation: 0
Extract column and paste in another file


I have 2 input files-file 1 and file 2 with delimiter '|'.I need to extract 3rd column from file 1 and insert it after 3rd column in file 2.With my limited awk knowledge,this is what I have so far:

awk 'FS=OFS="|"{field[NR]=$3}; FS=OFS="|"{$3=($3 FS field[FNR]);  print}' file1 file2
As of now, the $3 value in file2 is getting replaced. I don't want to lose the $3 value of file 2.Rather have the new value inserted after $3.Any help appreciated.Thanks!

Sample data:
File 1
File 2
Op required:

Last edited by manish72; 06-24-2014 at 06:02 AM.
Old 06-24-2014, 08:41 AM   #2
LQ Newbie
Registered: Mar 2011
Posts: 11

Original Poster
Rep: Reputation: 0
I have now managed to correct the command to some extent. The op is now two rows- in 1st row, the $3 of file 2 has got replaced. The 2nd row has correct entries with existing $3 and extracted data inserted after $3.

awk -F"|" 'FNR==NR{field[NR]=$3}; FNR<NR{$4=field[FNR] FS $4}1;' OFS="|" file1 file2
Old 06-24-2014, 08:57 AM   #3
LQ Guru
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,258

Rep: Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914
sounds like a job for cut and paste.


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
[SOLVED] Extract a column from file 1 and insert in file 2 ksvinaykumar Linux - Newbie 2 04-10-2012 06:20 AM
compare second column of a file then print the first column of it in a ne fil if true java_girl Linux - Newbie 2 03-16-2012 04:50 AM
How-to cut specific text from cell and paste into new column ivn Linux - Newbie 5 12-17-2011 08:53 PM
[SOLVED] Not able to extract last column from awk vinaytp Linux - Newbie 4 05-20-2011 04:27 AM
[SOLVED] Perl extract information for a particular column kdelover Programming 7 10-26-2010 03:24 AM > Forums > Linux Forums > Linux - Newbie

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

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