When I am using awk command to insert a line it is giving unterminated string error.

input file is like

date      mean       rms       bias                                       
.....    .....       ....      .......                                         
.....       .....        ........           .........                         
......        ........  .......           .......

Here I need to insert new values of date, mean, rms and bias.

my script is like this

echo $PDY $mean $rms  $bias                                      
awk '/Date/ { print; print "'$PDY' \t'$mean' \t'$rms' \t'$bias'"; next }1' file.txt

Terminal log is like this

+ echo 20180131 76.196 578.177 903.000                        
20180131 76.196 578.177 903.000
+ awk '/Date/ { print; print "20180131 \t' '76.196 \t578.177 \t903.000"; next }1' file.txt                                                      
awk: /Date/ { print; print "20180131 \t
awk:                       ^ unterminated string

There is a ' ' (gap/space) coming after print; print "20180131 \t in my awk command. I don't know why it is coming

Please give me a solution.

2 Answers 2


Your string starts with ' and ends with the second ':

awk '/Date/ { print; print "20180131 \t'

The unterminated string from error message is "20180131 \t.

Instead, use " and escape the inner " with \":

awk "/Date/ { print; print \"20180131 \t' '76.196 \t578.177 \t903.000\"; next }1" file.txt

The error message means that awk doesn't like the way you are using quotation marks. Glancing at your awk statement, I would point first to your frequent use of single-quotation marks within your print statement. awk is interpreting the single-quote after \t as marking the end of your entire awk program (that you began with a single-quote after the keyword awk), and at that point there was no closing double-quote for your print command (nor a close-brace for your awk statement).

The easier way to pass variables into an awk program is to use the command-line argument -V, eg. awk -V pdy="$PDY" -V mean="$mean", and then usepdyandmeanas nativeawk` variables. With this method, there will be no need for most of your quoting.

You must log in to answer this question.

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