pj013
pj013

Reputation: 1419

sed to extract pattern between a substring and first occurance of a substring in a string - get relative path

I have the following line:

SF:/Users/someuser/Documents/workspace/project/src/app/somejavascriptfile.js ./coverage/coveragereport.info

I am trying to get the relative path from the above absolute path using sed in bash.

Tried a bunch of combinations but none of the regexes seem to work appropriately.

This is what I tried:

ABSOLUTEPATH=SF:$(echo $PWD | sed 's_/_\\/_g')
sed -i '' 's/.*'$ABSOLUTEPATH'/SF:' ./coverage/coveragereport.info

but this doesn't work as intended.

Any idea?

Upvotes: 0

Views: 96

Answers (1)

codeforester
codeforester

Reputation: 43039

Why not do it directly, without having to use an intermediate variable:

sed -E -i '' "s@(SF:)$PWD@\1@" ./coverage/coveragereport.info

or

sed -i '' "s@SF:$PWD@SF:@" ./coverage/coveragereport.info

What you are doing right now is fine, except that it needs a / at the end of sed expression:

ABSOLUTEPATH=SF:$(echo $PWD | sed 's_/_\\/_g')
sed -i '' 's/.*'$ABSOLUTEPATH'/SF:/' ./coverage/coveragereport.info

Upvotes: 1

Related Questions