Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

what operation is $1.$2 ?

37 views
Skip to first unread message

Ralf Fassel

unread,
Mar 9, 2022, 2:29:25 PM3/9/22
to
GNU Awk 4.2.1, API: 2.0

I wanted to run

awk '{print $1 "." $2}'

i.e. add a dot between $1 and $2, but accidentially typed

awk '{print $1.$2}'

which simply concatenates $1$2:

echo 1 2 | awk '{print $1.$2}'
12

I wonder what operation is $1.$2, or why is awk not bailing out with a
syntax error on this, like in:

echo 1 2 | awk '{print $1 . $2}'
awk: cmd. line:1: {print $1 . $2}
awk: cmd. line:1: ^ syntax error


TNX
R'

Neil

unread,
Mar 9, 2022, 2:49:01 PM3/9/22
to
I think $1. is the same as $1 (which is also the same as $1.234 ).

Janis Papanagnou

unread,
Mar 9, 2022, 4:02:59 PM3/9/22
to
On 09.03.2022 20:29, Ralf Fassel wrote:
> GNU Awk 4.2.1, API: 2.0
>
> I wanted to run
>
> awk '{print $1 "." $2}'
>
> i.e. add a dot between $1 and $2, but accidentially typed
>
> awk '{print $1.$2}'
>
> which simply concatenates $1$2:
>
> echo 1 2 | awk '{print $1.$2}'
> 12
>
> I wonder what operation is $1.$2,

It's parsed as four components: $ 1. $ 2
i.e. two field selector operators ($) and two numbers (1. and 2).

Janis

J Naman

unread,
Mar 9, 2022, 8:25:07 PM3/9/22
to
Try echo 3 4 | gawk "{print $1.$2}" => 34 ( no 1's or 2's)
Try echo 3 4 | gawk "{print $1xyz$2}" => 34 (no xyz either)
echo 3 4 | gawk "{print $1.xyz$2}"=> 34 (no xyz either)
echo 3 4 | gawk "{print $1..$2}" two or more decimals gets syntax error
See: converting string to numeric ignores alpha, but not 1.., that's syntax
echo 123usd 456euro | gawk "{print $1+$2}" => 576
ref: UsrGuide 6.1.4 Conversion of Strings and Numbers

Janis Papanagnou

unread,
Mar 9, 2022, 11:51:32 PM3/9/22
to
On 10.03.2022 02:25, J Naman wrote:
> On Wednesday, 9 March 2022 at 16:02:59 UTC-5, Janis Papanagnou wrote:
>> On 09.03.2022 20:29, Ralf Fassel wrote:
>>> GNU Awk 4.2.1, API: 2.0
>>>
>>> I wanted to run
>>>
>>> awk '{print $1 "." $2}'
>>>
>>> i.e. add a dot between $1 and $2, but accidentially typed
>>>
>>> awk '{print $1.$2}'
>>>
>>> which simply concatenates $1$2:
>>>
>>> echo 1 2 | awk '{print $1.$2}'
>>> 12
>>>
>>> I wonder what operation is $1.$2,
>> It's parsed as four components: $ 1. $ 2
>> i.e. two field selector operators ($) and two numbers (1. and 2).
>>
>> Janis
>>> or why is awk not bailing out with a
>>> syntax error on this, like in:
>>>
>>> echo 1 2 | awk '{print $1 . $2}'
>>> awk: cmd. line:1: {print $1 . $2}
>>> awk: cmd. line:1: ^ syntax error
>>>
>>>
>>> TNX
>>> R'
>>>

Which OS and shell did you use to execute the subsequent commands?

On Unix systems the awk program argument should be quoted by single
quotes

awk '...'

otherwise $var and $0 are expanded by the shell as shell variables
and awk sees those instead of the passed in parameters.


> Try echo 3 4 | gawk "{print $1.$2}" => 34 ( no 1's or 2's)

Of course no 1s or 2s, because $1. identifies the 1st field and $2
identifies the second field.

> Try echo 3 4 | gawk "{print $1xyz$2}" => 34 (no xyz either)

Of course no xyz, because you concatenate field one, variable xyz,
and field 2, and variable xyz is undefined and thus empty. Define
that variable and you'll see it

$ echo 3 4 | gawk -v xyz=hoho '{print $1xyz$2}'
3hoho4

> echo 3 4 | gawk "{print $1.xyz$2}"=> 34 (no xyz either)

Of course not. Same reason like before.

> echo 3 4 | gawk "{print $1..$2}" two or more decimals gets syntax error

Of course. While 1 and 1. are syntactic correct numbers, 1.. is not.

> See: converting string to numeric ignores alpha, but not 1.., that's syntax
> echo 123usd 456euro | gawk "{print $1+$2}" => 576

Hardly correct. It must result in 579.

The context is arithmetic so from the input fields only the numbers
are taken.

> ref: UsrGuide 6.1.4 Conversion of Strings and Numbers

And what was the point of your post as a reply to our posts?

Janis

Ralf Fassel

unread,
Mar 10, 2022, 4:50:47 AM3/10/22
to
* Janis Papanagnou <janis_pa...@hotmail.com>
| > I wonder what operation is $1.$2,
>
| It's parsed as four components: $ 1. $ 2
| i.e. two field selector operators ($) and two numbers (1. and 2).

Ouch (i.e. I understand :-)

echo 1st 2nd | awk '{print $1.9}'
1st

echo 1st 2nd | awk '{print $1.9999999999999999999999999999999999}'
2nd

Thanks for the enlightment!
R'

Kpop 2GM

unread,
Mar 28, 2022, 2:53:10 PM3/28/22
to
i don't think it's an operation per se

i *think* awk sees your thing as

echo 1 2 | awk '{print $1.$2}'

——> awk '{print ($(1.))($2) }'
——> awk '{print ($(1.0))($2) }'
——> awk '{print ($1)($2) }'

which is a delimiter-less string concat. I'm only guessing here

The 4Chan Teller
0 new messages