Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Awk > Re: Print field...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 17 of 24 Topic 2212 of 2370
Post > Topic >>

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

by Ed Morton <morton@[EMAIL PROTECTED] > Mar 31, 2008 at 07:13 AM

On 3/31/2008 7:10 AM, Luuk wrote:
> 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
> 

Right, it was just to fix your additional-white-space problem. You can't
in
general fix the other problem with a loop (you need the sub()s solution),
though
by using FS instead of a space you may get more milage if you're parsing
files
that are separated by some specific, non-RE, FS value that isn't a single
blank
char.

	Ed.
 




 24 Posts in Topic:
Print fields starting with N to the end of line.
Peter <volkov.peter@[E  2008-03-30 11:12:35 
Re: Print fields starting with N to the end of line.
pk <pk@[EMAIL PROTECTE  2008-03-30 20:47:04 
Re: Print fields starting with N to the end of line.
Ed Morton <morton@[EMA  2008-03-30 13:34:59 
Re: Print fields starting with N to the end of line.
"Rajan" <svr  2008-03-30 15:25:31 
Re: Print fields starting with N to the end of line.
Luuk <Luuk@[EMAIL PROT  2008-03-30 23:57:26 
Re: Print fields starting with N to the end of line.
"Rajan" <svr  2008-03-30 21:18:15 
Re: Print fields starting with N to the end of line.
Luuk <Luuk@[EMAIL PROT  2008-03-31 13:14:16 
Re: Print fields starting with N to the end of line.
pk <pk@[EMAIL PROTECTE  2008-03-31 13:48:17 
Re: Print fields starting with N to the end of line.
Luuk <Luuk@[EMAIL PROT  2008-03-31 14:01:48 
Re: Print fields starting with N to the end of line.
pk <pk@[EMAIL PROTECTE  2008-03-31 14:24:17 
Re: Print fields starting with N to the end of line.
Janis Papanagnou <Jani  2008-03-31 19:54:37 
Re: Print fields starting with N to the end of line.
Janis Papanagnou <Jani  2008-03-31 19:56:05 
Re: Print fields starting with N to the end of line.
pk <pk@[EMAIL PROTECTE  2008-04-01 09:48:02 
Re: Print fields starting with N to the end of line.
Ed Morton <morton@[EMA  2008-04-01 08:11:14 
Re: Print fields starting with N to the end of line.
Ed Morton <morton@[EMA  2008-03-31 07:03:39 
Re: Print fields starting with N to the end of line.
Luuk <Luuk@[EMAIL PROT  2008-03-31 14:10:01 
Re: Print fields starting with N to the end of line.
Ed Morton <morton@[EMA  2008-03-31 07:13:53 
Re: Print fields starting with N to the end of line.
Barry Fishman <barry_f  2008-03-31 13:45:27 
Re: Print fields starting with N to the end of line.
Ed Morton <morton@[EMA  2008-03-31 10:47:10 
Re: Print fields starting with N to the end of line.
pk <pk@[EMAIL PROTECTE  2008-03-31 18:15:35 
Re: Print fields starting with N to the end of line.
Ed Morton <morton@[EMA  2008-03-31 11:29:11 
Re: Print fields starting with N to the end of line.
Janis Papanagnou <Jani  2008-03-31 20:01:12 
Re: Print fields starting with N to the end of line.
Janis <janis_papanagno  2008-04-01 00:39:44 
Re: Print fields starting with N to the end of line.
pk <pk@[EMAIL PROTECTE  2008-04-01 10:11:43 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Mon Oct 6 17:45:26 CDT 2008.