Print fields starting with N to the end of line. - awk

This is a discussion on Print fields starting with N to the end of line. - awk ; Hello, seems that I'm missing something obvious. There is file with variable number of field, is there any simple way in awk to print fields starting with field N up to the end of line. e.g. $ cat test.txt a ...

+ Reply to Thread
Page 1 of 3 1 2 3 LastLast
Results 1 to 10 of 24

Print fields starting with N to the end of line.

  1. Default Print fields starting with N to the end of line.

    Hello, seems that I'm missing something obvious. There is file with
    variable number of field, is there any simple way in awk to print
    fields starting with field N up to the end of line. e.g.

    $ cat test.txt
    a b c
    a b c d
    a b c d e f
    a b c d v e

    How to print
    $ awk <???> test.txt

    d
    d e f
    d v e

    Of course this is simplification of real data I have so I definitely
    need awk to parse it Thank you very much in advance.

  2. Default Re: Print fields starting with N to the end of line.



    On 3/30/2008 1:12 PM, Peter wrote:
    > Hello, seems that I'm missing something obvious. There is file with
    > variable number of field, is there any simple way in awk to print
    > fields starting with field N up to the end of line. e.g.
    >
    > $ cat test.txt
    > a b c
    > a b c d
    > a b c d e f
    > a b c d v e
    >
    > How to print
    > $ awk <???> test.txt
    >
    > d
    > d e f
    > d v e
    >
    > Of course this is simplification of real data I have so I definitely
    > need awk to parse it Thank you very much in advance.


    If you don't care about white space before/between fields:

    awk '{$1=$2=$3=""}1' file

    If you do care, with a POSIX awk:

    awk '{sub(/^[[:space:]]*([^[:space:]]*[[:space:]]*){3}/,"")}1'

    and with GNU awk:

    gawk --re-interval '{sub(/^[[:space:]]*([^[:space:]]*[[:space:]]*){3}/,"")}1'

    Ed.


  3. Default Re: Print fields starting with N to the end of line.

    Peter wrote:

    > Hello, seems that I'm missing something obvious. There is file with
    > variable number of field, is there any simple way in awk to print
    > fields starting with field N up to the end of line. e.g.
    >
    > $ cat test.txt
    > a b c
    > a b c d
    > a b c d e f
    > a b c d v e
    >
    > How to print
    > $ awk <???> test.txt
    >
    > d
    > d e f
    > d v e
    >
    > Of course this is simplification of real data I have so I definitely
    > need awk to parse it Thank you very much in advance.


    See this thread (comp.unix.shell):

    http://tinyurl.com/2qmonu

    --
    All the commands are tested with bash and GNU tools, so they may use
    nonstandard features. I try to mention when something is nonstandard (if
    I'm aware of that), but I may miss something. Corrections are welcome.

  4. Default Re: Print fields starting with N to the end of line.



    "Ed Morton" <morton@lsupcaemnt.com> wrote in message
    news:47EFDD53.30401@lsupcaemnt.com...
    >
    >
    > On 3/30/2008 1:12 PM, Peter wrote:
    >> Hello, seems that I'm missing something obvious. There is file with
    >> variable number of field, is there any simple way in awk to print
    >> fields starting with field N up to the end of line. e.g.
    >>
    >> $ cat test.txt
    >> a b c
    >> a b c d
    >> a b c d e f
    >> a b c d v e
    >>
    >> How to print
    >> $ awk <???> test.txt
    >>
    >> d
    >> d e f
    >> d v e
    >>
    >> Of course this is simplification of real data I have so I definitely
    >> need awk to parse it Thank you very much in advance.

    >
    > If you don't care about white space before/between fields:
    >
    > awk '{$1=$2=$3=""}1' file
    >
    > If you do care, with a POSIX awk:
    >
    > awk '{sub(/^[[:space:]]*([^[:space:]]*[[:space:]]*){3}/,"")}1'
    >
    > and with GNU awk:
    >
    > gawk --re-interval
    > '{sub(/^[[:space:]]*([^[:space:]]*[[:space:]]*){3}/,"")}1'
    >
    > Ed.
    >


    If number of fields is variable use something like this.
    gawk --re-interval -v ifields=3
    'BEGIN{rexp="^[[:space:]]*([^[:space:]]*[[:space:]]*){" ifields "}"} {
    sub(rexp,"")}1'

    The variable may also be set at runtime.


  5. Default Re: Print fields starting with N to the end of line.

    Peter schreef:
    > Hello, seems that I'm missing something obvious. There is file with
    > variable number of field, is there any simple way in awk to print
    > fields starting with field N up to the end of line. e.g.
    >
    > $ cat test.txt
    > a b c
    > a b c d
    > a b c d e f
    > a b c d v e
    >
    > How to print
    > $ awk <???> test.txt
    >
    > d
    > d e f
    > d v e
    >
    > Of course this is simplification of real data I have so I definitely
    > need awk to parse it Thank you very much in advance.


    $ awk -v nr=4 '{ for (x=nr; x<=NF; x++) {
    printf $x " "; }; print " " }' test.txt


    --
    Luuk

  6. Default Re: Print fields starting with N to the end of line.



    "Luuk" <Luuk@invalid.lan> wrote in message
    news:3ve5c5-p27.ln1@a62-251-88-195.adsl.xs4all.nl...
    > Peter schreef:
    >> Hello, seems that I'm missing something obvious. There is file with
    >> variable number of field, is there any simple way in awk to print
    >> fields starting with field N up to the end of line. e.g.
    >>
    >> $ cat test.txt
    >> a b c
    >> a b c d
    >> a b c d e f
    >> a b c d v e
    >>
    >> How to print
    >> $ awk <???> test.txt
    >>
    >> d
    >> d e f
    >> d v e
    >>
    >> Of course this is simplification of real data I have so I definitely
    >> need awk to parse it Thank you very much in advance.

    >
    > $ awk -v nr=4 '{ for (x=nr; x<=NF; x++) {
    > printf $x " "; }; print " " }' test.txt
    >
    >
    > --
    > Luuk


    One thing that we should note here is we will lose spacing between the
    fields this way.

    Rajan


  7. Default Re: Print fields starting with N to the end of line.

    Rajan schreef:
    >
    >
    > "Luuk" <Luuk@invalid.lan> wrote in message
    > news:3ve5c5-p27.ln1@a62-251-88-195.adsl.xs4all.nl...
    >> Peter schreef:
    >>> Hello, seems that I'm missing something obvious. There is file with
    >>> variable number of field, is there any simple way in awk to print
    >>> fields starting with field N up to the end of line. e.g.
    >>>
    >>> $ cat test.txt
    >>> a b c
    >>> a b c d
    >>> a b c d e f
    >>> a b c d v e
    >>>
    >>> How to print
    >>> $ awk <???> test.txt
    >>>
    >>> d
    >>> d e f
    >>> d v e
    >>>
    >>> Of course this is simplification of real data I have so I definitely
    >>> need awk to parse it Thank you very much in advance.

    >>
    >> $ awk -v nr=4 '{ for (x=nr; x<=NF; x++) {
    >> printf $x " "; }; print " " }' test.txt
    >>
    >>
    >> --
    >> Luuk

    >
    > One thing that we should note here is we will lose spacing between the
    > fields this way.
    >
    > Rajan


    read again, it says:
    printf $x " ";

    so, the output is:
    /tmp # awk -v nr=4 '{ for (x=nr; x<=NF; x++) {
    > printf $x " "; }; print " " }' test.txt


    d
    d e f
    d v e


    --
    Luuk

  8. Default Re: Print fields starting with N to the end of line.

    Luuk wrote:

    >> One thing that we should note here is we will lose spacing between the
    >> fields this way.
    >>
    >> Rajan

    >
    > read again, it says:
    > printf $x " ";
    >
    > so, the output is:
    > /tmp # awk -v nr=4 '{ for (x=nr; x<=NF; x++) {
    > > printf $x " "; }; print " " }' test.txt

    >
    > d
    > d e f
    > d v e


    But if the input is

    a b c d r y
    a s f d o u

    spacing will be lost (though that's not a problem if the OP's input is
    exactly as he showed). Moreover, you are inserting two extra spaces at the
    end of each line.

    --
    All the commands are tested with bash and GNU tools, so they may use
    nonstandard features. I try to mention when something is nonstandard (if
    I'm aware of that), but I may miss something. Corrections are welcome.

  9. Default Re: Print fields starting with N to the end of line.

    pk schreef:
    > Luuk wrote:
    >
    >>> One thing that we should note here is we will lose spacing between the
    >>> fields this way.
    >>>
    >>> Rajan

    >> read again, it says:
    >> printf $x " ";
    >>
    >> so, the output is:
    >> /tmp # awk -v nr=4 '{ for (x=nr; x<=NF; x++) {
    >> > printf $x " "; }; print " " }' test.txt

    >>
    >> d
    >> d e f
    >> d v e

    >
    > But if the input is
    >
    > a b c d r y
    > a s f d o u
    >
    > spacing will be lost (though that's not a problem if the OP's input is
    > exactly as he showed). Moreover, you are inserting two extra spaces at the
    > end of each line.
    >


    ok, i do get what you mean, but i've never seen that as a problem with
    how i use awk... ;-)

    and the 'print " "' can be replaced with a 'print ""', but than there's
    still an extra space at the end of the line....

    --
    Luuk

  10. Default Re: Print fields starting with N to the end of line.



    On 3/31/2008 6:14 AM, Luuk wrote:
    > Rajan schreef:
    >
    >>
    >>"Luuk" <Luuk@invalid.lan> wrote in message
    >>news:3ve5c5-p27.ln1@a62-251-88-195.adsl.xs4all.nl...
    >>
    >>>Peter schreef:
    >>>
    >>>>Hello, seems that I'm missing something obvious. There is file with
    >>>>variable number of field, is there any simple way in awk to print
    >>>>fields starting with field N up to the end of line. e.g.
    >>>>
    >>>>$ cat test.txt
    >>>>a b c
    >>>>a b c d
    >>>>a b c d e f
    >>>>a b c d v e
    >>>>
    >>>>How to print
    >>>>$ awk <???> test.txt
    >>>>
    >>>>d
    >>>>d e f
    >>>>d v e
    >>>>
    >>>>Of course this is simplification of real data I have so I definitely
    >>>>need awk to parse it Thank you very much in advance.
    >>>
    >>>$ awk -v nr=4 '{ for (x=nr; x<=NF; x++) {
    >>> printf $x " "; }; print " " }' test.txt
    >>>
    >>>
    >>>--
    >>>Luuk

    >>
    >>One thing that we should note here is we will lose spacing between the
    >>fields this way.
    >>
    >>Rajan

    >
    >
    > read again, it says:
    > printf $x " ";
    >
    > so, the output is:
    > /tmp # awk -v nr=4 '{ for (x=nr; x<=NF; x++) {
    > > printf $x " "; }; print " " }' test.txt

    >
    > d
    > d e f
    > d v e


    Right, so if the input was tab-separated for example, you'd be changing all the
    tabs to blank chars.

    More importantly, you aren't providing the right arguments to printf so you
    could get radically different output than your input. Look:

    $ echo "a c d e f" | awk -v nr=4 '{ for (x=nr; x<=NF; x++) {
    printf $x " "; }; print " " }'
    e f
    $ echo "a c d %s f" | awk -v nr=4 '{ for (x=nr; x<=NF; x++) {
    printf $x " "; }; print " " }'
    awk: cmd. line:1: (FILENAME=- FNR=1) fatal: not enough arguments to satisfy form
    at string
    `%s '
    ^ ran out for this one

    The first argument for printf is a format, not input data.

    ITYM:

    $ echo "a c d %s f" | awk -v nr=4 '{ for (x=nr; x<=NF; x++) {
    printf "%s ",$x; }; print " " }'
    %s f

    but that still, in addition to potentially changing all the white space,
    adds 2 blank chars to the end of the line. To avoid that problem do this:

    $ echo "a c d %s f" | awk -v nr=4 '{ for (x=nr; x<=NF; x++) {
    printf "%s%s",sep,$x; sep=FS }; print "" }'
    %s f

    Regards,

    Ed.


+ Reply to Thread
Page 1 of 3 1 2 3 LastLast