1831308 Members
3318 Online
110023 Solutions
New Discussion

Scripting problem

 
SOLVED
Go to solution
Dewa Negara_4
Regular Advisor

Scripting problem

Hi All,

I have an issue with my script. I want to insert a line based on application of the server.I have a file called report1 as below :
#cat report1
sihp8012 /root/.rhosts bdhp4262 root
sihp8012 /root/.rhosts bdhp4262.na.pg.com root
sihp8012 /root/.rhosts sisn8001 root
sihp8012 /root/.rhosts sihp8026.ap.pg.com root
sihp8026 /root/.rhosts bdhp4337.na.pg.com root
sihp8026 /root/.rhosts 192.44.190.109 root
sihp8020 /root/.rhosts bdhp4337.na.pg.com root

and I have a file called appl that contains the application name :
#cat appl
oracle,sihp8012
sap,sihp8026
web,sihp8020

I want to insert the application name between field1 and field 2 of file report1, so the output will be as below :
sihp8012 oracle /root/.rhosts bdhp4262 root
sihp8012 oracle /root/.rhosts bdhp4262.na.pg.com root
sihp8012 oracle /root/.rhosts sisn8001 root
sihp8012 oracle /root/.rhosts sihp8026.ap.pg.com root
sihp8026 sap /root/.rhosts bdhp4337.na.pg.com root
sihp8026 sap /root/.rhosts 192.44.190.109 root
sihp8020 web /root/.rhosts bdhp4337.na.pg.com root

Anyone can help how to do using shell script? Pls help. High score will be given.

Thanks and Best Regards,
Dewa


Santos
7 REPLIES 7
john korterman
Honored Contributor
Solution

Re: Scripting problem

Hi,
you can try this rather simple script. Activate it with two parameters: $1=reportfile, $2=applcation_name:

#!/usr/bin/sh

GEM_IFS=$IFS
while read appl_name1 two three user_name1
do
IFS=","
while read user_name2 appl_name2
do
if [ "$appl_name1" = "$appl_name2" ]
then
echo "$appl_name1 $user_name2 $two $three $user_name1"
continue
fi
done <$2
IFS=$GEM_IFS
done <$1


regards,
John K.
it would be nice if you always got a second chance
KapilRaj
Honored Contributor

Re: Scripting problem

cat appl|tr "," " " > /tmp/appl$$
for node in `cat /tmp/appl$$|awk '{print $2}'`
do
appl=`grep $node /tmp/appl$$|cut -f1`
cat report |sed "s;$node;$node $appl;g" >> /tmp/output
done
rm /tmp/appl$$

I hv'nt tested it , the order of report may get changed..


Kaps
Nothing is impossible
Keith Bryson
Honored Contributor

Re: Scripting problem

This works:

cat report1 | while read a b c d
do
echo "$a `grep $a appl | awk -F"," {'print $1'}` $b $c $d"
done

Have fun - Keith
Arse-cover at all costs
Peter Nikitka
Honored Contributor

Re: Scripting problem

Hi,

this short one should work:

nawk 'BEGIN {FS=",";while (getline <"appl" == 1) ap[$2]=$1;FS=" "}
{if(ap[$1]) $1=$1" "ap[$1]; print}' report

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
SureshKumar_2
Valued Contributor

Re: Scripting problem

hi dewa,

this is similar as kaps, but with little modified one...

#!/usr/bin/ksh
for name in `cat appl | awk -F"," {'print $2'}`
do
echo $name
appl=`grep $name appl| awk -F"," {'print $1'}`
echo $appl
cat report | grep ^$name |sed "s/$name/$name $appl/g" >> output
read
done

suresh
Things are very easy, when u know about it...
Dewa Negara_4
Regular Advisor

Re: Scripting problem

Hi All,

Thanks alot for your great help.

It should be fine now. I tested the script and it was working well.

Thanks again.

Best Regards,
Dewa
Santos
Peter Godron
Honored Contributor

Re: Scripting problem

Dewa,
if you can change the format of appl to:
s/^sihp8012 /sihp8012 oracle /
s/^sihp8026 /sihp8026 sap /
s/^sihp8020 /sihp8020 web /
then you can use the command:
sed -f appl report1 > report2
Regards,
Pete