1

I have 3 files

1file

 14/09/15
 14/09/15
 14/09/15
 14/09/15
 14/09/15

2file

14/09/01 
14/09/01
14/09/01
14/09/01
14/09/01

and 3file

15/09/14,11-37,01/09/14,1224A,0G,71%,RGS
15/09/14,11-41,01/09/14,2700A,0G,94%,RAN
15/09/14,11-43,01/09/14,2701A,0G,100%,RAN
15/09/14,11-44,01/09/14,2701B,0G,92%,RAN
15/09/14,11-46,01/09/14,2708A,0G,88%,RAN

I need replace the column 1 from 3f with the column 1 from 1f and the third from 3f with the column 1 of 2f

How can I replace using awk?

1
  • i tried awk 'FNR==NR{a[NR]=$3;next}{$1=a[FNR]}1 a awk like this but dont work, thanks for the suport Commented Nov 12, 2014 at 21:08

1 Answer 1

1

The following awk code will be usefull

$ awk   'BEGIN{OFS=FS=","}NF==1{line[FNR]=line[FNR]","$0} NF>1{split(line[FNR], a); $1=a[2]; $2=a[3]; print $0}' 1file 2file 3file
14/09/15,14/09/01,01/09/14,1224A,0G,71%,RGS
14/09/15,14/09/01,01/09/14,2700A,0G,94%,RAN
14/09/15,14/09/01,01/09/14,2701A,0G,100%,RAN
14/09/15,14/09/01,01/09/14,2701B,0G,92%,RAN
14/09/15,14/09/01,01/09/14,2708A,0G,88%,RAN

What it does??

  • OFS=FS="," sets output field separatorOFS and field separator FS as ,

  • NF==1{line[FNR]=line[FNR]","$0} If number of fields/columsn , NF is one, save the value in line variable seperated by commas

  • NF>1{split(line[FNR], a); $1=a[2]; $2=a[3]; print $0} takes the action when NF greater than one

    • split(line[FNR], a); split the line variable to array a

    • $1=a[2]; $2=a[3]; sets the first and second column

    • print $0 prints the entire record

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.