For Programmers: Free Programming Magazines  


Home > Archive > AWK > January 2005 > Diff between '{..}' and {'..'}









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Diff between '{..}' and {'..'}
Anil

2005-01-15, 3:55 am

Hi,

Could anyone tell me the difference between

......| awk '{.......}'

AND

......| awk {'......'}


And also when(in what situation) these are used?
Thanks in advance,
Anil.

Jürgen Kahrs

2005-01-15, 8:55 am

Anil wrote:
> Thanks for the reply Stephane.
>
> Similarly in what situations shud I use
>
> awk '{........}' < filename


Here the shell reads the file and presents
it to standard input of the AWK interpreter.
The file name is unknown to AWK.

> AND
>
> awk '{.........}' filename


Here it is the AWK interpreter which
opens the file and reads the data.
The FILENAME variable can be read in
this case.

Last w someone pointed out that the
second variant should be preferred over
the first one but I cant remember the
reason.
Stephane CHAZELAS

2005-01-15, 8:55 am

2005-01-12, 22:37(-08), Anil:
> Hi,
>
> Could anyone tell me the difference between
>
> .....| awk '{.......}'
>
> AND
>
> .....| awk {'......'}
>
>
> And also when(in what situation) these are used?
> Thanks in advance,

[...]

The second one should never be used.

The difference is at the shell level, not at the awk level.
'...' are strong shell quotes, which means that the text inside
single quotes is passed literally as one single argument to awk.

If you put "{" and "}" outside the quotes, they may be treated
specially depending on the shell.

For instance the bash, zsh and some versions of ksh shells (and
csh-like shells, but those have a different syntax), use {...}
to perform what is called brace expansion.

cmd foo{bar,baz}

is expanded into 3 arguments: "cmd", "foobar", "foobaz"

So in

awk {'print "''foo,bar''"'}

awk would take three arguments: <awk>, <print "'foo> and <bar'">
which means run the <print "'foo> code on the file named
<bar'">, while in

awk '{print "''foo,bar''"}'

awk would take two arguments: <awk> and <{print "'foo,bar'"}>
which means run the <{print "'foo,bar'"}> code on stdin.



awk {'print "foo"'}

would be the same as

awk '{print "foo"}'

(except for csh-like shells) as there's no unquoted commas
inbetween the braces (so, it's no candidate for brace
expansion).

--
Stephane
Anil

2005-01-19, 3:56 am

Thanks for the reply Stephane.

Similarly in what situations shud I use

awk '{........}' < filename
AND

awk '{.........}' filename

(The "<" sign before filename)

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com