Code Comments
Programming Forum and web based access to our favorite programming groups.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
Post Follow-up to this message
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.
Post Follow-up to this messageLuuk 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.
Post Follow-up to this messageLuuk <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
Post Follow-up to this messageOn 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.
Post Follow-up to this messageEd 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.
Post Follow-up to this messageOn 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.
Post Follow-up to this messageLuuk 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
Post Follow-up to this messageJanis 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
Post Follow-up to this messageEd 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. >
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.