Print commas between awk output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print commas between awk output
# 1  
Old 09-25-2018
Print commas between awk output

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

Code:
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

# 2  
Old 09-25-2018
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 "," ...
# 3  
Old 09-25-2018
Code:
f2[$3]=($1 OFS $2 OFS $4 OFS $5 OFS $6)

This User Gave Thanks to vgersh99 For This Post:
# 4  
Old 09-25-2018
Quote:
Originally Posted by vgersh99
Code:
f2[$3]=($1 OFS $2 OFS $4 OFS $5 OFS $6)


That did it, I must not be searching the right question. What would I search to find this?
# 5  
Old 09-26-2018
Quote:
Originally Posted by vgersh99
Code:
f2[$3]=($1 OFS $2 OFS $4 OFS $5 OFS $6)


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?
# 6  
Old 09-26-2018
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.
This User Gave Thanks to MadeInGermany For This Post:
# 7  
Old 09-26-2018
Thanks, With every day that I spend a little time on this script I am slowly getting better!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using awk to print output based on first field.

Hi Folks, I have one requirement, There is one file, which contains two fields. Based on first field, I need to print an output. Example will be more suitable. Input file like this. abc 5 abc 10 xyz 6 xyz 9 xyz 10 mnp 10 mnp 12 mnp 6 (2 Replies)
Discussion started by: Raza Ali
2 Replies

2. Shell Programming and Scripting

Print awk output in same line ,For loop

My code is something like below. #/bin/bash for i in `ps -ef | grep pmon | grep -v bash | grep -v grep | grep -v perl | grep -v asm | grep -v MGMT|awk '{print $1" "$8}'` do echo $i ORACLE_SID=`echo $line | awk '{print $2}'` USERNAME=`echo $line | awk '{print $1}'` done ============= But... (3 Replies)
Discussion started by: tapia
3 Replies

3. Shell Programming and Scripting

awk print output problem

Hello friends, I have written a script and i need to add some part into it so that i could print out more results depending on more conditions, This is the core part of the script which does the actual work: echo "$j" && nawk -v stat=$2 'NR==FNR &&... (1 Reply)
Discussion started by: EAGL€
1 Replies

4. UNIX for Dummies Questions & Answers

Any awk one liner to print df output?

Hi, OS = Solaris Can anyone advise if there is a one liner to print specific output from a df -k output? Running df from a command line, it sometimes gives me 2 lines for some volume. By re-directing the output to a file, it always gives 1 line for each. Below is an example output,... (4 Replies)
Discussion started by: newbie_01
4 Replies

5. Shell Programming and Scripting

Awk script to run a sql and print the output to an output file

Hi All, I have around 900 Select Sql's which I would like to run in an awk script and print the output of those sql's in an txt file. Can you anyone pls let me know how do I do it and execute the awk script? Thanks. (4 Replies)
Discussion started by: adept
4 Replies

6. Shell Programming and Scripting

awk subtraction and print output

FileA F97S 83 530 K569E 531 736 output shud be F16S K40E it is code sed 's///g' FileA |awk '{print $1-$2+2}' it will print 16 40 anything can come with this output?? (1 Reply)
Discussion started by: cdfd123
1 Replies

7. UNIX for Dummies Questions & Answers

awk {print $NF} output??

Hi, I am trying to debug an old script and have found the problem lies within this function: isIdoc() { # validate the file type fileType=`file $1 | awk '{print $NF}'` && echo 0 || echo 1 } My question is, how can I determine what is in the variable $fileType ? The program is... (1 Reply)
Discussion started by: vervette
1 Replies

8. Shell Programming and Scripting

using perl or awk to print output

suppose u have file File A A -> G C->D A -> R P->A File B A=1 C=2 D=3 E=4 F=5 G=6 H=7 I=8 K=9 L=10 M=11 (5 Replies)
Discussion started by: cdfd123
5 Replies

9. Shell Programming and Scripting

formatted output with commas

var=12345 echo $var >>>12345 printf "%8.1f \n" $var >>> 12345.0 How to get this as 12,345? I suppose I could break into sections by dividing by 1000 or 1000000. But, is the a trick to this? (4 Replies)
Discussion started by: joeyg
4 Replies

10. Shell Programming and Scripting

using awk to search and print output

suppose i have one file file A 18 24 30 35 38 45 55 Another file file B 08_46 A 16 V -0.36 0.23 E : 1.41 08_46 A 17 D -1.04 0.22 E : 0.84 08_46 A 18 Q -0.49 0.12 E : 0.06 08_46 A 19 G 0.50 0.14 E : 0.05 08_46 A 20 V ... (5 Replies)
Discussion started by: cdfd123
5 Replies
Login or Register to Ask a Question