1753928 Members
8936 Online
108810 Solutions
New Discussion юеВ

Setting a value with AWK

 
SOLVED
Go to solution
rmueller58
Valued Contributor

Setting a value with AWK

I am wanting to define a variable in a shell script value using AWK

If I have a value greater then 6 then define it as the next value, so 6=7, 7=8, 8=9, 9=10 upto a max of 20, and if the value is NULL or empty define it as 6.

CURDOCID=`awk -F"|" '{print $1}' /tmp/jacunload.txt`
NEWDOCID=${CURDOCID}+1
echo ${NEWDOCID}

CURDOCID Is integer or is NULL.

If it is null I want to assign a value of 6 to NEWDOCID if is valued I want to increment it but 1.

any assistance appreciated deeply
5 REPLIES 5
Dennis Handly
Acclaimed Contributor
Solution

Re: Setting a value with AWK

>NEWDOCID=`expr "${CURDOCID}" + 1`

You can also use the shell directly:
(( NEWDOCID = CURDOCID + 1 ))
NEWDOCID=$(( CURDOCID + 1 ))
Mark McDonald_2
Trusted Contributor

Re: Setting a value with AWK

Try this in ksh:

echo "${CURDOCID:-6}"

will default to 6 if not set to anything else.
James R. Ferguson
Acclaimed Contributor

Re: Setting a value with AWK

Hi:

Why not let 'awk' do everything!?!

# awk '{if (NF<=1) {print "6";next};if ($2>5 && $2<21) {print $2+1} else {print $2}}' file

Regards!

...JRF...
rmueller58
Valued Contributor

Re: Setting a value with AWK

James,

I am reinventing a major script that has been a mess from the outset combining the function of about 6 scripts into a much more streamline solution.

Awk has been my friend for some time but my scripting knowledge or lack their of limited how much I used it. It has become a very important tool.

Thanks for all that replied.

rmueller58
Valued Contributor

Re: Setting a value with AWK

Thanks guys..