a short cut related question in awk 
Author Message
 a short cut related question in awk

{ $3=100.0; print $0}
can do the trick, depending on whether you are worried about exact
spacing. If you are then something along the lines of:
{ a=$0
  n=length($3)
  i=index(a,$3)
  a=substr(a,i-1) "100.0" substr(a,i+n+1)
  print a
Quote:
}

will meet your needs; beware that if $1 or $2 look like $3 then the
index function call will have to be replaced with a fairly complicated
match/regular expression and RSTART and RLENGTH need to be used in place
of i and n in the subsequent substr's.
Quote:

> I have a file containing more than 20 columns and I want  to reprint
> it with only the third column changed and evrything else same as
> before.
> the way I was think ing of doing it  is:
> awk '{print $1,$2,100.0,$4.......upto $20}' name of file
> My question is instead of typing $4 upto $20 is there a shorter way to
> do this?

> thanks
> melroy



Wed, 23 Mar 2005 03:18:30 GMT  
 a short cut related question in awk

Quote:

>I have a file containing more than 20 columns and I want  to reprint
>it with only the third column changed and evrything else same as
>before.
>the way I was think ing of doing it  is:
>awk '{print $1,$2,100.0,$4.......upto $20}' name of file
>My question is instead of typing $4 upto $20 is there a shorter way to
>do this?

awk '{$3=100.0; print $0}' infile

or

awk '{$3=100.0; print }' infile

Chuck Demas

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Wed, 23 Mar 2005 03:37:50 GMT  
 a short cut related question in awk

Quote:
> I have a file containing more than 20 columns and I want  to reprint
> it with only the third column changed and evrything else same as
> before.
> the way I was think ing of doing it  is:
> awk '{print $1,$2,100.0,$4.......upto $20}' name of file
> My question is instead of typing $4 upto $20 is there a shorter way to
> do this?

> thanks
> melroy

Nice answers from others...the psychotic reduction is:

    $3="100.0"

as in

    awk '$3="100.0"' datafile

It can't be just

    awk '$3=100.0'

as one poster suggested because awk (at least gawk3.1.0/Win98) evaluates
unquoted 100.0 as a number, stored reductionalistly as 100.  You don't get
the ".0" that you demonstrated in your output.

$3="100.0" is an assignment expression that yeilds a string, and is
converted to a number for comparison with 0, to see if the rule's action
should be executed.  Since it is non-zero (in fact, it is 100), the action
is executed.  Since the action (the {} part) is not present, the default
action is used.  The default action is {print}.  'print', without arguments,
by default does 'print $0'.  Evaluating $0 after an assignment to a '$'
element forces a reassemblage of $0 with OFS (output field separator), which
defaults to the space character.

The problem with psychotic reduction is that it takes many lines of comment
to explain one assignment, and why it works.

    - Dan



Wed, 23 Mar 2005 11:47:11 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. long = short*short ; (short,short) = long/short

2. How do you disable a short cut key?

3. (VSE) Short cut's on menu...

4. Windows XP short-cut - HTML Help problems

5. short cuts?

6. RubyChangeRequest #U001: cut operator for short-circuiting method chains

7. any short cut?

8. cut short string

9. short cut to loop ?

10. Personalized keyboard short cut.

11. Keyboard short-cuts

12. NT related question (not really Python related)

 

 
Powered by phpBB® Forum Software