Sponsored Content
Full Discussion: set sub-string as variable
Top Forums UNIX for Dummies Questions & Answers set sub-string as variable Post 302437421 by tedy2808 on Thursday 15th of July 2010 05:57:05 AM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 02:38 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy