1844882 Members
3001 Online
110233 Solutions
New Discussion

AWK Shell script problem

 
SOLVED
Go to solution
Peter Remirez
Occasional Advisor

AWK Shell script problem

Hi,
I would like to insert a line:
"auth.warn @mickey.msc.com" into the file /etc/syslog.conf. This line is to be inserted after the first opening comment blocks:
#ident "@(#)syslog.conf 1.393/12/09 SMI" /* HP11.0 */
#
#
# syslog configuration file.
#
# This file is processed by m4 so be careful to quote (`') names
# that match m4 reserved words. Also, within ifdef's, arguments
# containing commas must be quoted.
#
# Note: Have to exclude user from most lines
# so that user.alert and user.emerg are not
# included, because old sendmails
#will generate them for debugging #information. If you
#have no 4.2BSD based systems doing network
#logging, you
#can remove all the special cases for "user"
#logging.

Below is my script which is intended to insert the line "auth.warn @mickey.msc.com" into the /etc/syslog.conf file.


#!/bin/sh
gawk '/#/{
if(startHash==0){
startHas=1;
};
print
};
$0 !~/#/{
if(startHash==1){
startHash=2;
print"auth.warn @mickey.msc.com"
}
else{
print $0
}
}' syslog.conf

Unfortunately, the "auth.warn @mickey.msc.com" does not get printed into the file /etc/syslog.conf.

I could have done:
echo "auth.warn mickey.msc.com" >> /etc/syslog.conf
which would place this entry at the bottom most of the file.

However, I would like to have this entry at the very beginning of the /etc/syslog.conf i.e after the 1st comment blocks of /etc/syslog.conf.

Could someone show me how it's to be done?

Thanks in advance.
9 REPLIES 9
Ian Dennison_1
Honored Contributor

Re: AWK Shell script problem

if(startHash==0){
startHas=1;
};

You are missing the 'h' at the end of the variable name! Therefore the variable you test (startHash) never gets set!

Share and Enjoy! Ian

Building a dumber user
Michael Kelly_5
Valued Contributor

Re: AWK Shell script problem

Hi Peter,
first of all there is a typo in the script (startHas=1 instead of startHash=1) which will definitely prevent it from working.
Secondly, as written gawk will write its output to stdout.
You could redirect the output to something like /etc/syslog.conf-new, and then
mv /etc/syslog.conf /etc/syslog.conf-old
mv /etc/syslog.conf-new /etc/syslog.conf

HTH,
Michael.
The nice thing about computers is that they do exactly what you tell them. The problem with computers is that they do EXACTLY what you tell them.
Peter Remirez
Occasional Advisor

Re: AWK Shell script problem

Ian,
Unfortunately it still did not print the line "auth.warn @mickey.msc.com" into the /etc/syslog.conf file.

Any further ideas?

Thanks
Peter Remirez
Occasional Advisor

Re: AWK Shell script problem

Michael,
I tried what you suggested, but it still did not produce the result:
gawk '/#/{
if(startHash==0){
startHash=1;
};
##CHANGED THIS LINE
print > syslog.conf.1
};
$0 !~/#/{
if(startHash==1){
startHash=2;
#print"auth.warn @pgldev01.png.intel.com"
echo "auth.warn @pgldev01.png.intel.com" >> syslog.conf.1

## ADDED THIS LINE
cp syslog.conf.1 syslog.conf
}
else{
print $0
}
}' syslog.conf

Instead it produced the error:
gawk: cmd. line:5: print > syslog.conf.1
gawk: cmd. line:5: ^ parse error
gawk: cmd. line:11: echo "auth.warn @pgldev01.png.intel.com" >> syslog.conf.1
gawk: cmd. line:11: ^ parse error
gawk: cmd. line:12: cp syslog.conf.1 syslog.conf
gawk: cmd. line:12: ^ parse error
gawk: cmd. line:12: cp syslog.conf.1 syslog.conf
gawk: cmd. line:12: ^ parse error

Could you show me how's to be done?

Thanks
Ian Dennison_1
Honored Contributor
Solution

Re: AWK Shell script problem

#!/usr/bin/sh

cat /tmp/testfile |awk 'BEGIN {found=0} found == 0 && $0 !~ /^#/ {print "auth.warn @mickey.msc.com";print $0 ;found=1}
found != 0 || $0 ~ /^#/ {print $0}' >/tmp/testfile2

cat /tmp/testfile2

This code works. I think the problem was your code is too complex for the job it needs to do. Share and Enjoy! Ian

Output from test code as follows,...

# comment block 1
# end comment block 1
auth.warn @mickey.msc.com


# comment block 2
# comment block 2a

# comment block 3
# comment block 3a

Building a dumber user
Michael Kelly_5
Valued Contributor

Re: AWK Shell script problem

Here is my version:

Michael.

#!/bin/sh
gawk '/#/{
if(startHash==0){
startHash=1;
};
print
};
$0 !~/#/{
if(startHash==1){
startHash=2;
print"auth.warn @mickey.msc.com"
# Now print the original line as well
print $0
}
else{
print $0
}
}' syslog.conf >syslog.conf-new
mv syslog.conf syslog.conf-old
mv syslog.conf-new syslog.conf
The nice thing about computers is that they do exactly what you tell them. The problem with computers is that they do EXACTLY what you tell them.
rachel_11
Advisor

Re: AWK Shell script problem

here is mw awkfile:
BEGIN {
p=0
}

/^#/ {p++ }
{if (p==0) {print "auth.warn@mickey.msc.com "; p=2
}}
{if (p==1) p=0}
{print $0
}

run:
awk -f awkfile /etc/syslog.conf
and redircet the output
Peter Remirez
Occasional Advisor

Re: AWK Shell script problem

Michael, Rachel & Ian,

I tried the solution from Michael and the changes are now reflected in the /etc/syslog.conf file.

However, the comment block beginning with "#" are not reflected in the new file or new output.

Could anyone of you show me how I could preserve all comment blocks in the /etc/syslog.conf file once the line auth.warn @mickey.msc.com
is included?

Thanks

PS: Attached is the /etc/syslog.conf file after the inclusion of the line:
auth.warn @mickey.msc.com

Note that the "#" are not present compared to the original.
Ian Dennison_1
Honored Contributor

Re: AWK Shell script problem

try setting the firsy occurence of "print" by itself to be "print $0".

Share and Enjoy! Ian
Building a dumber user