LinuxQuestions.org
Review your favorite Linux distribution.
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 06-24-2014, 06:55 AM   #1
manish72
LQ Newbie
 
Registered: Mar 2011
Posts: 11

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


Hi,

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:

Code:
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
Code:
1|2|3|4|5
6|7|8|9|10
File 2
Code:
a|b|c|d|e
f|g|h|i|j
Op required:
Code:
a|b|c|3|d|e
f|g|h|8|i|j

Last edited by manish72; 06-24-2014 at 07:02 AM.
 
Old 06-24-2014, 09:41 AM   #2
manish72
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.

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

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


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
[SOLVED] Extract a column from file 1 and insert in file 2 ksvinaykumar Linux - Newbie 2 04-10-2012 07: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 05:50 AM
How-to cut specific text from cell and paste into new column ivn Linux - Newbie 5 12-17-2011 09:53 PM
[SOLVED] Not able to extract last column from awk vinaytp Linux - Newbie 4 05-20-2011 05:27 AM
[SOLVED] Perl extract information for a particular column kdelover Programming 7 10-26-2010 04:24 AM


All times are GMT -5. The time now is 12:45 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