1

需要 cut/awk 脚本来截断“/branches/testbranch/”并获取文件的目录路径但忽略目录

/branches/testbranch/abc/efg/hij/klm/casestudy
/branches/testbranch/abc/efg/hij/klm/casestudy/testBlocksCargo.java
/branches/testbranch/abc/efg/hij/klm/casestudy/testBlocksCollection.java
/branches/testbranch/abc/efg/hij/klm/casestudy/testBlocksPrimaryKey.java
/branches/testbranch/claims/returns/policy/checkmate/casestudy
/branches/testbranch/claims/returns/policy/checkmate/casestudy/testBlocksGenDAO.java
/branches/testbranch/claims/returns/policy/checkmate/casestudy/testIndexGenDAO.java

期望的输出:

abc/efg/hij/klm/casestudy
abc/efg/hij/klm/casestudy
abc/efg/hij/klm/casestudy
claims/returns/policy/checkmate/casestudy
claims/returns/policy/checkmate/casestudy

我的脚本:

while read line
do
      change_directory=`echo "$line" | awk -F'/[^/]*$' '{sub("/branches/testbranch/","",$1)}$0=$1'`
      echo "$change_directory"
done <"test

脚本输出:

 abc/efg/hij/klm
 abc/efg/hij/klm/casestudy
 abc/efg/hij/klm/casestudy
 abc/efg/hij/klm/casestudy
 claims/returns/policy/checkmate
 claims/returns/policy/checkmate/casestudy
 claims/returns/policy/checkmate/casestudy

顺便说一句,目录和分支详细信息发生了变化

4

5 回答 5

2

我认为这就是你想要的(它至少符合你的预期输出):

一种 Unix 方法:

# Strip leading dirs based on char count
$ fgrep . file | cut -c22- | xargs -n1 dirname
abc/efg/hij/klm/casestudy
abc/efg/hij/klm/casestudy
abc/efg/hij/klm/casestudy
claims/returns/policy/checkmate/casestudy
claims/returns/policy/checkmate/casestudy

# Strip leading dirs based on number of dirs 
$ fgrep . file | cut -d/ -f4- | xargs -n1 dirname
abc/efg/hij/klm/casestudy
abc/efg/hij/klm/casestudy
abc/efg/hij/klm/casestudy
claims/returns/policy/checkmate/casestudy
claims/returns/policy/checkmate/casestudy

一种awk方法:

$ awk '/[.]/{NF--;print substr($0,22)}' FS=/ OFS=/ file
abc/efg/hij/klm/casestudy
abc/efg/hij/klm/casestudy
abc/efg/hij/klm/casestudy
claims/returns/policy/checkmate/casestudy
claims/returns/policy/checkmate/casestudy
于 2013-06-19T19:38:44.883 回答
1

使用 GNU Find 和 GNU Sed

找到testbranch的所有文件,然后使用 sed 去除您希望丢弃的路径名的前导部分。

find /branches/testbranch -type f | sed 's!/branches/testbranch/!!'

其他版本的 find 和 sed 可能需要也可能不需要修改此管道。YMMV。

于 2013-06-19T19:37:55.963 回答
1

一种方法awk

 awk -F/ '$NF~/\./{for(i=4;i<(NF-1);i++) printf "%s/", $i; print $(NF-1)}' test

测试:

$  awk -F/ '$NF~/\./{for(i=4;i<(NF-1);i++) printf "%s/", $i; print $(NF-1)}' test
abc/efg/hij/klm/casestudy
abc/efg/hij/klm/casestudy
abc/efg/hij/klm/casestudy
claims/returns/policy/checkmate/casestudy
claims/returns/policy/checkmate/casestudy
于 2013-06-19T19:32:44.850 回答
1

它们是真正的目录和文件吗?

while read; do
    if [[ -f "$REPLY" ]]; then # Test if the input exists and is a file
        change_directory="${REPLY#/branches/testbranch/}"
        printf '%s\n' "${change_directory%/*}"
    fi
done < test
于 2013-06-19T19:36:02.780 回答
0

使用 sed:

sed -e 's#^/branches/testbranch/##' -e 's#/[^/.]*\..*$##' file
于 2013-06-19T19:31:25.887 回答