Ed Morton schreef:
>
> On 3/31/2008 6:14 AM, Luuk wrote:
>> Rajan schreef:
>>
>>> "Luuk" <Luuk@[EMAIL PROTECTED]
> wrote in message
>>> news:3ve5c5-p27.ln1@[EMAIL PROTECTED]
>>>
>>>> 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 im****tantly, 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.
>
i agree totally...
with you last suggestion you would also change all field-seperators to
default seperators used with awk
--
Luuk


|