Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

easily omitting fields from print

10 views
Skip to first unread message

tyapca7

unread,
Nov 19, 2009, 2:45:49 AM11/19/09
to
Hi,

is there any way to print out, e.g. , $2 $3 $4.... $LAST,
i.e., just omitting $1 ?

Thanks,
++pac.

Hermann Peifer

unread,
Nov 19, 2009, 2:51:15 AM11/19/09
to
tyapca7 wrote:
> Hi,
>
> is there any way to print out, e.g. , $2 $3 $4.... $LAST,
> i.e., just omitting $1 ?
>

There are some awkish ways of doing this, but the most obvious thing to
do would be to try this:

cut -f2-

Hermann

tyapca7

unread,
Nov 19, 2009, 3:14:01 AM11/19/09
to

thank you, however, I live on Plan 9, where there is no 'cut' ...
maybe it is wort to be ported ... is it in textutils?
++pac

Ed Morton

unread,
Nov 19, 2009, 3:53:21 AM11/19/09
to

Assuming that you're using the default FS, with a POSIX awk it's:

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

and with GNU awk:

gawk --re-interval '...same...'

The number within the "{...}" is the number of initial fields to delete.

Ed.

tyapca7

unread,
Nov 19, 2009, 4:53:33 AM11/19/09
to

I have the original UNIX awk, i think derived fom Sys7, or so, manpge
is here
http://plan9.bell-labs.com/magic/man2html/1/awk
so, no POSIX extensions, but,
I think for me ' ' , or \x20, should == [[:space:]]
but I still can't get what's going on...
I'm going to consult man gawk on linux box, but would be happy if you
could explain the above mentioned awk statement in a few words,
big thank you, cordially,

Peter.

tyapca7

unread,
Nov 19, 2009, 5:05:48 AM11/19/09
to
I've just obtained this solution from Russ Cox, so I forward it to
others, thanks, Russ, and thank you all!!


awk '{print substr($0, 1+length($1)+1)}'

Ed Morton

unread,
Nov 19, 2009, 5:14:34 AM11/19/09
to
tyapca7 wrote:
> On Nov 19, 9:53 am, Ed Morton <mortons...@gmail.com> wrote:
>> tyapca7 wrote:
>>> Hi,
>>> is there any way to print out, e.g. , $2 $3 $4.... $LAST,
>>> i.e., just omitting $1 ?
>>> Thanks,
>>> ++pac.
>> Assuming that you're using the default FS, with a POSIX awk it's:
>>
>> awk '{sub(/^[[:space:]]*([^[:space:]]*[[:space:]]*){1}/,"")}1'
>>
>> and with GNU awk:
>>
>> gawk --re-interval '...same...'
>>
>> The number within the "{...}" is the number of initial fields to delete.
>>
>> Ed.
>
> I have the original UNIX awk,

Then get a new awk as that's commonly referred to as "old, broken awk" for good
reason.

> i think derived fom Sys7, or so, manpge is here
> http://plan9.bell-labs.com/magic/man2html/1/awk

That man page describes "nextfile" and the ability to delete a whole array, both
of which are GNU awk extensions so that doesn't match what you said about which
awk you're using.

> so, no POSIX extensions, but,
> I think for me ' ' , or \x20, should == [[:space:]]

No, [[:space:]] is a character class that includes multiple white space
characters (blank, tab, newline, etc.), not just one.

> but I still can't get what's going on...
> I'm going to consult man gawk on linux box, but would be happy if you
> could explain the above mentioned awk statement in a few words,
> big thank you, cordially,

In awk the default FS represents any sequence of white space characters except
the RS. Using the default FS, any leading white space characters are ignored in
determining the fields, so $1 is the first sequence of non-white-space
characters in the record (line). This script:

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

says:

awk '{sub(/ # replace
^[[:space:]]* # an initial sequence of zero or more spaces
( # followed by a group of (
[^[:space:]]* # zero or more non-spaces
[[:space:]]* # followed by zero or more spaces
){N} # ) repeated N times
/, # with
"")} # a null string
1' # and print the current record

So the initial "^[[:space:]]*" causes any leading white space to be skipped and
"([^[:space:]]*[[:space:]]*)" matches a field plus the spaces after that field.
The "{N}" says how many of those field-then-FS groups to match. Replacing it all
with a null string obviously deletes the full regexp, i.e. leading white space
followed by N field-then-FS groups.

Ed.

Ed Morton

unread,
Nov 19, 2009, 5:24:14 AM11/19/09
to

That is not a solution. Look:

$ echo " abc def" | awk '{print substr($0, 1+length($1)+1)}'
abc def

$ echo " abc def" | gawk --re-interval

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

def


$ echo "abc def" | awk '{print substr($0, 1+length($1)+1)}'
def

$ echo "abc def" | gawk --re-interval

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

def


Regards,

Ed.

Ed Morton

unread,
Nov 19, 2009, 5:31:25 AM11/19/09
to
Ed Morton wrote:
> tyapca7 wrote:
>> I've just obtained this solution from Russ Cox, so I forward it to
>> others, thanks, Russ, and thank you all!!
>>
>>
>> awk '{print substr($0, 1+length($1)+1)}'

If you wanted something that will only work if you have no leading white space
and just one blank between fields, that'd just be:

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

Ed,

>
> That is not a solution. Look:
>
> $ echo " abc def" | awk '{print substr($0, 1+length($1)+1)}'
> abc def
>
> $ echo " abc def" | gawk --re-interval
> '{sub(/^[[:space:]]*([^[:space:]]*[[:space:]]*){1}/,"")}1'
> def
>
>
> $ echo "abc def" | awk '{print substr($0, 1+length($1)+1)}'
> def
>
> $ echo "abc def" | gawk --re-interval
> '{sub(/^[[:space:]]*([^[:space:]]*[[:space:]]*){1}/,"")}1'
> def
>
>
> Regards,
>
> Ed.
>

If you want something that will work if you have no leading white space and just
one FS between fields, that'd just be:

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

tyapca7

unread,
Nov 19, 2009, 5:34:09 AM11/19/09
to
Thanks 1000x, Ed!

Peter, aka ++pac

tyapca7

unread,
Nov 19, 2009, 6:38:54 AM11/19/09
to
thanks! replacing leading/multiple whitespace is a matter of [ sed |
tr ], if I understand it right... do keep things as simple as
possible... IMHO... thanks2 all, again,
++pac.

tyapca7

unread,
Nov 19, 2009, 6:58:31 AM11/19/09
to

Ed Morton

unread,
Nov 19, 2009, 10:23:29 AM11/19/09
to

Well, using 1 tool instead of 3 seems like it'd keep things as simple as
possible. The awk solution I gave you originally is intended to satisfy your
question in general, but if you're looking at a mush of sed, tr, length(), etc.
to do what you want then you must have a very restricted input and set of
requirements so if you want to post whatever your pipeline solution is and some
small set of sample input and expected output, we can show you how to do it
simply with just awk.

Ed.

Loki Harfagr

unread,
Nov 19, 2009, 10:50:36 AM11/19/09
to
Thu, 19 Nov 2009 04:14:34 -0600, Ed Morton did cat :

Excellent Ed! I guess you had your promised coffee :-)

For the OP, in case you don't mind losing the spacing
here's a quick and dirty:
$ echo ' one two three four ' | gawk '{$1=""} 1'
two three four
$

Ed Morton

unread,
Nov 19, 2009, 11:04:28 AM11/19/09
to
Loki Harfagr wrote:
> Thu, 19 Nov 2009 04:14:34 -0600, Ed Morton did cat :
>
>> tyapca7 wrote:
>>> On Nov 19, 9:53 am, Ed Morton <mortons...@gmail.com> wrote:
>>>> tyapca7 wrote:
>>>>> Hi,
>>>>> is there any way to print out, e.g. , $2 $3 $4.... $LAST, i.e., just
>>>>> omitting $1 ?
<snip>

> For the OP, in case you don't mind losing the spacing
> here's a quick and dirty:
> $ echo ' one two three four ' | gawk '{$1=""} 1'
> two three four
> $

Right, and a simple sub at the back would take care of the leading white space:

awk '{$1="";sub(/^ +/,"")}1'

Ed.

w_a_x_man

unread,
Nov 19, 2009, 11:50:21 AM11/19/09
to

ruby -pne 'sub(/^\s*\S+\s+/, "")' file

=== input ===
a b c d e
a b c d e
a b c d e

=== output ===
b c d e
b c d e
b c d e

tyapca7

unread,
Nov 20, 2009, 6:18:06 AM11/20/09
to
What is wrong with this?

% cat foo2
1,2,3,4
5,6,7,8
9,10,11,12

% gawk '{FS=","; $2=""; $3=""; } 1' foo2
1,2,3,4
5 8
9 12

%

pk

unread,
Nov 20, 2009, 6:30:21 AM11/20/09
to
tyapca7 wrote:

See "Advanced Notes: Changing FS Does Not Affect the Fields" here:

http://www.gnu.org/manual/gawk/gawk.html#Field-Splitting-Summary

Kenny McCormack

unread,
Nov 20, 2009, 7:21:48 AM11/20/09
to
In article <29ca7f3e-98a9-4534...@r24g2000yqd.googlegroups.com>,

You made a small typo. You meant to write:

BEGIN {FS=","}
{ $2=""; $3="";print }

You may want to research the "BEGIN" keyword in AWK.

tyapca7

unread,
Nov 20, 2009, 7:43:02 AM11/20/09
to

> BEGIN {FS=","}
> { $2=""; $3="";print }
>
Thanks! Now it works both with gawk and Plan 9's awk.
0 new messages