Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Print fields starting with N to the end of line.
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.

Report this thread to moderator Post Follow-up to this message
Old Post
Peter
03-31-08 12:18 AM


Re: Print fields starting with N to the end of line.
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.

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.

Report this thread to moderator Post Follow-up to this message
Old Post
pk
03-31-08 12:18 AM


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

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.


Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
03-31-08 12:18 AM


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

"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.


Report this thread to moderator Post Follow-up to this message
Old Post
Rajan
03-31-08 12:18 AM


Re: Print fields starting with N to the end of line.
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

Report this thread to moderator Post Follow-up to this message
Old Post
Luuk
03-31-08 12:18 AM


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

"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


Report this thread to moderator Post Follow-up to this message
Old Post
Rajan
03-31-08 09:15 AM


Re: Print fields starting with N to the end of line.
Rajan 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

Report this thread to moderator Post Follow-up to this message
Old Post
Luuk
03-31-08 01:21 PM


Re: Print fields starting with N to the end of line.
Luuk 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.

Report this thread to moderator Post Follow-up to this message
Old Post
pk
03-31-08 01:21 PM


Re: Print fields starting with N to the end of line.
Ed 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

Report this thread to moderator Post Follow-up to this message
Old Post
Luuk
03-31-08 01:21 PM


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

On 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.


Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
03-31-08 01:21 PM


Sponsored Links




Last Thread Next Thread Next
Pages (3): [1] 2 3 »
Search this forum -> 
Post New Thread

AWK archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 03:50 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.