search the output line and save in variable

2019-07-23 03:46发布

How to search word of the output of a shell script and save in other variable .

Below command will display the list of baselines in my view .

cmd :cleartool lsstream -fmt "%[found_bls]NXp\n" -view $VIEW_NAME

Output :

baseline:MHC_BUILDTREE1.0.1
baseline:JEPG_DUIF_CI
baseline:MOR_BuildTree_BLD_I.0.1

I need to search the line which contains "MOR_BuildTree" and that output line i have to save in one variable to execute the rest of the commands . Can any one give the suggestion ?

3条回答
一夜七次
2楼-- · 2019-07-23 04:29

Isn't this a simple piece shell scripting?

variable=$(cmd :cleartool lsstream -fmt "%[found_bls]NXp\n" -view $VIEW_NAME |
           sed -n '/MOR_Buildtree/s/^baseline://p')

The -n options means 'do not print by default'. The command looks for your name, removes the leading baseline: and prints it.

查看更多
何必那么认真
3楼-- · 2019-07-23 04:30

Not exactly sure what you are asking, but to set a variable to the result of a command, you can use:

var=`command`

or you can pipe your command to some filter to reformat it as required.

command | some_filter | another filter

you can also combine the two:

var=`command | some_filter | another filter`
查看更多
ら.Afraid
4楼-- · 2019-07-23 04:48

Here is what I just tested to get one baseline out of all the foundation baselines:

$ res=`cleartool lsstream -fmt "%[found_bls]CXp" -view $VIEW_NAME | tr -s " " "\\012" | grep $yourBaselineName| sed -e "s/.*://" -e "s/@.*//"`

(set $yourBaselineName to the appropriate name you want to extract)

The main difficulty comes from the fact an lsstream -fmt "%[found_bls]CXp"will list all foundation baselines on the same output line (so the output is only one line of baseline names, separated by comma).
You need to split the line into multiple ones first.
See "How to split one string into multiple strings in bash shell?": that is the tr -s " " "\\012" part of the above command.

Then you need to remove what precede and follow the baseline name:

 baseline:aBaselineName@/vobs/YourPVob

The sed part of the above command can do that:

sed -e "s/.*://" -e "s/@.*//"

(two regular expressions remove everything before the ':' and after the '@')

查看更多
登录 后发表回答