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

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

ok, i do get what you mean, but i've never seen that as a problem with
how i use awk... ;-)

and the 'print " "' can be replaced with a 'print ""', but than there's
still an extra space at the end of the line....

--
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 6:14 AM, Luuk wrote:
> Rajan schreef:
> 
>
>
> 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

Right, so if the input was tab-separated for example, you'd be changing all 
the
tabs to blank chars.

More importantly, 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.


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


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

> ok, i do get what you mean, but i've never seen that as a problem with
> how i use awk... ;-)

I didn't mean to say that that is necessarily a problem :-). It /may/ be a
problem sometimes and, since there are ways to avoid that, imho it should
be best avoided.

> and the 'print " "' can be replaced with a 'print ""', but than there's
> still an extra space at the end of the line....

You can do this then (still assuming inter-field spacing does not matter):

awk -v nr=4 '{ for (x=nr; x<NF; x++) {printf $x " "} print $NF }'

--
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.
Luuk <Luuk@invalid.lan> writes:
> Ed Morton schreef: 

If this is just a part of something more complicated, the following
might be another approach:

BEGIN {
shift=3
}
NF > shift {
for (x=1; x<= NF-shift; x++)
$x = $(x+shift)
NF=NF-shift
print $0
next
}
{ print "" }

Works with gawk and mawk.

--
Barry Fishman

Report this thread to moderator Post Follow-up to this message
Old Post
Barry Fishman
04-01-08 12:40 AM


Re: Print fields starting with N to the end of line.
On 3/31/2008 8:45 AM, Barry Fishman wrote:
> Luuk <Luuk@invalid.lan> writes:
> 
>
> If this is just a part of something more complicated, the following
> might be another approach:
>
> BEGIN {
>   shift=3
> }
> NF > shift {
>    for (x=1; x<= NF-shift; x++)
>      $x = $(x+shift)
>    NF=NF-shift
>    print $0
>    next
> }
> { print "" }
>
> Works with gawk and mawk.
>

You live & learn. I've never seen setting NF to a lower value used to trunca
te
the record before. You can make the above code a bit more concise:

BEGIN { shift=3 }
{
nf=0
for (x=shift+1; x<=NF; x++)
$(++nf) = $x
NF=nf
print
}

but of course it still has the problem of recompiling $0 and so changing FS 
to
OFS and I'm not sure all awks would behave the same if you explicitly set NF
.

Ed.


Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
04-01-08 12:40 AM


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

> You live & learn. I've never seen setting NF to a lower value used to
> truncate the record before.

But, isn't

awk 'NF--' file.txt

a common idiom to remove last field in each line of a file (assuming each
line has at least one field)?

Thanks

--
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
04-01-08 12:40 AM


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

On 3/31/2008 11:15 AM, pk wrote:
> Ed Morton wrote:
>
> 
>
>
> But, isn't
>
> awk 'NF--' file.txt
>
> a common idiom to remove last field in each line of a file (assuming each
> line has at least one field)?

I've never seen that before. May just be me, though...

Ed.


Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
04-01-08 12:40 AM


Re: Print fields starting with N to the end of line.
Luuk wrote:
> pk schreef:
> 
>
> ok, i do get what you mean, but i've never seen that as a problem with
> how i use awk... ;-)
>
> and the 'print " "' can be replaced with a 'print ""', but than there's
> still an extra space at the end of the line....
>

You can build a string inside the loop and print it outside, if you like...

for (x=nr; x<=NF; x++)
z = z " " $x
print substr(z, 2)


Janis

Report this thread to moderator Post Follow-up to this message
Old Post
Janis Papanagnou
04-01-08 12:40 AM


Re: Print fields starting with N to the end of line.
Janis Papanagnou wrote:
> Luuk wrote:
> 
>
> You can build a string inside the loop and print it outside, if you like...[/color
]

And don't forget initializing z...

z = ""

>   for (x=nr; x<=NF; x++)
>       z = z " " $x
>   print substr(z, 2)
>
>
> Janis

Report this thread to moderator Post Follow-up to this message
Old Post
Janis Papanagnou
04-01-08 12:40 AM


Re: Print fields starting with N to the end of line.
Ed Morton wrote:
>
> On 3/31/2008 11:15 AM, pk wrote:
> 
>
>
> I've never seen that before. May just be me, though...

It's not a common idiom.

I seem to recall to have it seen here in c.l.a the first time a few ws
ago, posted by Hermann Peifer, AFAIR.

Janis

>
> 	Ed.
>

Report this thread to moderator Post Follow-up to this message
Old Post
Janis Papanagnou
04-01-08 12:40 AM


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:48 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.