Code Comments
Programming Forum and web based access to our favorite programming groups.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.
Post Follow-up to this messagePeter 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.
Post Follow-up to this message
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.
Post Follow-up to this message
"Ed Morton" <morton@lsupcaemnt.com> wrote in message
news:47EFDD53.30401@lsupcaemnt.com...
>
>
> On 3/30/2008 1:12 PM, Peter wrote:
>
> 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.
Post Follow-up to this messagePeter 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
Post Follow-up to this message
"Luuk" <Luuk@invalid.lan> wrote in message
news:3ve5c5-p27.ln1@a62-251-88-195.adsl.xs4all.nl...
> Peter schreef:
>
> $ 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
Post Follow-up to this messageRajan schreef:
>
>
> "Luuk" <Luuk@invalid.lan> wrote in message
> news:3ve5c5-p27.ln1@a62-251-88-195.adsl.xs4all.nl...
>
> 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
Post Follow-up to this messageLuuk wrote:
>
> read again, it says:
> printf $x " ";
>
> so, the output is:
> /tmp # awk -v nr=4 '{ for (x=nr; x<=NF; x++) {
>
> 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.
Post Follow-up to this messageEd Morton schreef:
>
> On 3/31/2008 6:14 AM, Luuk wrote:
>
> Right, so if the input was tab-separated for example, you'd be changing al
l the
> tabs to blank chars.
>
> More importantly, you aren't providing the right arguments to printf so yo
u
> 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 satisf
y 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
Post Follow-up to this messageOn 3/31/2008 7:10 AM, Luuk wrote: > Ed Morton schreef: > > > > 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), th ough by using FS instead of a space you may get more milage if you're parsing fil es that are separated by some specific, non-RE, FS value that isn't a single bl ank char. Ed.
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.