Set specific part in command output into variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Set specific part in command output into variable
# 1  
Old 08-15-2008
Set specific part in command output into variable

I am trying unsuccessfully to set into a variable a specific part of command output:
The command output will be as:
line 1: <varied>
line 2: 2 options:
option 1:
Set view: ** NONE **
or
option 2:
Set view: <different_name_of_views_always_without_spaces>

and I would like to get into the variable either the ** NONE ** or the name of the view (<different_name_of_views_always_without_spaces> in option 2).

What is the syntax to find that "field" into my variable?

Thanks a lot
# 2  
Old 08-15-2008
Something like:

Code:
VAR=`herecomesyourcmd| grep 'Set view: ** NONE **'`

If you want the other pattern, you'll have to exchange it.
# 3  
Old 08-15-2008
Thanks,
I tried the advised code as following:
echo "test123"
v1=`ct pwv | grep 'Set view: ** NONE **'`
echo $v1

and I get:
test123


(empty line after the test123)

1. Any idea?
2. In the 2nd option for the command output (Set view: <different_name_of_views_always_without_spaces>) the last part (<different_name_of_views_always_without_spaces>) is varied and may be any string, how can I catch that string?

Thanks a lot
# 4  
Old 08-15-2008
For getting nothing as output I guess the grep'ed pattern is wrong then. Best try it without assigning it to a variable if grep finds something on the command line.
For the other pattern you want to find.. can we sum it up that you simply want any output that starts with "Set view:" ?
# 5  
Old 08-15-2008
You need to backslash the asterisks or use fgrep -- asterisk means "the previous character zero or more times" (and doesn't match itself) in regex.
# 6  
Old 08-15-2008
OK, With fgrep it worked.

regarding the other pattern - What I want to be set into the variable is the next string without spaces (till next space or new line) right after "Set view: "
any suggestion?

Thanks a lot!

Last edited by orit; 08-15-2008 at 01:25 PM..
# 7  
Old 08-16-2008
Code:
v1=`ct pwv | sed -n 's/Set view: //p'`

As far as I can tell, the ** NONE ** and the regular case can be handled equivalently; by stripping off the "Set view: " prefix.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Save an specific part of a expect_out in a variable

I have a expect file like this #!/opt/tools/unsupported/expect-5.39/bin/expect spawn ssh -l user ip expect_after eof {exit 0} set timeout 10 log_file /report.txt expect "Password:" { send "pasword\r" } expect "$ " { send "date\r" } expect "$ " { send "readlink /somelink\r" } set... (7 Replies)
Discussion started by: bebehnaz
7 Replies

2. UNIX for Dummies Questions & Answers

Set Command to output a log of every command executed in the script

Hi Guys, I like to output every command executed in the script to a file. I have tried set -x which does the same. But it is not giving the logs of the child script which is being called from my script. Is there any parameters in the Set command or someother way where i can see the log... (2 Replies)
Discussion started by: mac4rfree
2 Replies

3. Shell Programming and Scripting

Parse output path to set variable

I am looking to parse a text file output and set variables based on what is cropped from the parsing. Below is my script I am looking to add this feature too. All it does is scan a certain area of users directories for anyone using up more than X amount of disk space. It then writes to the... (4 Replies)
Discussion started by: es760
4 Replies

4. Shell Programming and Scripting

set output of linux cmd as a variable in .exp

I am trying to make a script to take commands from a .txt file ( line by line) and pass it using send ( used in another function ) what i am trying to achieve is : set nol "`grep '' ${exp_path2}/cmdlist.txt | wc -l `" as in shell script nol=`grep '' $exp_path2/cmdlist.txt | wc -l` ... (0 Replies)
Discussion started by: dixyantar
0 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

set variable to command output

I'm hoping you guys can help me out here. I've been trying different methods to try and get what IW as hoping would be a fairly simple script but has turned into a pain. Bit of background - I am writing a script to check values in certain failes to ensure they are corerct. I'm runnign this on... (2 Replies)
Discussion started by: stuc
2 Replies

8. Shell Programming and Scripting

How to set IFS for a specific command

Hi there, I'm using two commands that need different IFS. The mysql command need IFS to include space because I'm given the mysql command as a variable: supernova:~# cat myscript IFS=' ' MYSQL="mysql -u user -ppassword database" $MYSQL -Ne "SELECT COUNT(1), MAX(id), MAX(name) FROM terminal"... (7 Replies)
Discussion started by: chebarbudo
7 Replies

9. UNIX for Dummies Questions & Answers

Set a variable from awk output

I have a file which I am processing using awk to spit out the following: export CLIENT=1 ; export USER=1 ; export METABASE=1 ; export TASK=1 ; export TOTAL=3 What i want to do now is execute that within the script so those variables are available to other commands. I've tried piping the... (3 Replies)
Discussion started by: Cranie
3 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