# Some help for awk

**URL:** <https://community.unix.com/t/some-help-for-awk/153731>\
**Category:** Shell Programming and Scripting\
**Created:** [June 1, 2005, 11:22am UTC](https://community.unix.com/t/some-help-for-awk/153731 "2005-06-01T11:22:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![penguin-friend](https://community.unix.com/user_avatar/community.unix.com/penguin-friend/32/40_2.png) [@penguin-friend](https://community.unix.com/u/penguin-friend)\
**Post date:** [June 1, 2005, 11:22am UTC](https://community.unix.com/t/some-help-for-awk/153731/1 "2005-06-01T11:22:11Z")

</div>

Here is my problem:

i'm looking how to append at the end of a line a string.  
I'm working on a script to insert a new machine in the hostexinfo.cfg of nagios.  
Depending of what type of machine it is, it should be inserted at the right place:  
my file looks like this:  
[...]  
define hostextinfo{  
host\_name samba,kastor,f87,c1105,solferino,sirdata,sirappli,tfc627,ulysse  
icon\_image linux40.jpg  
vrml\_image linux40.png  
statusmap\_image linux40.gd2  
}

In fact, in order to determine where to insert the new server, it have a check on the "icon\_image".  
The input has decided the machine is a linux, so it should be added to the "host\_name" line.  
How can i achieve this?  
I have an idea but very heavy... using cat -n hostextinfo, then a grep to get the line number. Then a expr to get the line number of the first "define hostextinfo" just before and the following "}". i'm sure there is easier!!!  
Thanx for your help...

Jason

---

<div class="post-metadata">

**Author:** ![muthukumar](https://community.unix.com/letter_avatar/muthukumar/32/5_5575768a8748004e209b776fc1b2916d.png) [@muthukumar](https://community.unix.com/u/muthukumar)\
**Post date:** [June 2, 2005, 6:20am UTC](https://community.unix.com/t/some-help-for-awk/153731/2 "2005-06-02T06:20:12Z")

</div>

You can try for linux as,  
#!/bin/sh  
echo "Enter server name"  
read server

awk '/icon\_image/ { print $2 }' hostexinfo.cfg | grep -q 'linux'  
if [[$? -eq 0]]  
then  
LINUX=1  
fi

while read line;  
do  
echo $line | grep -q 'host\_name'  
if [[$? -eq 0 && $LINUX -eq 1]]  
then  
echo $line,$server  
fi  
done \< hostexinfo.cfg \> /tmp/newhostexinfo.cfg

# Safe usage  
mv hostexinfo.cfg hostexinfo.cfg.old  
mv /tmp/newhostexinfo.cfg hostexinfo.cfg

# exit  
exit 0

hth.

---

<div class="post-metadata">

**Author:** ![muthukumar](https://community.unix.com/letter_avatar/muthukumar/32/5_5575768a8748004e209b776fc1b2916d.png) [@muthukumar](https://community.unix.com/u/muthukumar)\
**Post date:** [June 2, 2005, 6:21am UTC](https://community.unix.com/t/some-help-for-awk/153731/3 "2005-06-02T06:21:28Z")

</div>

Specify your requirement other type of server's. Where the input servername has to be inserted?

hth.
