Reputation: 440
I have a command which when executed gives following outputs
$ cleartool pwv
Working directory view: abhijeet_trialProj_r2.4-phase1_ba_abhijeet_branch
Set view: abhijeet_trialProj_r2.4-phase1_ba_abhijeet_branch
$ cleartool pwv
Working directory view: ** NONE **
Set view: abhijeet_trialProj_r2.4-phase1_ba_abhijeet_branch
$ ct pwv
Working directory view: ** NONE **
Set view: ** NONE **
I am using this command "cleartool pwv" in a shell script.
view_used=`cleartool pwv`
Thus $view_used is assigned the string "Working directory view: ** NONE ** Set view: abhijeet_trialProj_r2.4-phase1_ba_abhijeet_branch"
I need to retrieve two values from $view_used such that
working_dir_view="**** NONE ****" or "abhijeet_trialProj_r2.4-phase1_ba_abhijeet_branch"
set_view = "**** NONE ****" or "abhijeet_trialProj_r2.4-phase1_ba_abhijeet_branch"
Could some body help me with this?
Thanks in advance.
Upvotes: 0
Views: 419
Reputation: 342373
if you have awk,
var=$(cleartool pwv |awk -F":" '/Working directory view/{
t=$2
}/Set view/{s=$2}
END{
print t,s
}
')
set -- $var
echo $1
echo $2
with shell(bash) scripting
while IFS=":" read -r a b
do
case "$a" in
*"Working directory"* ) w="$b";;
*"Set view"* ) s="$b";;
esac
done <"file1"
echo "$w"
echo "$s"
Upvotes: 1
Reputation: 113906
If you have awk:
working_dir_view=$(cleartool pwv | awk '
/Working directory view/ {gsub(/.*:\s+/,"");print $0}
')
set_view=$(cleartool pwv | awk '
/Set view/ {gsub(/.*:\s+/,"");print $0}
')
If you really want to do it from a string in the variable:
working_dir_view=$(echo $view_used | awk '
/Working directory view/ {gsub(/.*:\s+/,"");print $0}
')
set_view=$(echo $view_used | awk '
/Set view/ {gsub(/.*:\s+/,"");print $0}
')
In a script file it's probably more maintainable to refactor them as a function:
getView(){
echo $2 | awk "/$1 view/ {gsub(/^.*:[ \t]+/,\"\");print\$0}"
}
So now you can do:
working_dir_view=`getView "Working directory" "$view_used"`
set_view=`getView Set "$view_used"`
Upvotes: 0
Reputation: 881543
Assuming they're always of that format (two lines, same order, colon separator):
working_view="$(ct pwv | head -1 | sed 's/[^:]*: //')"
set_view="$(ct pwv | tail -1 | sed 's/[^:]*: //')"
If you must use ${view_used}
(in other words, if you don't want to call the ClearCase tool twice), simply substitute it as follows:
working_view="$(echo "${view_used}" | head -1 | sed 's/[^:]*: //')"
set_view="$(echo "${view_used}" | tail -1 | sed 's/[^:]*: //')"
Upvotes: 0