Add Double Quotes Around Fields in Awk Script Output

Add double quotes around fields in AWK script output?

If you want:

add this to the existing script.

You can insert additional \"\" in each argument of print like this:

print "\"admin\"", "\"base\"", ...

Edited:

Yes, perhaps seting OFS is better solution:

BEGIN { OFS="\";\""; } ... print "\"admin", ...., "simple\"";

How to print quotes between value using awk

To specify the field separator, you have to use -F instead of -f; to change what the field separator is for the output, you have to change the OFS variable (the output field separator).

The get quotes around your fields, you can loop over all fields and add them:

$ awk -F"|" -v OFS="," '{for (i=1; i<=NF; ++i){$i="\""$i"\""}}1' infile
"A","B","C","D","E","F"

Alternatively, using sed:

$ sed 's/\([^|]*\)/"\1"/g;y/|/,/' infile
"A","B","C","D","E","F"

This surrounds all sequences of non-pipe characters with quotes, then substitutes all the pipes with commas.

How to add quotes for awk when single and double quotes are already used?

I would go with a simple:

alias sqrun='sq | awk '\''$4 == "R" {print $0}'\'

or with a function, like @DiegoTorresMilano suggested (but unlike aliases, it's not so easy to find a function name that you forgot):

sqrun() { sq | awk '$4 == "R" {print $0}'; }

or if there are several aliases to define:

#!/bin/sh

# A little magic that will show you the way:
while IFS='' read -r cmd
do
# define a dummy alias
alias "unamed=$cmd"
# then show it
alias unamed
done <<'EOF'
squeue -u as1056 --format="%.18i %.36j %.8u %.2t %.10M %.10L %.6D %.6C %.12P %R"
sq | awk '$4 == "R" {print $0}'
EOF

now you can copy paste the output into your .bashrc and set a name for your aliased commands

notes:

  • <<'EOF' ... EOF is a quoted here-document; It will feed the stdin of a command (the while loop) with its content. When quoted it means that the shell won't expand anything inside of it.

  • The read command reads a line from stdin (feeded with the here-document). The IFS='' and the -r option are there to prevent read from modifying the line.

Printing variable with double quotes in AWK

Change the print statement from

print iface "," if_arr[1] "," mac

to

print "\"" iface "\",\"" if_arr[1] "\",\"" mac "\""

Using double quotes in awk

If the awk command is single quoted, the $2 is not interpreted by the shell, but is instead passed as the literal string $2 to awk. awk will then print the second space delimited token in the input string, which in this case is a.

echo "line1 a b c" | awk '{ print $2 }' # prints the second space-delimited token
> a

If the awk command is double quoted, the $2 is interpreted by the shell. Because $2 is empty (in this case), the empty string is substituted. awk sees a command which looks like awk "{ print }", which is an instruction to print everything.

echo "line1 a b c" | awk '{ print }' # prints all input
> line1 a b c

It is also possible to use double qotes, and escape the $, which will cause the $ to not be interpreted by the shell, and instead the $2 string will be passed to awk.

echo "line1 a b c" | awk "{ print \$2 }" # prints the second space-delimited token
> a

How to add double quotes to a line with SED or AWK?

Your input file has carriage returns at the end of the lines. You need to use dos2unix on the file to remove them. Or you can do this:

sed 's/\(.*\)\r/"\1"/g'

which will remove the carriage return and add the quotes.

Add double quotes in .CSV comma delimited file using awk

If your data is really that simple with no embedded quotes or newlines or anything then all you need is:

$ awk -F"'?,'?" -v OFS='","' '{$1=$1; gsub(/^.|$/,"\"")} 1' file
"2016-03-12","12393659","134","","35533605","189348","9798","gmail.com;live_com.com"
"2016-03-12","12390103","138","","35438006","5133","1897","google.com"
"2016-03-12","45616164","139","","01318800","10945593","596633","facebook.com;tumblr.com;t.co"
"2016-03-12","45673436","38","","86441702","4350985","150327","serving-sys.com;chartboost.com;admarvel.com;mydas.mobi;adap.tv;cloudfront.net"

awk to add single quotes around particular column and concat with other column

I think you want this:

echo "1|2|3|4|5|6|7|8|9" | awk -F'|' '{print "\047" $6 "\047" $9}'
'6'9

Or, with a variable q to hold the quote:

echo "1|2|3|4|5|6|7|8|9" | awk -F'|' '{q="\047"; print q $6 q $9}'

Or, if you prefer hex:

echo "1|2|3|4|5|6|7|8|9" | awk -F'|' '{q="\x27"; print q $6 q $9}'

Or, you could pass the quote in as a variable:

echo "1|2|3|4|5|6|7|8|9" | awk -F'|' -v q="'" '{print q $6 q $9}'

Or, slightly shorter:

echo "1|2|3|4|5|6|7|8|9" | awk -F'|' -v q=\' '{print q $6 q $9}'


Related Topics



Leave a reply



Submit