set sub-string as variable


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers set sub-string as variable
# 1  
Old 07-13-2010
set sub-string as variable

Hi all,

i really new in linux and just heard about shell scripting couple days ago..

i did exercises on linux in online tutorial but as a beginner, i'm facing problems in developing the script as there are errors that sometimes i dun have any idea on how to solve it.What i'm doing now is not homework, but i had been assigned to develop some system in linux.

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 seperate the information in the line and assign as variable..

below are my script:

(the script reside in test.sh)

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

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

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

guide me please..

Last edited by pludi; 07-14-2010 at 03:55 AM.. Reason: code tags, please...
# 2  
Old 07-14-2010
Hi,

Your first line (shebang) should be
Code:
#!/bin/sh

This:
Code:
name=${msg:0:8}

is not valid POSIX sh syntax. It is valid in bash or ksh93. If you want that you need to change the first line to the desired shell. If you want to use POSIX shell, have a look here to see what is or is not allowed:
Shell & Utilities: Detailed ToC
# 3  
Old 07-14-2010
set sub-string as variable

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?


Code:
#!/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"


Last edited by pludi; 07-14-2010 at 03:55 AM.. Reason: code tags, please...
# 4  
Old 07-15-2010
below are the scripts that i've edited based on my understanding:

Code:
#!/bin/sh
dir=/home/devusr02/nadiah/input/file_name.log

msg=`tail -1 $dir`
name=${msg%% Failed *}


status=Failed
if [ ${#name} -eq ${#msg} ]
then
# length of string is same as original message
# ie replace matched nothing - assume success string
# status=success
name=${msg%% successfully *}
status=Success
fi
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
echo "$msg"
echo "$name"
echo "$status"


the script output:


if success:

Code:
report_name successfully ran on Tue Jul 11 23:15:00 SST 2010.
report_name
Success
00:15:00

if failed:

Code:
ERROR: report_name is Failed on Tue Jul 11 23:15:00 SST 2010. ErrorCode : 500

ERROR: report_name is
Failed
00:15:00

BUT the output for name should be only "report_name" instead of "ERROR: report_name is" if failed

Moderator's Comments:
Mod Comment Use code tags, please...


---------- Post updated at 04:57 AM ---------- Previous update was at 03:51 AM ----------

one more question..

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

based on script below,

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

Last edited by Scott; 07-15-2010 at 07:01 AM.. Reason: Code tags
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Check if string variable is a subset of another string variable

Below is my ksh shell script where I need to check if variable fileprops is a subset of $1 argument. echo "FILE PROPERTY: $fileprops" echo "PARAMETER3: $1" if ; then echo "We are Good. $line FILE is found to be INTACT !! " else echo... (2 Replies)
Discussion started by: mohtashims
2 Replies

2. Shell Programming and Scripting

If a string variable value exists in a set of values

Can some one please help me with the syntax in shell script for the below : if $var1 exists in ('val1','val2','val3') I want to execute a set of commands if the value of var1 variable matches any one of the given string values. Please let me know if there are any other option to go by. ... (10 Replies)
Discussion started by: Pandee
10 Replies

3. UNIX for Dummies Questions & Answers

Comparing a String variable with a string literal in a Debian shell script

Hi All, I am trying to to compare a string variable with a string literal inside a loop but keep getting the ./testifstructure.sh: line 6: #!/bin/sh BOOK_LIST="BOOK1 BOOK2" for BOOK in ${BOOK_LIST} do if then echo '1' else echo '2' fi done Please use next... (1 Reply)
Discussion started by: daveu7
1 Replies

4. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

5. HP-UX

What is the use of command set -- and set - variable?

Hi, I am using hp unix i want to know the use of the following commands set -- set - variable thanks (4 Replies)
Discussion started by: gomathi
4 Replies

6. Shell Programming and Scripting

BASH - set specific user variable via string operators

Apologies for the utter triviality of this question, but we all have to start somewhere! I've also tried searching but this question is pretty vague so I didn't (a) really know what to search for or (b) get many relevant hits to what I did search for. Anyway, I'm in the process of self-teaching... (1 Reply)
Discussion started by: u5j84
1 Replies

7. Shell Programming and Scripting

Howto set string variable in TCL?

Anyone knows how to set variable in TCL ? let say i have a password variable and it will have different values. set variable and variable has different values like: xxxx yyyy zzzz (0 Replies)
Discussion started by: linuxgeek
0 Replies

8. Shell Programming and Scripting

how to find a particular string from a set of string output

Hi , I have a line by line output as follows,for example output of ls sample1 sample2 sample i need to check if this output contains the exact string sample.If i use grep , it will find out all strings that contain sample as a part of their word.I dont want to do a pattern matching... (11 Replies)
Discussion started by: padmisri
11 Replies

9. Shell Programming and Scripting

set variable with another variable? c shell

okay, this shouldn't be difficult but I can't figure it out. How can I set a variable with another variable. I have the following: foreach pe ($dir $sp) set tpe = `echo $pe | grep M` if ($tpe == M) then set ${$pe} = M <--- This doesn't work else endif end In this case what... (2 Replies)
Discussion started by: wxornot
2 Replies

10. UNIX for Dummies Questions & Answers

Export command giving Variable Name vs the Value set for the Variable

I'm having an issue when I export within my program. I'm getting the variable name, not the variable value. I have a configuration file (config.txt) that has the values of the variables set as so: set -a export ARCHIVEPOSourceDir="/interfaces/po/log /interfaces/po/data" export... (2 Replies)
Discussion started by: ParNone
2 Replies
Login or Register to Ask a Question