1

I want to add 6 field of File2 in last field of File1, i'm using following command but unable to get output.

awk 'FNR==NR{a[$1]=$6; next} {print $0,a[$1]}' File2 File1

File1:

zehriscollection.co.uk,IPAddress,hqfmqxvm,[email protected],2015-06-06 16:34,home,8000,4,Professional Linux Based,paper_lantern,root,Hostname,1433590496,4212,8192000
zindagidesire.com,IPAddress,hgchcjhcj,[email protected],2015-08-19 18:16,home,8000,107,Professional Linux Based,paper_lantern,root,Hostname,1439990214,110126,8192000
zobasra.co.uk,IPAddress,egranius,"[email protected], [email protected]",2013-11-30 19:07,home,3072,4,Standard,x3,root,Hostname,1385820470,4208,3145728

File2:

zehriscollection.co.uk        hqfmqxvm         Usage:  4.02M      Inodes:  275
zindagidesire.com             hgchcjhcj        Usage:  107.19M    Inodes:  4765
zobasra.co.uk                 egranius         Usage:  4.02M      Inodes:  390

What i want to get:

zehriscollection.co.uk,IPAddress,hqfmqxvm,[email protected],2015-06-06 16:34,home,8000,4,Professional Linux Based,paper_lantern,root,Hostname,1433590496,4212,8192000,275
zindagidesire.com,IPAddress,hgchcjhcj,[email protected],2015-08-19 18:16,home,8000,107,Professional Linux Based,paper_lantern,root,Hostname,1439990214,110126,8192000,4765
zobasra.co.uk,IPAddress,egranius,"[email protected], [email protected]",2013-11-30 19:07,home,3072,4,Standard,x3,root,Hostname,1385820470,4208,3145728,390

2 Answers 2

2

This does the job:

awk '{print $6}' File2 | paste -d ',' File1 -

The - at the end is the standard input that's piped in from awk.

EDIT: When you need to make sure, that the domain names match in the files, you can do it with join instead of paste.

For example like this:

sort File2 | awk '{print $1,",",$6}' | sed 's/ //g' | join -t ',' File1 -
8
  • FYI, when I tested this, I had to remove the additional whitespaces at the end of the lines of File1.
    – fancyPants
    Commented Jun 7, 2016 at 10:44
  • If the file2 contains random domains than it doesn't put the valid fields against each domain.
    – blaCkninJa
    Commented Jun 7, 2016 at 10:53
  • @rlinux57 I edited my answer to include an example that worked for me when not all domain names match.
    – fancyPants
    Commented Jun 7, 2016 at 11:09
  • Why you use join -t ',' File1 - could you explain a bit ?
    – blaCkninJa
    Commented Jun 7, 2016 at 11:20
  • The -t switch specifies the character to use as input and output field separator. This can't be specified separately, unfortunately. That's why I included the , in the awk script as well and removed the additional spaces created by awk. Actually I have no clue about awk. There might be a more elegant way to do this. Anyway, the -t ',' is just, so that the fields are separated in the result as you wish. Is that what you were asking for?
    – fancyPants
    Commented Jun 7, 2016 at 11:34
1

One way using the split function:

awk 'FNR==NR{a[$1]=$6; next}
{split($0, b, ","); u=b[1]; if (u in a) {$0=$0","a[u]}}
1' file2 file1

It saves 6th field from file2 in a[1st field] then for each line of file1 it splits it on comma into array b and assigns the first element b[1] to u. If u is in a it appends a comma and a[u] to the line. The final 1 prints each line of file1 whether it was modified or not.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .