onlyf onlyf - 1 month ago 6x
Perl Question

Adding a column from another file

i have the following problem :

There is a file, tab delimited :

Code1 Number1 Name1 Phone1
Code2 Number2 Name2 Phone2
Code3 Number3 Name3 Phone3
Code4 Number4 Name4 Phone4

And i have a file :

Surname 1
Surname 2
Surname 3
Surname 4

My desired output is :

Code1 Number1 Name1 Surname1 Phone1
Code1 Number1 Name1 Surname2 Phone1
Code1 Number1 Name1 Surname3 Phone1
Code1 Number1 Name1 Surname4 Phone1

I know i propably have to use awk for this, but i only know how to insert a column of one fixed value in between others, using the :

awk '{ $2=$2"newvalue" print $0 }'

But i have no idea how to read lines from the other file and storing them in newvalue to pull the above desired output off. I do not require an awk specific advise. Thanks for the help.


AWK only:

$ awk 'NR==FNR{a[FNR]=$1$2;next} {print $1,$2,$3,a[FNR],$4}' file2 file1
Code1 Number1 Name1 Surname1 Phone1
Code2 Number2 Name2 Surname2 Phone2
Code3 Number3 Name3 Surname3 Phone3
Code4 Number4 Name4 Surname4 Phone4