You are not logged in.

#1 2019-05-14 21:52:52

Plup
Member
Registered: 2019-05-14
Posts: 4

Awk can't execute the following command with echo [SOLVED]

I'm trying to run this command and put an apostrophe at the beginning:

echo test | awk '{ system("echo '"'"' " $var) }'

However

sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file

But I wanted:

'test

For example, when i run this command:

echo test | awk '{ system("echo '"e"' " $var) }'

Results:

e test

How can i add a single quote at beggining?

Last edited by Plup (2019-05-14 23:42:11)

Offline

#2 2019-05-14 22:21:57

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,442
Website

Re: Awk can't execute the following command with echo [SOLVED]

echo test | awk '{ printf "\047"; system("echo " $var); }'

But why are you using system("echo ...") in the first place?

echo test | awk '{ printf "\047%s\n", $var; }'

Last edited by Trilby (2019-05-14 22:22:59)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3 2019-05-14 22:42:51

Plup
Member
Registered: 2019-05-14
Posts: 4

Re: Awk can't execute the following command with echo [SOLVED]

I'm a very beginner on script, so it's hard for me to say what could  be put in the place of echo and what should be batter. Echo was the closest command that came into my mind.

I traied with printf  and tried again with awk, but echo was the closest successful command

Offline

#4 2019-05-14 22:45:04

Plup
Member
Registered: 2019-05-14
Posts: 4

Re: Awk can't execute the following command with echo [SOLVED]

It worked! Thank you, Trilby!

echo test | awk '{ printf "\047%s\n", $var; }'

Offline

#5 2019-05-14 23:04:38

2ManyDogs
Forum Moderator
Registered: 2012-01-15
Posts: 4,645

Re: Awk can't execute the following command with echo [SOLVED]

Please remember to mark your thread [SOLVED] (edit the title of your first post).


How to post. A sincere effort to use modest and proper language and grammar is a sign of respect toward the community.

Offline

#6 2019-05-14 23:11:24

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,442
Website

Re: Awk can't execute the following command with echo [SOLVED]

Also note that '$var' is really just nonsense there that only coincidentally does what you want.  There is no variable 'var', but as there is no variable with that name it expands to 0 which then makes that $0 or a reference to the whole line from the stdin.  It'd be much more readable (and less fragile) to just explicitly reference $0.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#7 2019-05-14 23:41:24

Plup
Member
Registered: 2019-05-14
Posts: 4

Re: Awk can't execute the following command with echo [SOLVED]

Trilby wrote:

Also note that '$var' is really just nonsense there that only coincidentally does what you want.  There is no variable 'var', but as there is no variable with that name it expands to 0 which then makes that $0 or a reference to the whole line from the stdin.  It'd be much more readable (and less fragile) to just explicitly reference $0.

This is true, thank you again!

Offline

Board footer

Powered by FluxBB