2

I have a file which contains the following:

export TM_FUNCTION=SUM08619 ;export TM_PARAM_SEQNO=2 ;export PSEUDO_USER=HIHI; . /projects/MSS/LEE1MP/shells/sumh8701.sh  001 DATE:W:0:C:D:1:11:1:N:: DATE:W:0:C:D:1:11:N:1:W: IT ++ ++ 000052233714 DATE:W:0:C:D:1:11:N:N:: 100000 N FMPOST A / &

If i run the following value 100000 is returned:

awk -F' ' '{ print $17 }' filename

I would like to increment $17 using a script by 1 each time i run what is the best way?

2 Answers 2

3

You can use

awk '{$17=$17+1}1' filename > tmp && mv tmp filename

Calling this each time will increment Field 17 by one.

If you have a GNU awk you may use

awk -i inplace '{$17=$17+1}1' filename
Sign up to request clarification or add additional context in comments.

1 Comment

awk supports pre/post increment operators so $17++ instead of $17=$17+1 would work too.
0
mawk '++$17_'  # either form works
gawk '_$17++'
 

The extra _ is a somewhat uncommon but still POSIX-compliant way to

force string concat with empty string _

thus guaranteeing print out of all rows even if $17 got incremented from -1 to 0 due to this awk peculiarity :

numeric zero (+0) := false

  • but

string zero ("0") := TRUE (since it's a non-empty string)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.