how to print from n-th field to the end of the line

This is a discussion on how to print from n-th field to the end of the line within the awk forums in Programming Languages category; Hello everybody, I am looking for a way to print the contents of a line beginning with the n-th field until the end of the line (or until some other field). Is there a way to do this without using a loop? I mean, is there a syntax how to specify - say - the 3rd to 5th field - something like ${3..5}? I have been looking for this for a while now but it does not seem to be covered anywhere... Cheers, Wolfgang...

Go Back   Application Development Forum > Programming Languages > awk

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-01-2008, 05:34 PM
wolfgang.arendt@googlemail.com
Guest
 
Default how to print from n-th field to the end of the line

Hello everybody,

I am looking for a way to print the contents of a line beginning with
the n-th field until the end of the line (or until some other field).
Is there a way to do this without using a loop? I mean, is there a
syntax how to specify - say - the 3rd to 5th field - something like
${3..5}?
I have been looking for this for a while now but it does not seem to
be covered anywhere...

Cheers, Wolfgang
Reply With Quote
  #2  
Old 08-01-2008, 06:07 PM
Ed Morton
Guest
 
Default Re: how to print from n-th field to the end of the line

On 8/1/2008 4:34 PM, wolfgang.arendt@googlemail.com wrote:
> Hello everybody,
>
> I am looking for a way to print the contents of a line beginning with
> the n-th field until the end of the line (or until some other field).
> Is there a way to do this without using a loop? I mean, is there a
> syntax how to specify - say - the 3rd to 5th field - something like
> ${3..5}?
> I have been looking for this for a while now but it does not seem to
> be covered anywhere...
>
> Cheers, Wolfgang


$ echo "a b c d e f" |
gawk --re-interval '{sub(/^[[:space:]]*([^[:space:]]*[[:space:]]*){2}/,"")}1'
c d e f

$ echo "a b c d e f " |
gawk --re-interval '{sub(/([[:space:]]*[^[:space:]]*){2}[[:space:]]*$/,"")}1'
a b c d

$ echo "a b c d e f" |
gawk --re-interval '{
sub(/^[[:space:]]*([^[:space:]]*[[:space:]]*){2}/,"")
sub(/([[:space:]]*[^[:space:]]*){2}[[:space:]]*$/,"")
}1'
c d

Regards,

Ed.

Reply With Quote
  #3  
Old 08-04-2008, 08:26 AM
poulteki
Guest
 
Default Re: how to print from n-th field to the end of the line

On 2 août, 00:07, Ed Morton <mor...@lsupcaemnt.com> wrote:
> On 8/1/2008 4:34 PM, wolfgang.are...@googlemail.com wrote:
>
> > Hello everybody,

>
> > I am looking for a way to print the contents of a line beginning with
> > the n-th field until the end of the line (or until some other field).
> > Is there a way to do this without using a loop? I mean, is there a
> > syntax how to specify - say - the 3rd to 5th field - something like
> > ${3..5}?
> > I have been looking for this for a while now but it does not seem to
> > be covered anywhere...

>
> > Cheers, Wolfgang

>
> $ echo "a b c d e f" |
> gawk --re-interval '{sub(/^[[:space:]]*([^[:space:]]*[[:space:]]*){2}/,"")}1'
> c d e f
>
> $ echo "a b c d e f " |
> gawk --re-interval '{sub(/([[:space:]]*[^[:space:]]*){2}[[:space:]]*$/,"")}1'
> a b c d
>
> $ echo "a b c d e f" |
> gawk --re-interval '{
> sub(/^[[:space:]]*([^[:space:]]*[[:space:]]*){2}/,"")
> sub(/([[:space:]]*[^[:space:]]*){2}[[:space:]]*$/,"")}1'
>
> c d
>
> Regards,
>
> Ed.


Ok guys,
why using a beautiful command (of course we are on comp.lang.awk)
while these ones do the correct job ?

Exemple :
echo "a b c d e f" | tr -s ' ' | cut -d' ' -f1-4
a b c d

My "mentor" told me to use the simpliest before the complex.
Reply With Quote
  #4  
Old 08-04-2008, 08:35 AM
Loki Harfagr
Guest
 
Default Re: how to print from n-th field to the end of the line

Mon, 04 Aug 2008 05:26:07 -0700, poulteki did catÂ*:

> On 2 août, 00:07, Ed Morton <mor...@lsupcaemnt.com> wrote:
>> On 8/1/2008 4:34 PM, wolfgang.are...@googlemail.com wrote:
>>
>> > Hello everybody,

>>
>> > I am looking for a way to print the contents of a line beginning with
>> > the n-th field until the end of the line (or until some other field).
>> > Is there a way to do this without using a loop? I mean, is there a
>> > syntax how to specify - say - the 3rd to 5th field - something like
>> > ${3..5}?
>> > I have been looking for this for a while now but it does not seem to
>> > be covered anywhere...

>>
>> > Cheers, Wolfgang

>>
>> $ echo "a b c d e f" |
>> gawk --re-interval
>> '{sub(/^[[:space:]]*([^[:space:]]*[[:space:]]*){2}/,"")}1' c d e f
>>
>> $ echo "a b c d e f " |
>> gawk --re-interval
>> '{sub(/([[:space:]]*[^[:space:]]*){2}[[:space:]]*$/,"")}1' a b c d
>>
>> $ echo "a b c d e f" |
>> gawk --re-interval '{
>> sub(/^[[:space:]]*([^[:space:]]*[[:space:]]*){2}/,"")
>> sub(/([[:space:]]*[^[:space:]]*){2}[[:space:]]*$/,"")}1'
>>
>> c d
>>
>> Regards,
>>
>> Ed.

>
> Ok guys,
> why using a beautiful command (of course we are on comp.lang.awk) while
> these ones do the correct job ?


Probably because they don't !-)

> Exemple :
> echo "a b c d e f" | tr -s ' ' | cut -d' ' -f1-4 a b c d


Firstly, what the OP had in mind was (as mentionned in the subject
line) closer to:

$ echo "a b c d e f" | tr -s ' ' | cut -d' ' -f4-
d e f

And, there was a catch in it, preserve the "pavement" :-)
As you can see there are many blanks lost with the 'cut':
$ echo "a b c d e f" | tr -s ' ' | cut -d' ' -f4-
d e f

> My "mentor" told me to use the simpliest before the complex.


Writing on the wall is a goo story to read, my
janitor told me to read before to write ;D)
Reply With Quote
  #5  
Old 08-04-2008, 08:35 AM
Janis Papanagnou
Guest
 
Default Re: how to print from n-th field to the end of the line

poulteki wrote:
> On 2 août, 00:07, Ed Morton <mor...@lsupcaemnt.com> wrote:
>
>>On 8/1/2008 4:34 PM, wolfgang.are...@googlemail.com wrote:
>>
>>
>>>Hello everybody,

>>
>>>I am looking for a way to print the contents of a line beginning with
>>>the n-th field until the end of the line (or until some other field).
>>>Is there a way to do this without using a loop? I mean, is there a
>>>syntax how to specify - say - the 3rd to 5th field - something like
>>>${3..5}?
>>>I have been looking for this for a while now but it does not seem to
>>>be covered anywhere...

>>
>>>Cheers, Wolfgang

>>
>>$ echo "a b c d e f" |
>>gawk --re-interval '{sub(/^[[:space:]]*([^[:space:]]*[[:space:]]*){2}/,"")}1'
>>c d e f
>>
>>$ echo "a b c d e f " |
>>gawk --re-interval '{sub(/([[:space:]]*[^[:space:]]*){2}[[:space:]]*$/,"")}1'
>>a b c d
>>
>>$ echo "a b c d e f" |
>>gawk --re-interval '{
>>sub(/^[[:space:]]*([^[:space:]]*[[:space:]]*){2}/,"")
>>sub(/([[:space:]]*[^[:space:]]*){2}[[:space:]]*$/,"")}1'
>>
>>c d
>>
>>Regards,
>>
>> Ed.

>
>
> Ok guys,
> why using a beautiful command (of course we are on comp.lang.awk)
> while these ones do the correct job ?
>
> Exemple :
> echo "a b c d e f" | tr -s ' ' | cut -d' ' -f1-4
> a b c d
>
> My "mentor" told me to use the simpliest before the complex.


But your solution, as opposed to Ed's, changes the spacing between
the fields (which might not be what one expects in any case).

Janis
Reply With Quote
  #6  
Old 08-04-2008, 08:42 AM
poulteki@live.fr
Guest
 
Default Re: how to print from n-th field to the end of the line

On 4 août, 14:35, Janis Papanagnou <Janis_Papanag...@hotmail.com>
wrote:
> poulteki wrote:
> > On 2 août, 00:07, Ed Morton <mor...@lsupcaemnt.com> wrote:

>
> >>On 8/1/2008 4:34 PM, wolfgang.are...@googlemail.com wrote:

>
> >>>Hello everybody,

>
> >>>I am looking for a way to print the contents of a line beginning with
> >>>the n-th field until the end of the line (or until some other field).
> >>>Is there a way to do this without using a loop? I mean, is there a
> >>>syntax how to specify - say - the 3rd to 5th field - something like
> >>>${3..5}?
> >>>I have been looking for this for a while now but it does not seem to
> >>>be covered anywhere...

>
> >>>Cheers, Wolfgang

>
> >>$ echo "a b c d e f" |
> >>gawk --re-interval '{sub(/^[[:space:]]*([^[:space:]]*[[:space:]]*){2}/,"")}1'
> >>c d e f

>
> >>$ echo "a b c d e f " |
> >>gawk --re-interval '{sub(/([[:space:]]*[^[:space:]]*){2}[[:space:]]*$/,"")}1'
> >>a b c d

>
> >>$ echo "a b c d e f" |
> >>gawk --re-interval '{
> >>sub(/^[[:space:]]*([^[:space:]]*[[:space:]]*){2}/,"")
> >>sub(/([[:space:]]*[^[:space:]]*){2}[[:space:]]*$/,"")}1'

>
> >>c d

>
> >>Regards,

>
> >> Ed.

>
> > Ok guys,
> > why using a beautiful command (of course we are on comp.lang.awk)
> > while these ones do the correct job ?

>
> > Exemple :
> > echo "a b c d e f" | tr -s ' ' | cut -d' ' -f1-4
> > a b c d

>
> > My "mentor" told me to use the simpliest before the complex.

>
> But your solution, as opposed to Ed's, changes the spacing between
> the fields (which might not be what one expects in any case).
>
> Janis


Okay, sorry guys, you are the best.
Reply With Quote
  #7  
Old 08-04-2008, 10:27 AM
Ed Morton
Guest
 
Default Re: how to print from n-th field to the end of the line



On 8/4/2008 7:26 AM, poulteki wrote:
> On 2 août, 00:07, Ed Morton <mor...@lsupcaemnt.com> wrote:
>
>>On 8/1/2008 4:34 PM, wolfgang.are...@googlemail.com wrote:
>>
>>
>>>Hello everybody,

>>
>>>I am looking for a way to print the contents of a line beginning with
>>>the n-th field until the end of the line (or until some other field).
>>>Is there a way to do this without using a loop? I mean, is there a
>>>syntax how to specify - say - the 3rd to 5th field - something like
>>>${3..5}?
>>>I have been looking for this for a while now but it does not seem to
>>>be covered anywhere...

>>
>>>Cheers, Wolfgang

>>
>>$ echo "a b c d e f" |
>>gawk --re-interval '{sub(/^[[:space:]]*([^[:space:]]*[[:space:]]*){2}/,"")}1'
>>c d e f
>>
>>$ echo "a b c d e f " |
>>gawk --re-interval '{sub(/([[:space:]]*[^[:space:]]*){2}[[:space:]]*$/,"")}1'
>>a b c d
>>
>>$ echo "a b c d e f" |
>>gawk --re-interval '{
>>sub(/^[[:space:]]*([^[:space:]]*[[:space:]]*){2}/,"")
>>sub(/([[:space:]]*[^[:space:]]*){2}[[:space:]]*$/,"")}1'
>>
>>c d
>>


> Ok guys,
> why using a beautiful command (of course we are on comp.lang.awk)
> while these ones do the correct job ?


As you said, this is comp.lang.awk where people come for an awk solution.
Typically if an OP says they're on a particular OS and we think there's a
significantly better solution on that OS, we'll direct them to an appropriate NG
where the experts in that OS can suggest and/or critique a solution so the OP
gets the best solution for that OS rather than the just the best we can come up
with in this NG for that OS.

In this case, there's a slightly briefer sed solution and, as always, there's
probably similair ruby and perl solution, but it wasn't worth mentioning.

> Exemple :
> echo "a b c d e f" | tr -s ' ' | cut -d' ' -f1-4
> a b c d
>
> My "mentor" told me to use the simpliest before the complex.


OK, but if the OP is on a Windows box and has no idea about UNIX tools,
implementing the above would be more complex than implementing the awk solution.

Ed.

Reply With Quote
  #8  
Old 08-04-2008, 10:35 AM
Kenny McCormack
Guest
 
Default Re: how to print from n-th field to the end of the line

In article <489711D4.50902@lsupcaemnt.com>,
Ed Morton <morton@lsupcaemnt.com> wrote:
....
>As you said, this is comp.lang.awk where people come for an awk
>solution. Typically if an OP says they're on a particular OS and we
>think there's a significantly better solution on that OS, we'll direct
>them to an appropriate NG where the experts in that OS can suggest
>and/or critique a solution so the OP gets the best solution for that OS
>rather than the just the best we can come up with in this NG for that
>OS.


Agreed, totally.

>In this case, there's a slightly briefer sed solution and, as always,
>there's probably similair ruby and perl solution, but it wasn't worth
>mentioning.


Well put.

>> Exemple : echo "a b c d e f" | tr -s ' ' | cut -d' ' -f1-4 a b c
>> d
>>
>> My "mentor" told me to use the simpliest before the complex.

>
>OK, but if the OP is on a Windows box and has no idea about UNIX tools,
>implementing the above would be more complex than implementing the awk
>solution.


Yes, and note that simplicity != efficiency. I always get a kick out of
these:
grep | tr | sed | grep | cut | ...
"solutions", where a simple script in AWK (or any other capable
language; note that I do not count any of the above named commands as
"capable") would do the job. Once you bring in multiple processes, you
can kiss efficiency goodbye.

Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 06:46 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.