Unable to pass a space inside a variable shell scripting | Unix Linux Forums | HP-UX

  Go Back    


HP-UX HP-UX (Hewlett Packard UniX) is Hewlett-Packard's proprietary implementation of the Unix operating system, based on System V.

Unable to pass a space inside a variable shell scripting

HP-UX


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 08-14-2013
Kits Kits is offline
Registered User
 
Join Date: Sep 2011
Last Activity: 26 December 2013, 5:07 AM EST
Posts: 11
Thanks: 1
Thanked 0 Times in 0 Posts
HP Unable to pass a space inside a variable shell scripting

Can anyone help me in solving this ?


Code:
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:


Code:
# 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:


Code:
# 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:


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

or


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

I need the o/p like below:


Code:
Aug  4

which has two spaces in between. Can somebody help ?
Moderator's Comments:
Using CODE tags is crucial when you need to preserve spacing in your posts.

Last edited by Don Cragun; 08-14-2013 at 08:56 AM.. Reason: Add CODE tags.
Sponsored Links
    #2  
Old 08-14-2013
krishmaths krishmaths is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 30 June 2014, 4:32 AM EDT
Location: Chennai, India
Posts: 443
Thanks: 22
Thanked 70 Times in 68 Posts

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

Sponsored Links
    #3  
Old 08-14-2013
Kits Kits is offline
Registered User
 
Join Date: Sep 2011
Last Activity: 26 December 2013, 5:07 AM EST
Posts: 11
Thanks: 1
Thanked 0 Times in 0 Posts
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:


Code:
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

Last edited by Scott; 08-14-2013 at 03:15 PM.. Reason: Please use code tags
    #4  
Old 08-14-2013
krishmaths krishmaths is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 30 June 2014, 4:32 AM EDT
Location: Chennai, India
Posts: 443
Thanks: 22
Thanked 70 Times in 68 Posts
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


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

The Following User Says Thank You to krishmaths For This Useful Post:
Kits (08-14-2013)
Sponsored Links
    #5  
Old 08-14-2013
Kits Kits is offline
Registered User
 
Join Date: Sep 2011
Last Activity: 26 December 2013, 5:07 AM EST
Posts: 11
Thanks: 1
Thanked 0 Times in 0 Posts
Thanks Krishmaths
Sponsored Links
    #6  
Old 08-14-2013
Don Cragun's Avatar
Don Cragun Don Cragun is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 23 October 2014, 6:00 AM EDT
Location: San Jose, CA, USA
Posts: 4,885
Thanks: 182
Thanked 1,641 Times in 1,392 Posts
First, this all seems to be a very convoluted way of getting output from the command:

Code:
date '+%b %e'

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

Code:
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:

Code:
echo "$p"

not:

Code:
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:

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

or:

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


Last edited by Don Cragun; 08-14-2013 at 09:12 AM.. Reason: Add way to get desired data from log file.
Sponsored Links
    #7  
Old 08-14-2013
Kits Kits is offline
Registered User
 
Join Date: Sep 2011
Last Activity: 26 December 2013, 5:07 AM EST
Posts: 11
Thanks: 1
Thanked 0 Times in 0 Posts
Thanks Don

That is really a useful information.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to pass bash variable inside EOF? Akshay Hegde Shell Programming and Scripting 2 04-12-2013 02:32 AM
unable to pass value to user prompt from calling shell script cmastays Shell Programming and Scripting 4 07-24-2012 04:13 PM
Not able to store command inside a shell variable, and run the variable gvinayagam Shell Programming and Scripting 3 11-19-2010 05:59 AM
nested double quota and white space inside variable lindazhou Shell Programming and Scripting 3 05-18-2010 07:49 PM
how to pass a variable to an update sql statement inside a loop ryukishin_17 Shell Programming and Scripting 0 04-14-2010 02:02 AM



All times are GMT -4. The time now is 06:07 AM.