Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

awk script to add text to a file 2

Status
Not open for further replies.

tumichaelf

IS-IT--Management
May 17, 2011
33
US
Hello,

I am new to awk, and while I can do using other scripting tools, I really want to learn how to properly use awk.

I have a file (/var/tmp/nrpe.config) that has a line
"command[check_disk]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -x /shared -x /software"

that I need to add text to.

The text I need to add is "-x /boot" so that the final line looks like

"command[check_disk]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -x /shared -x /software -x /boot"

I need to do this to 49 servers (/var/tmp/ is my test area, not the final area) and I can easily send a 1 line command across to all of the servers using perform.

Any help would be appreciated.
 
1 important thing to note, not every file contains the "-x /shared -x /software" so it needs to find "command[check_disk]" and add the " -x /boot" to the end (note the space is needed).
 
Hi

tumichaelf said:
I really want to learn how to properly use awk.
For those who really wants to learn Awk we have forum271 .
Code:
awk '/^command\[check_disk\]=/{$0=$0 " -x /boot"}1' /var/tmp/nrpe.config > /var/tmp/nrpe.config.new
Note that that the above will append " -x /boot" each time it will run, so better check its existence :
Code:
awk '/^command\[check_disk\]=/&&!/-x \/boot/{$0=$0 " -x /boot"}1' /var/tmp/nrpe.config > /var/tmp/nrpe.config.new
Also note that Awk will not alter the input file, so you will have to save its output in a temporary file and then rename it. ( Sed, Perl, Ruby and some other tools could do an in place editing. )


Feherke.
 
could you please provide 2 additional things for me

1) an explanation (step by step) as to what the awk command just did (fyi it ran perfectly)?

2) how to do the same thing with sed?

I have used basic bash and expect scripting in the past, but never have had a huge call for scripting myself (even though I try to whenever I can).
 
Hi

Code:
awk '
/^command\[check_disk\]=/  [gray]# if current record matches this regular expression...[/gray]
&&                         [gray]# ... and...[/gray]
!                          [gray]# ... not...[/gray]
/-x \/boot/                [gray]# ... matches this one[/gray]
{                          [gray]# then execute this block[/gray]
  $0=$0 " -x /boot"        [gray]#   set the current record to current record plus that string[/gray]
}                          [gray]# end of block to be executed[/gray]
1                          [gray]# if true ( as the action part is missing, will perform the default one : print the current record )[/gray]
' /var/tmp/nrpe.config > /var/tmp/nrpe.config.new
Code:
sed -i '/^command\[check_disk\]=/{/-x \/boot/!s/$/ -x \/boot/}' /var/tmp/nrpe.config

perl -i -pe 's/$/ -x \/boot/ if/^command\[check_disk\]=/&&!/-x \/boot/' /var/tmp/nrpe.config

ruby -i -pe 'sub /$/," -x /boot"if/^command\[check_disk\]=/&&!/-x \/boot/' /var/tmp/nrpe.config
Note that Perl and Ruby could do it without regular expressions too, which would be abit faster.


Feherke.
 
This is excellent, I appreciate your help on this :)
 
1 more question (if you don't mind)

in all 4 ways listed, how would I change

"command[check_disk]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -x /shared -x /software -x /boot"

to

"command[check_disk]=/usr/local/nagios/libexec/check_disk -w 15% -c 10% -x /shared -x /software -x /boot"

that was 20% --> 15%?
 
for sed I wrote, tested and verified that
Code:
sed -i 's/check_disk\ \-w\ 20\%/check_disk\ \-w\ 15\%/g' /usr/local/nagios/etc/nrpe.cfg
works great.

For perl I wrote, tested and verified that
Code:
perl -i -pe 's/check_disk\ \-w\ 20\%/check_disk\ \-w\ 15\%/g' /var/tmp/nrpe.cfg1
works great.

For ruby I wrote (ruby is not installed on the machine)
Code:
ruby -i -pe 's/check_disk\ \-w\ 20\%/check_disk\ \-w\ 15\%/g' /var/tmp/nrpe.cfg
and was hoping you could tell me if this was correct or not.

As for awk, I am not sure how to write this. Any help would be appreciated.
 
Hi
[ul]
[li]The space, dash ( - ) and percent sign ( % ) are not metacharacters, so they need no escaping.[/li]
[li]The [tt]s///[/tt] command's [tt]g[/tt] ( global ) modifier means to repeat searching for matches until the string ends. Unless you expect to find more than one occurrences of that "check_disk -w 20%" in one line, putting the regular expression engine to continue the search after the first match is just wasting the time.[/li]
[li]In Ruby the substitution is done by the [tt]sub()[/tt] method, its first parameter is a regular expression and the second a string. The [tt]g[/tt] modifier can not be passed, instead there is a [tt]gsub()[/tt] variant of the method.[/li]
[/ul]
Code:
sed -i 's/check_disk -w 20%/check_disk -w 15%/' /var/tmp/nrpe.cfg

perl -i -pe 's/check_disk -w 20%/check_disk -w 15%/' /var/tmp/nrpe.cfg

ruby -i -pe 'sub /check_disk -w 20%/,"check_disk -w 15%"' /var/tmp/nrpe.cfg
Regarding the Awk solution, there is nothing left to add to the solution in thread271-1655335 .


Feherke.
 
Hey Feherke,

Thanks again for all of your help, and not just for giving me the commands, but for helping me to understand how to do this going forward.

I was allowed to give you another star today ;)

Michael
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top