When I output fields 1 2 4 5 & 6, I would like to have a comma between them but I am beating my head against the wall to get it to work. Any help is appreciated
sed 's/[[:space:]]*,[[:space:]]*/,/g' file1 > file1.$$ && awk -F, 'FNR==NR{f2[$3]=$1 $2 $4 $5 $6;next} FNR==1{print $0, "CDP NE Hostname,CDP NE IP,Remote Interface,VLAN,Admin IP" ;next} {print $0,($2 in f2)?f2[$2]:"NA,NA,NA,NA,NA"}' OFS=, file2 file1.$$ > file3 && rm file1.$$ file1
Assuming you're dealing with the files from your former thread, I can't reproduce your problem with above code. All fieds are comma separated.
If you're talking of populating the f2 array: in awk , strings are concatenated by just listing them in sequence on the assignment side, no operator necessary. So - no comma given - no comma inserted. Assign like f2[$3] = $1 "," $2 "," ...
As the f2 elements are being used for matching file1 's line afterwards, wouldn't it make more sense to intersperse FS characters, then, to prepare for the case FS and OFS are different?
And, are the parentheses really necessary?
The paranthesis increase readability, especially with such a long concatenation.
The OFS make sense because the string is destined for printing. An intuitive thing, at least for me.