简体   繁体   English

sed提取字符串中的子字符串和子字符串的首次出现之间的模式-获取相对路径

[英]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. 我正在尝试使用bash中的sed从上述绝对路径获取相对路径。

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? 任何想法?

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: 您现在正在执行的操作很好,只是在sed表达式的末尾需要一个/

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM