LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Extract column and paste in another file (https://www.linuxquestions.org/questions/linux-newbie-8/extract-column-and-paste-in-another-file-4175508997/)

manish72 06-24-2014 05:55 AM

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


manish72 06-24-2014 08:41 AM

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

schneidz 06-24-2014 08:57 AM

sounds like a job for cut and paste.


All times are GMT -5. The time now is 07:15 PM.