Unable to pass a space inside a variable shell scripting

Can anyone help me in solving this ?

p=`date`
e=`echo $p | awk '{print $2,$3}'`
# echo $p
Wed Aug 4 12:00:08 IST 2013

but when I am echoing the value of e it is giving me with one space. As shown below:

# echo $e
Aug 4

I need this value to be exact as found in /var/adm/syslog/syslog.log file. When I tail last line of syslog file, I get like this:

# tail -1 /var/adm/syslog/syslog.log
Aug  4 12:02:54 pairdb01 emcp_mond: PP daemon: Error: Unable to start.

and after extracting the first two fields I get the value with only one space.
As shown below:

# e=`echo $p | awk '{print $2,$3}'`
# echo $e
Aug 4

or

# c=`tail -1 /var/adm/syslog/syslog.log | awk '{print $1,$2}'`
# echo $c
Aug 4

I need the o/p like below:

Aug  4

which has two spaces in between. Can somebody help ?

echo "Wed Aug 4" | awk '{printf("%s %2s",$2,$3)}'

Thanks Krishmaths,

However, I need flexible output not confined to one date. Like if the dates are 1 - 9 I can grep the first two values from date including the two spaces and after 9 i.e. from 10 - 31 I don't need this variable.

Actually I have made a script which will throw a mail if any error or failure messages are encountered in /var/adm/syslog/syslog.log file. The script will automatically check the date and grep the errors|failures from today's date and mail us.

The sample script is below:

p=`date`
e=`echo $p | awk '{print $2,$3}'`
#tail -1 /var/adm/syslog/syslog.log | cut -c 1-6 > /tmp/val.txt
grep -i "$e" /var/adm/syslog/syslog.log > /tmp/temp.txt | egrep -i "error|failure" /tmp/temp.txt > /tmp/err.txt

cat /tmp/utilz.txt /tmp/err.txt > /tmp/status.txt
mailx -s "$sub" $email < /tmp/status.txt

The script is working fine for dates greater than 9 but not for 1 to 9.

Thanks,
Ankit

The above awk code I posted works for both single digit and double digit dates. Please try changing your code by replacing e=`echo $p | awk '{print $2,$3}'` with the following

e=`echo $p | awk '{printf("%s %2s",$2,$3)}'`
1 Like

Thanks Krishmaths :slight_smile:

First, this all seems to be a very convoluted way of getting output from the command:

date '+%b %e'

Second, if you save the output from the above command in a variable:

p=$(date '+%b %e')

and then print that value using echo without quotes, you are throwing away the fact there the date command will put two spaces between the abbreviated month name and the day of the month for the 1st nine days of every month. If you want to preserve the spacing, use:

echo "$p"

not:

echo $p

There is a big difference between being able to pass a space in a variable and getting the shell to recognize that two spaces are different that one space when you subject the contents of that variable to word splitting.

To get what you want out of the log file, there are lots of ways to do it depending on your shell. With any standards conforming shell (such as bash or ksh), try:

tail -n 1 /var/adm/syslog/syslog.log|(
    read abmon dom
    printf "%s %2s" $abmon $dom
)

or:

tail -n 1 /var/adm/syslog/syslog.log|awk '{print substr($0, 1, 6)}'

Thanks Don :slight_smile:

That is really a useful information.