LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-15-2013, 01:55 PM   #1
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Weird behaviour of a simple awk program


I have a question about awk. I made a simple one-liner to print out multiple lines in a file as a colon separated list of items. Input file:
Code:
123634.netserver
123635.netserver
123647.netserver
Code:
$ awk '{j ? j=j":"$0 : j=$0} END{print j}' file
123634.netserver:123635.netserver:123647.netserver
This works flawlessly, but if I change the variable name to _ it doesn't print the entire list but only the last item preceded by a colon:
Code:
$ awk '{_ ? _=_":"$0 : _=$0}END{print _}' file
:123647.netserver
To avoid this I have to put a space before the first double quotes:
Code:
$ awk '{_ ? _=_ ":"$0 : _=$0}END{print _}' file
123634.netserver:123635.netserver:123647.netserver
or add the --posix option:
Code:
$ awk --posix '{_ ? _=_":"$0 : _=$0}END{print _}' file
123634.netserver:123635.netserver:123647.netserver
I cannot explain the reason of this behaviour and why the underscore triggers it if not in posix compatibility mode. I tried this with awk 3.1.7 and awk 4.0.1 on different machines and the result is always the same. Why?
 
Old 10-15-2013, 02:07 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
13.3 Internationalizing awk Programs:

Quote:
_"your message here"
String constants marked with a leading underscore are candidates for translation at runtime. String constants without a leading underscore are not translated.
 
2 members found this post helpful.
Old 10-15-2013, 02:09 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Original Poster
Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Well, I think I have the solution after having read a part of the GNU awk manual I've always ignored in the past. From chapter 13.4 (Translating awk Programs) it looks like the underscore is used as a marker for strings that has to be translated in other languages. Something that the strict POSIX standard doesn't allow. Hence the string
Code:
_":"
is a string marked for translation whereas with the blank space in the middle
Code:
_ ":"
is just the concatenation between the content of the variable _ and the string ":", exactly as it's supposed to be.
 
Old 10-15-2013, 02:10 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Original Poster
Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Thanks ntubski! There is always something new to learn about GNU awk!
 
Old 10-16-2013, 07:06 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I realise the original question was about the unusual behaviour of _, but I just thought I would mention the alternative to provide the same output
Code:
awk '$1=$1' RS="" FS="\n" OFS=":" file
 
1 members found this post helpful.
Old 10-16-2013, 07:20 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Original Poster
Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Nice one, grail! Thanks
 
Old 10-16-2013, 08:39 AM   #7
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
With this InFile ...
Code:
123634.netserver
123635.netserver
123647.netserver
... this paste ...
Code:
paste -sd: $InFile >$OutFile
... produced this OutFile ...
Code:
123634.netserver:123635.netserver:123647.netserver
Fewer keystrokes and (IMHO) better readability than other solutions.

Daniel B. Martin
 
1 members found this post helpful.
Old 10-16-2013, 10:36 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Original Poster
Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Thanks Daniel! I have multiple choices at this point!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Weird behaviour jim.thornton Linux - Newbie 10 10-22-2012 09:30 PM
[SOLVED] Weird awk behaviour with NOT regexp switch sarenace Programming 7 05-18-2012 12:54 AM
Weird behaviour when trying to connect to LQ bigjohn LQ Suggestions & Feedback 2 03-11-2006 10:27 AM
9.3 weird behaviour ????? bigjohn SUSE / openSUSE 9 07-06-2005 05:44 AM
[SOLVED] k3b weird behaviour filosophem Linux - Software 3 01-23-2005 04:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration