corymathews
corymathews

Reputation: 12619

C system() call fails with error "unterminated string"

I have a small piece of c code which should run an awk command on my linux machine. However for the life of me it will not exec. The awk works if I directly run it in the terminal.

My current failed command

system("awk '{ printf \"%d \n\", $12 }' results.dat | sort -n");

It fails with

awk: { printf "%d 
awk:          ^ unterminated string

How else do you escape the double quotes so that the command will run? Also why does this fail, but when I replace the system call with a printf it will print?

Upvotes: 2

Views: 975

Answers (1)

Daniel LeCheminant
Daniel LeCheminant

Reputation: 51071

Perhaps you should escape the \n again, as in

system("awk '{ printf \"%d \\n\", $12 }' results.dat | sort -n");
//                         ^ note the extra \

as I think the \n is meant to be part of the printf.

Your current construction calls system with an argument of

awk '{ printf "%d 
", $12 }' results.dat | sort -n

Upvotes: 8

Related Questions