我的完整路径是/data7/stmt_data16/pdf/RL/20170202/INLAND/641/K_EDGE1_641
我想从pdf
目录开始仅提取和打印路径,
即/pdf/RL/20170202/INLAND/641/K_EDGE1_641
。
那么如何用shell脚本中的sed或awk命令实现呢?
答案 0 :(得分:1)
如果你的shell是bash,你既不需要sed也不需要awk:
path='/data7/stmt_data16/pdf/RL/20170202/INLAND/641/K_EDGE1_641'
echo "${path#*/pdf/}"
RL/20170202/INLAND/641/K_EDGE1_641
这也会切断/pdf/
,但可以手动添加:
echo "/pdf/${path#*/pdf/}"
答案 1 :(得分:0)
也可以这样做:
sed -r 's#(.*)(/pdf/.*)#\2#' <<<"$yourpathvvar"
测试:
mypath="/data7/stmt_data16/pdf/RL/20170202/INLAND/641/K_EDGE1_641"
sed -r 's#(.*)(/pdf/.*)#\2#' <<<"$mypath"
/pdf/RL/20170202/INLAND/641/K_EDGE1_641
答案 2 :(得分:0)
grep
$ path='/data7/stmt_data16/pdf/RL/20170202/INLAND/641/K_EDGE1_641'
$ echo $path | grep -o '/pdf.*'
$ /pdf/RL/20170202/INLAND/641/K_EDGE1_641
-o
-print only match
答案 3 :(得分:0)
AWK
和match
函数 substr
方法:
path='/data7/stmt_data16/pdf/RL/20170202/INLAND/641/K_EDGE1_641'
echo "$path" | awk 'match($0, /\/pdf.*/) {print substr($0, RSTART, RLENGTH) }'
输出:
/pdf/RL/20170202/INLAND/641/K_EDGE1_641
SED
方法:
path='/data7/stmt_data16/pdf/RL/20170202/INLAND/641/K_EDGE1_641'
echo "$path" | sed -n 's/.*\(\/pdf.*\)/\1/p'
答案 4 :(得分:0)
这里有很多解决方案......用纯粹的bash再抓一个:
echo "${mypath/*pdf//pdf}"
答案 5 :(得分:0)
path='/data7/stmt_data16/pdf/RL/20170202/INLAND/641/K_EDGE1_641'
echo "$path" |awk -F '_data16' '{print $2}'
/pdf/RL/20170202/INLAND/641/K_EDGE1_641