• AWK Do not print the whole String

    From Abdou B@abdou.bourakba@gmail.com to comp.lang.awk on Wed Mar 23 04:04:56 2022
    From Newsgroup: comp.lang.awk

    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
    --- Synchronet 3.19c-Linux NewsLink 1.113
  • From Janis Papanagnou@janis_papanagnou@hotmail.com to comp.lang.awk on Wed Mar 23 12:44:55 2022
    From Newsgroup: comp.lang.awk

    On 23.03.2022 12:04, Abdou B wrote:
    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 ?

    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


    --- Synchronet 3.19c-Linux NewsLink 1.113
  • From Ben Bacarisse@ben.usenet@bsb.me.uk to comp.lang.awk on Wed Mar 23 11:49:14 2022
    From Newsgroup: comp.lang.awk

    Abdou B <abdou.bourakba@gmail.com> writes:

    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.

    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.
    --- Synchronet 3.19c-Linux NewsLink 1.113
  • From Manuel Collado@m-collado@users.sourceforge.net to comp.lang.awk on Wed Mar 23 13:49:42 2022
    From Newsgroup: comp.lang.awk

    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
    --- Synchronet 3.19c-Linux NewsLink 1.113
  • From Abdou B@abdou.bourakba@gmail.com to comp.lang.awk on Wed Mar 23 06:44:01 2022
    From Newsgroup: comp.lang.awk

    The dos2unix on my files made it work
    Many Thanks to all !
    Le mercredi 23 mars 2022 à 13:49:43 UTC+1, Manuel Collado a écrit :
    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
    --- Synchronet 3.19c-Linux NewsLink 1.113
  • From gazelle@gazelle@shell.xmission.com (Kenny McCormack) to comp.lang.awk on Wed Mar 23 13:53:57 2022
    From Newsgroup: comp.lang.awk

    In article <eca857df-59c0-4003-a81c-fcd284bfcb0bn@googlegroups.com>,
    Abdou B <abdou.bourakba@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
    --- Synchronet 3.19c-Linux NewsLink 1.113
  • From Kpop 2GM@jason.cy.kwan@gmail.com to comp.lang.awk on Mon Mar 28 11:47:19 2022
    From Newsgroup: comp.lang.awk

    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

    --- Synchronet 3.19c-Linux NewsLink 1.113