0

我需要一个 awk/cut one liner 来获取模式和最后一个文件名之间的所有内容,例如:

$ cat .exportfiles.text
/branches/WP_Fix/Code/SharedApp/src/gov/illinois/ies/business/rules/ed/CorticonFinIncomeEntiyLoader.java
/branches/WP_Fix/Code/BEAN/Framework/ejbModule/gov/illinois/ies/business/rules/entities/fin/Income.java
/branches/WP_Fix/settings/Environment/RT/properties/build.properties
/branches/WP_Fix/test/Environment/DEV/properties/build.properties

期望的输出:

Code/SharedApp/src/gov/illinois/ies/business/rules/ed
Code/BEAN/Framework/ejbModule/gov/illinois/ies/business/rules/entities/fin
settings/Environment/RT/properties
test/Environment/DEV/properties
4

6 回答 6

5
sed -e 's,/branches/WP_Fix/\(.*\)/.*,\1,' < exportfiles.text
于 2013-05-28T22:27:19.100 回答
0

cut为了完整起见,这是一个解决方案。

cut -d / -f 4- .exportfiles.text | xargs -n 1 dirname

不仅使用cut,因为无法指定从最后一个返回的列。

于 2013-05-28T22:32:31.120 回答
0

awk不让您自己管理分隔符的替代解决方案:

awk -v FS=/ -v OFS=/ '{ for (i=1; i<NF; ++i) { $i = $(i+3) }; NF -= 4; print }' exportfiles.txt 

...打高尔夫球,对于喜欢这种事情的人:

awk -F/ -v OFS=/ '{for(i=0;++i<NF;){$i=$(i+3)}NF-=4}1' exportfiles.txt

但是如果你打破 Perl,这个可能会更容易:

perl -F/ -lane 'print join "/", @F[ 3 .. ($#F - 1) ]' exportfiles.txt 

..或者

 perl -F/ -lane '$,="/";print@F[3..$#F-1]' exportfiles.txt 
于 2013-05-28T22:49:57.100 回答
0

这条线可以工作:

awk -F'/[^/]*$' '{sub("/branches/WP_Fix/","",$1)}$0=$1' file

"/branches/WP_Fix/"可以是一个 shell 变量,以使该行更灵活。

于 2013-05-28T22:43:20.560 回答
0
perl -F"/" -lane 'print join "/",@F[3..scalar(@F)-2]' your_file

测试:

> cat temp
/branches/WP_Fix/Code/SharedApp/src/gov/illinois/ies/business/rules/ed/CorticonFinIncomeEntiyLoader.java
/branches/WP_Fix/Code/BEAN/Framework/ejbModule/gov/illinois/ies/business/rules/entities/fin/Income.java
/branches/WP_Fix/settings/Environment/RT/properties/build.properties
/branches/WP_Fix/test/Environment/DEV/properties/build.properties
> perl -F"/" -lane 'print join "/",@F[3..scalar(@F)-2]' temp
Code/SharedApp/src/gov/illinois/ies/business/rules/ed
Code/BEAN/Framework/ejbModule/gov/illinois/ies/business/rules/entities/fin
settings/Environment/RT/properties
test/Environment/DEV/properties
>

上面我使用了 autosplit。相反,如果你想进行 pettern 匹配,你也可以使用它:

perl -lne 'print $1 if(/\/branches\/WP_Fix\/(.*)\//)' your_file
于 2013-05-29T05:52:58.133 回答
0

简单的bash解决方案:

while IFS= read -r file; do 
    file=${file%/*}; file=${file#/*/*/}; echo "$file"; 
done < exportfiles.txt

测试:

[jaypal:~/Temp] while IFS= read -r file; do 
file=${file%/*}; file=${file#/*/*/}; echo "$file"; 
done < exportfiles.txt

Code/SharedApp/src/gov/illinois/ies/business/rules/ed
Code/BEAN/Framework/ejbModule/gov/illinois/ies/business/rules/entities/fin
settings/Environment/RT/properties
test/Environment/DEV/properties
于 2013-05-28T23:35:34.710 回答