set sub-string as variable (linux)

Unitech Pt630d mobile computer, batch, 1...
July 13, 2010 at 19:44:37
Specs: Windows XP, 2gb
Hi all,

i appreciate if there are people that willing to guide me on this matter.

my problem currently is on sub-string matter, where i need to read the line from file/directory and based on the line retrieved,i need to separate the information in the line and assign as variable..

below are my script: (in shell scripting)

(the script reside in test.sh)

!/bin/sh
dir=/home/user/input/test.log #(this is the input file)

msg=`tail -1 $dir`
#(it will take only the last line as log file will be updated every day)

echo "$msg"

thus, the output something like this:

file_name successfully ran on Mon Jul 12 23:15:00 SST 2009.


now, what i need to do is to extract certain information from that line which is the name, date, time and the status

the desired output is:

name date time status
file_name Mon Jul 12 23:15:00 success

so,my next step is to identify the sub-string and assigned as variable first in order to parse the info and output it.

thus,my script is:

!/bin/sh
dir=/home/user/input/test.log #(this is the input file)

msg=`tail -1 $dir`
#(it will take only the last line as log file will be updated every day)

sub= $msg
name=${msg:0:8}

echo "$msg"
echo "name"


but i received error message which is

./test.sh[10]: name=${msg:0:8}: The specified substitution is not valid for this command.

guide me please..




See More: set sub-string as variable (linux)

Report •

#1
July 13, 2010 at 20:39:31
your shebang is wrong, it should be

#!/bin/bash

/bin/sh is probably linked to some other shell. If you have bash, then use bash

GNU win32 packages | Gawk


Report •

#2
July 13, 2010 at 22:17:03
ok...i've edited my script and i got my desired output..
but the position of the string is always changes..Thus the script is no longer efficient.It may output wrong result. i'm thinking of creating a script that can read the exact sub-string that i wanted...let say from the whole line, i want to capture only "my file name"..instead of i have to set the position.

maybe in the log file, the last line has long/many words,so i just wanted to capture certain word in order for me to assign it as variable..
i've tried to search and did some try an error but still doesn't work.. any idea?


#!/bin/sh
dir=/home/user/input/test.log

#cat $dir

msg=`tail -1 $dir`
sub=`echo ${msg:6:23}`
echo "$sub"

#name=${msg:0:21}


echo "$msg"
#echo "$name"
Edit/Delete Message


Report •

#3
July 13, 2010 at 23:31:38
you should just show some different samples of the strings that may occur in your log file, and what you want to get as final output

GNU win32 packages | Gawk


Report •

Related Solutions

#4
July 13, 2010 at 23:38:02
here are the sample:

example of output 1;
1)
report_name successfully ran on Tue Jul 16 23:10:00 SST 2010.

example of output 2;
2)
ERROR:report_name successfully ran on Tue Jul 11 23:10:00 SST 2010.


Report •

#5
July 15, 2010 at 01:50:43
tanx everyone.. script runs well!

Report •

#6
July 15, 2010 at 02:55:59
one more question..

i wonder if is it possible if the date can be set as : 11 Jul 2010

based on script below,

datetime=${msg##*ran on } # remove "ran on " and all proceeding
datetime=${datetime%% SST *} # remove " SST " and all following
time=${datetime##* } # remove last space and all proceeding
date=${datetime% *} # remove last space and all following

the output is : "Tue Jul 11"

is there any other easier way to make the date as 11 Jul 2010 instead of i need to do step by step and combine the all the variable for example : echo "$day" "$month" "$year" ??

in addition, can someone guide me on how to eliminate day in front of the month " Tue Jul 11" as the day is changing from monday-sunday..

thanks.


Report •

Ask Question