Unable to pass a space inside a variable shell scripting

Login or Register for Dates, Times and to Reply

Thread Tools Search this Thread
# 1  
HP Unable to pass a space inside a variable shell scripting

Can anyone help me in solving this ?

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


# 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 ?
Moderator's Comments:
Mod Comment Using CODE tags is crucial when you need to preserve spacing in your posts.

Last edited by Don Cragun; 08-14-2013 at 10:56 AM.. Reason: Add CODE tags.
# 2  
echo "Wed Aug 4" | awk '{printf("%s %2s",$2,$3)}'

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

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.


Last edited by Scott; 08-14-2013 at 05:15 PM.. Reason: Please use code tags
# 4  
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)}'`

This User Gave Thanks to krishmaths For This Post:
# 5  
Thanks Krishmaths Smilie
# 6  
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"

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

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

Last edited by Don Cragun; 08-14-2013 at 11:12 AM.. Reason: Add way to get desired data from log file.
# 7  
Thanks Don Smilie

That is really a useful information.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #917
Difficulty: Easy
Unix time is also known as Epoch time.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Unable to pass value from .Shell script to .SQL file

Hi All, I am new to shell script. I am trying to pass value from .sh file to .sql file . But I am able to run the .sql file from .sh file with values in sql file. But I am unable to pass the values from .sh file. can some one please help to resolve this. here is my .sh file s1.sh ... (4 Replies)
Discussion started by: reddy298599
4 Replies

2. Shell Programming and Scripting

Unable to pass send command in shell script

I am trying to automate a testcase . I am installing some software and it waits for user input after displaying "Do you want to continue ? " I am trying to do this in shell scripting. #!/bin/bash #!/usr/bin/expect -f /usr/bin/expect << EOF spawn apt-get install openjdk-7-jdk expect "*Do you... (1 Reply)
Discussion started by: Abdul Navaz
1 Replies

3. Shell Programming and Scripting

Unable to pass shell script variable to awk command in same shell script

I have a shell script (.sh) and I want to pass a parameter value to the awk command but I am getting exception, please assist. diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff | awk... (2 Replies)
Discussion started by: Ashunayak
2 Replies

4. UNIX for Dummies Questions & Answers

Pass shell Variable to awk

Hello, May i please know how do i pass the shell variable to awk expression in the below script. It is returning null #!/bin/bash UNINUM=720922 UNINUM_DESC=`awk -F'|' -v UNINUM=$2 '/UNINUM/ {print $4}' datafile` echo $UNINUM_DESC datafile 4|First|720194|asdasdad 4|First|720735|asdasdsa... (8 Replies)
Discussion started by: Ariean
8 Replies

5. Shell Programming and Scripting

How to pass current year and month in FOR LOOP in UNIX shell scripting?

Hi Team, I have created a script and using FOR LOOP like this and it is working fine. for Month in 201212 201301 201302 201303 do echo "Starting the statistics gathering of $Month partitions " done But in my scripts the " Month " variable is hard-coded. Can you please any one... (3 Replies)
Discussion started by: shoan
3 Replies

6. Shell Programming and Scripting

unable to pass value to user prompt from calling shell script

This is my script structure main script calls configure script which needs to be run as a different user and the configure script calls my application installation script. the application instruction script prompts the user for a directory which I need to pass from my main or configure script. ... (4 Replies)
Discussion started by: cmastays
4 Replies

7. Shell Programming and Scripting

Pass shell variable to gnuplot

Hi I am plotting a series of CDFs using gnuplot using plot "data" u 1:(1./x.) smooth cumulative I am doing this over many files and I need to tune the x value to the number of lines that meets a particular condition. Is it possible to get the line count from shell using cat file | grep... (7 Replies)
Discussion started by: jamie_123
7 Replies

8. Shell Programming and Scripting

Not able to store command inside a shell variable, and run the variable

Hi, I am trying to do the following thing var='date' $var Above command substitutes date for and in turn runs the date command and i am getting the todays date value. I am trying to do the same thing as following, but facing some problems, unique_host_pro="sed -e ' /#/d'... (3 Replies)
Discussion started by: gvinayagam
3 Replies

9. Shell Programming and Scripting

nested double quota and white space inside variable

I have a question about nested double quotes. Any help is appreciated. Here are my commands on Mac OS. # string="Ethernet \"USB Ethernet\" \"Bluetooth DUN\" AirPort FireWire \"Bluetooth PAN\"" # echo $string Ethernet "USB Ethernet" "Bluetooth DUN" AirPort FireWire "Bluetooth PAN" #... (3 Replies)
Discussion started by: lindazhou
3 Replies

10. Shell Programming and Scripting

how to pass a variable to an update sql statement inside a loop

hi all, i am experiencing an error which i think an incorrect syntax for the where clause passing a variable was given. under is my code. sqlplus -s ${USERNAME}/${PASSWORD}@${SID} << END1 >> $LOGFILE whenever sqlerror exit set serveroutput on size 1000000 declare l_rc ... (0 Replies)
Discussion started by: ryukishin_17
0 Replies

Featured Tech Videos