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

AWK Do not print the whole String

88 views
Skip to first unread message

Abdou B

unread,
Mar 23, 2022, 7:04:58 AM3/23/22
to
Hello Guys,

I would like to concatenate all the line of a file in a String.
So I created this awk command :

gawk 'BEGIN{texte=""}{texte=(texte ", " $0); print length(texte);}END{print " -- "texte "--"; print length(texte)}' listvip.properties

But I have a strange result.
It seems AWK concatenate the strings in one.
But it does not display the correct value, AWK retrun only the last line.
But when I used length on the variable texte, I see the actual lentght is greater that what has been display.

Any Solution ?

Best Regards
Abdou

Janis Papanagnou

unread,
Mar 23, 2022, 7:44:57 AM3/23/22
to
It is not clear what happens in your environment, you didn't tell us
anything about it.

What do you see if you redirect the output into a file and use a text
editor to inspect the contents?

gawk '{ texte = texte ", " $0; print length(texte) }
END { print " -- "texte "--"; print length(texte) }
' listvip.properties >output


Janis

>
> Best Regards
> Abdou
>

Ben Bacarisse

unread,
Mar 23, 2022, 7:49:27 AM3/23/22
to
I suspect the input has \r\n line separators and awk is splitting on \n.
I.e. you are getting the output you want, but the display is overwriting
earlier text with later text.

Try setting RS="\r\n" in the BEGIN block.

But, if all you want is to concatenate the input lines without having to
store them just by printing them without a newline: printf(", %s", $0);
and if you don't want the comma at the start:

gawk 'BEGIN{RS="\r\n"} {printf("%s%s", NR > 1? ", " : "", $0)} END{print ""}'

--
Ben.

Manuel Collado

unread,
Mar 23, 2022, 8:49:43 AM3/23/22
to
El 23/03/2022 a las 12:04, Abdou B escribió:
> Hello Guys,
>
> I would like to concatenate all the line of a file in a String.
> So I created this awk command :
>
> gawk 'BEGIN{texte=""}{texte=(texte ", " $0); print length(texte);}END{print " -- "texte "--"; print length(texte)}' listvip.properties
>
> But I have a strange result.

It works OK in my system. No problem.

> It seems AWK concatenate the strings in one.
> But it does not display the correct value, AWK retrun only the last line.

If lines are terminated by CR+LF and the CR char is kept as the final
char of each line, then what is printed en the screen will have the text
lines overprinted.

> But when I used length on the variable texte, I see the actual lentght is greater that what has been display.

A LOCALE problem? Please test:

LC_ALL=C gawk .....

And see if it makes a difference.

>
> Any Solution ?
>
> Best Regards
> Abdou
>

--
Manuel Collado - http://mcollado.z15.es

Abdou B

unread,
Mar 23, 2022, 9:44:03 AM3/23/22
to
The dos2unix on my files made it work
Many Thanks to all !

Kenny McCormack

unread,
Mar 23, 2022, 9:53:59 AM3/23/22
to
In article <eca857df-59c0-4003...@googlegroups.com>,
Abdou B <abdou.b...@gmail.com> wrote:
>The dos2unix on my files made it work
>Many Thanks to all !

You might also check out a thread of mine on this newsgroup that addresses
this issue. Look for a thread with Subject something like:

Handling DOS (Windows) text files in Unix (Linux) - a nifty extension lib.

Just read the original post; you can ignore all the bozo commentary from
some of the, er, lesser able, members of this group.

--
I am not a troll.
Rick C. Hodgin
I am not a crook.
Rick M. Nixon

Kpop 2GM

unread,
Mar 28, 2022, 2:47:20 PM3/28/22
to
if you wanna just chain everything onto one line, you don't even need to use print statements, or any statements for that matter :

version 1 :

[g/m/n]awk 'NF-=!$NF' FS='\n\r?|\r\n?' OFS=',' RS='^$'

(if you're very sure the final row isn't +/- 0, and also no empty lines in between)

version 2 :

[g/m/n]awk 'NF-=$NF!~"."' FS='[\n\r]+' OFS=',' RS='^$'

version 3 :

[ g/m]awk 'NF-=$_~FS"$"' FS='[\n\r]+' OFS=',' RS='^$'
nawk 'NF-=$-_~FS"$"' FS='[\n\r]+' OFS=',' RS='^$'

(more verbose but safer approach. this version also squeezes empty lines)

Tested on gawk, mawk134, mawk1.996, and nawk; i
includes tail-excess-delimiter auto-trim

The 4Chan Teller

0 new messages