Running call execute_command line with a command that has " " in it

58 views
Skip to first unread message

Pat

unread,
Jul 27, 2022, 1:26:46 PMJul 27
to
I have a Fortran 90 program that needs to run a bash command in it for text file manipulation.

Unfortunately the compiler gets confused between the "" for the command and the "" for the bash command

Example:
call execute_command_line("cat test.txt | awk '{$1=$NF=""; print $0}' > output.txt")

How do I fix this?

Thanks.

Peter Klausler US

unread,
Jul 27, 2022, 1:36:43 PMJul 27
to
Replace the two double quotes within the character literal constant with four double quotes: $NF=""""

Pat

unread,
Jul 27, 2022, 1:40:35 PMJul 27
to
Thanks, it works :)

gah4

unread,
Jul 27, 2022, 1:45:16 PMJul 27
to
On Wednesday, July 27, 2022 at 10:36:43 AM UTC-7, Peter Klausler US wrote:

(snip)

> Replace the two double quotes within the character literal constant with four double quotes: $NF=""""

Unix is pretty consistent on its quoting conventions, unlike another popular OS.

You have to double quotes as they are seen by the shell, and the double them
as they are seen by awk. Tradition is to run Bourne shell, but otherwise there is still
a shell between you and awk, to process quotes.

If you are using CMD.EXE as your shell, I am much less sure how to do it.

gah4

unread,
Jul 27, 2022, 2:07:46 PMJul 27
to
On Wednesday, July 27, 2022 at 10:26:46 AM UTC-7, Pat wrote:

(snip)

> call execute_command_line("cat test.txt | awk '{$1=$NF=""; print $0}' > output.txt")

The other complication, which it seems you already figured out,
is that the shell will expand $ variables inside double quotes, but not
single quotes. Combined with awk's use of $, that can be tricky.


dave_th...@comcast.net

unread,
Aug 18, 2022, 1:29:18 AMAug 18
to
Or more sneakily use the fact that an awk variable (not field) is
implictly initalized to the empty string, plus the default operand for
print is $0 and awk reads files just as well as cat:

...( "awk '{$1=$NF=empty;print}' test.txt >output.txt" )

Reply all
Reply to author
Forward
0 new messages