Reputation: 11
Suppose I have this piece of text:
GINC/weary/crazy/RMSD=2316
I was hoping there was a way to extract what lies between the 2nd forward slash /
and /RMSD=2316
. In other words, I want to look for the string /RMSD=2316
and extract whatever lies between it and the forward slash that precedes it.
Upvotes: 1
Views: 419
Reputation: 77105
With awk
:
awk -F/ '{for(i=1;i<=NF;i++) if($i=="RMSD=2316") print $(i-1)}' t
Upvotes: 2
Reputation: 200293
Perl solution:
str=GINC/weary/crazy/RMSD=2316
echo "$str" | perl -ne 'print "$1\n" if m|.*?/.*?/(.*)/RMSD=2316|'
Upvotes: 1
Reputation: 5835
You can use sed
:
sed 's/.*\/\([^\/]\+\)\/RMSD=2316/\1/'
This matches all text leading up to a forward-slash, captures all non-forward-slash characters leading up to /RMSD=2316
, and replaces the entire match with the captured characters.
Upvotes: 1
Reputation: 23364
Since you have marked this bash
var='GINC/weary/crazy/RMSD=2316'
var1=${var%/RMSD=2316}
echo $var1
echo ${var1##*/}
Upvotes: 1
Reputation: 97948
Using sed, without any positional assumptions:
echo 'GINC/weary/crazy/RMSD=2316' | \
sed -n 's!.*/\([^/]*\)/RMSD=2316.*!\1!p'
Upvotes: 2
Reputation: 3534
What about
cut -d/ -f3
-d/
is the slash delimiter
-f3
means third column
Upvotes: 1