This is probably a silly question but I'm a beginner. Currently having trouble extracting fields from a csv file. Ideally, I want to print it out with li tags with the third column printing in parenthesis. The closest I've gotten is using

awk -F'\t' '{print "\"<li>$2($3)</li>\""}' OFS='","' datafile.csv

which only results in "<li>$2($3)</li>" repeatedly without extracting the data.

Using awk -F'\t' '{print "\<li>$2,($3)</li>\"}' OFS='","' datafile.csv only results in a non-terminating string error.

If I use:

awk '{print $1","$2","$4}' datafile.csv

it prints nearly everything with commas instead of spaces

If I use:

awk '{print $2","$3"}' datafile.csv

I get:

awk: non-terminated string ,}... at source line 1
 context is
    {print >>>  $2","$3"} 

I'm not sure what the issue is. I've also tried using cut with no success.

  • Regarding awk: non-terminated string ,}... at source line 1 - note the lone, mismatched " at the end of your code print $2","$3" which the error message is pointing you to look at - context is {print >>> $2","$3"}.
    – Ed Morton
    Commented Sep 14, 2022 at 17:01
  • edit your question to include concise, testable sample input and expected output (no links or images, just plain text) so we can help you further.
    – Ed Morton
    Commented Sep 14, 2022 at 17:05

3 Answers 3


You seem to want to print <li>$2($3)</li>, where $2 and $3 are the 2nd and 3rd tab-delimited field form your TSV file.

You can do that in many different ways, but the two most obvious ones would be either

print "<li>" $2 "(" $3 ")</li>"


printf "<li>%s(%s)</li>\n", $2, $3

Note that neither of the two variations includes quoting $2 or $3, as doing so would output the literal strings $2 and $3 and not what they refer to. This is the issue in your first piece of awk code. The other ones suffer from unbalanced quotes.

If you want double quotes around the string, then use either

print "\"<li>" $2 "(" $3 ")</li>\""


printf "\"<li>%s(%s)</li>\"\n", $2, $3

As you've discovered, awk does not substitute the $i construct inside of a double-quoted string.

echo foo bar baz | awk '{print "$1:$2"}'

awk does string concatenation by simply placing the strings side-by-side.

echo foo bar baz | awk '{print $1 ":" $2}'

I see you setting OFS but not taking advantage of it: when printing, a comma-separated list of strings will be joined using OFS

echo foo bar baz | awk -v OFS=":" '{print $1, $2}'

Your issue seems to be with the \" you are using.

  1. Once you start a quoted string with" it goes to the next (unquoted) ". A \" inserted inside the quoted string will not terminate that qouted string but will be inserted "as is", i. e.: a double quote character.

  2. Inside a quoted string the $n values are not expanded.

$ echo "one two three" | awk '{print "$2,$3"}'

$ echo "one two three" | awk '{print "\"$2,$3\""}'

$ echo "one two three" | awk '{print "\"",$2,$3,"\""}'   # concatenated quotes
" two three "

$ echo "one two three" | awk '{print "\"" $2 $3 "\""}'   # only one string

$ echo "one two three" | awk '{print "\""$2,$3"\""}'     # two strings
"two three"

$ echo "one two three" | awk '{print "\""$2"\"","\""$3"\""}'  # Using OFS
"two" "three"

$ echo "one two three" | awk -vOFS="," '{print "\""$2"\"","\""$3"\""}' # Changing OFS

Those are some possible uses of the \" string inside awk.

So, you probably want this:

awk -F'\t' '{print "\"<li>" $2 "(" $3 ")</li>\""}' OFS='","' datafile.csv

You must log in to answer this question.

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