1844060 Members
3105 Online
110227 Solutions
New Discussion

Shell program problem!

 
SOLVED
Go to solution
Fragon
Trusted Contributor

Shell program problem!

I have a text file cosalary , and it's format like this:
username sn salary
========= == ======
acduser0001 1 1355
acduser0002 2 1570
...
isduser0001 1 1669
isduser0002 2 1398
...
psduser0001 1 1688
psduser0002 2 1568
...
I want to make that all users whose username begin with "isduser", its salary increase 1000.
How to do?
Thanks a lot!
7 REPLIES 7
Heiner E. Lennackers
Respected Contributor
Solution

Re: Shell program problem!

Hi,

here is a quick and dirty awk hack, use with care:awk 'match($1,"^isduser") { printf "%s %d %d\n",$1,$2,$3+1000;a=1}
a==0 {print $0}
{a=0}' >

You my move the newfile over the origfile
mv .bak
mv

Heiner

if this makes any sense to you, you have a BIG problem
Jean-Luc Oudart
Honored Contributor

Re: Shell program problem!

awk '
{
if(substr($1,1,7)=="isduser")
{ salary=$3 + 1000;
print $1,$2,salary;
} else print $0;
}' yourfile > yournewfile

Jean-Luc
fiat lux
Leif Halvarsson_2
Honored Contributor

Re: Shell program problem!

Hi
awk ' { $1 ~ /^isduser/ { if ($3 >= 1000) ; print $1 $3 }}'
harry d brown jr
Honored Contributor

Re: Shell program problem!

cat filename | awk '{salary=$3; if ( /^isduser/ ) {salary = salary * 1.10}; printf("%s %s %s\n",$1,$2,salary);}'

live free or die
harry
Live Free or Die
H.Merijn Brand (procura
Honored Contributor

Re: Shell program problem!

l1:/u/usr/merijn 107 > cat sal_file
username sn salary
========= == ======
acduser0001 1 1355
acduser0002 2 1570
...
isduser0001 1 1669
isduser0002 2 1398
...
psduser0001 1 1688
psduser0002 2 1568
l1:/u/usr/merijn 108 > perl -pi -e'/^isduser/&&s/\b(\d+)$/$1+1000/e' sal_file
l1:/u/usr/merijn 109 > cat sal_file
username sn salary
========= == ======
acduser0001 1 1355
acduser0002 2 1570
...
isduser0001 1 2669
isduser0002 2 2398
...
psduser0001 1 1688
psduser0002 2 1568
l1:/u/usr/merijn 110 >
Enjoy, Have FUN! H.Merijn
harry d brown jr
Honored Contributor

Re: Shell program problem!


change my {salary=salary*1.10} to {salary=salary+1000}

as in

cat dah4 | awk '{salary=$3; if ( /^isduser/ ) {salary=salary + 1000}; printf("%s %s %s\n", $1, $2, salary);}'


live free or die
harry
Live Free or Die
Leif Halvarsson_2
Honored Contributor

Re: Shell program problem!


Sorry

I misunderstod you, this should work better.
awk ' $1 ~ /^isduser/ { if ($3 >= 1000) ; print $1" "$2" " $3+1000 }
$1 !~ /^isduser/ { print $0 } '