How to solve awk: line 1: runaway string constant error?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to solve awk: line 1: runaway string constant error?
# 1  
Old 03-21-2013
How to solve awk: line 1: runaway string constant error?

Hi All !

I am just trying to print bash variable in awk statement as string
here is my script

Code:
n=1
for file in `ls *.tk |sort -t"-" -k2n,2`; do
ak=`(awk 'FNR=='$n'{print $0}' res.dat)`

awk '{print "'$ak'",$0}' OFS="\t" $file
n=$((n+1))
unset ak
done

I am getting following error

Code:
awk: line 1: runaway string constant

is there any solution to read reference file first line print with 1st file(in loop) $0, then read 2nd line from reference file print with 2nd file in loop
# 2  
Old 03-21-2013
It's not a good practice to use shell variables directly in awk. Pass the shell variables to awk using -v.

Check this example here(run away string problem)..

Guru.
# 3  
Old 03-21-2013
Why is it normally not a good idea to use shell vars in awk? Is it because of the single quotes hiding the variable? Or some other reason?
# 4  
Old 03-21-2013
Its mainly because of the quoting issues present in a shell variable. Refer this link for more: Using Shell Variables - The GNU Awk User's Guide

Guru.
# 5  
Old 03-21-2013
Thanks.
# 6  
Old 03-21-2013
Code:
awk 'NR=='"$n"' { print  }' filename

# 7  
Old 03-22-2013
Hi,

Can you please help me to find/display out last Friday's date of the month using awk command in Unix/Linux.

i have found this command and its work

cal 03 2013 | awk 'NF>5{last = $6} END{print last}'

But it show the output in single digit like :29

But i need the output will shown in this format (Fri Mar 29 2013)

Please help.
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies

2. Programming

String Constant C

I wonder string constant exists permanently or temporary. For example, printf("hello, world"); the function printf access to it is through a pointer. Does it mean storage is allocated for the string constant to exist permanently in memory? :confused: (4 Replies)
Discussion started by: kris26
4 Replies

3. Shell Programming and Scripting

Trouble appending string constant to variable

Hi. I define my variables as: month=jul DD=17 YEAR=2012 transmission_file_name_only=test_$month$DD$YEAR_partial.dat However when I run my script the variable 'transmission_file_name_only' resolves to: File "/downloads/test_jul17.dat" not found. How can I append this '_partial'... (3 Replies)
Discussion started by: buechler66
3 Replies

4. Solaris

Line too long error Replace string with new line line character

I get a file which has all its content in a single row. The file contains xml data containing 3000 records, but all in a single row, making it difficult for Unix to Process the file. I decided to insert a new line character at all occurrences of a particular string in this file (say replacing... (4 Replies)
Discussion started by: ducati
4 Replies

5. Shell Programming and Scripting

choose random text between constant string.. using awk?

Hallo I have maybe a little bit advanced request.... I need to choose one random part betwen %.... so i have this.. % text1 text1 text1 text1 text1 text1 text1 text1 text1 % text2 text2 text2 text2 text2 % text3 text3 text3 tetx3 % this choose text between % awk ' /%/... (8 Replies)
Discussion started by: sandwich
8 Replies

6. Shell Programming and Scripting

Runaway String Problem

Database.txt John:30:40 echo -n "New Title Please :" read NewTitle awk -F":" 'OFS = ":"{ $1 = "'$NewTitle'" ; print $0 } ' Database.txt> Database2.txt mv Database2.txt Database.txt what this does, is that when i input something into $NewTitle, it will update $1 which is "John" into... (3 Replies)
Discussion started by: gregarion
3 Replies

7. Programming

'strlen' of a constant string

In a declaration, I have: const char comment_begin = "<!--"; const char comment_end = "-->"; const int comment_begin_len = strlen(comment_begin); const int comment_end_len = strlen(comment_end); When I compile, I get the warnings: emhttpc.c:64: warning: initializer element is not... (10 Replies)
Discussion started by: cleopard
10 Replies
Login or Register to Ask a Question