我有一个像这样的文件
MY-27125:some text
MY-27132:some text
MY-26565:some text
some text
MY-26097:some text
MY-27129:some text
MY-27211 && MY-26565:some text
some another text
MY-27175/MY-27147:some text
MY-27159\MY-27149:some text
MY-26747,MY-26945:some text
MY-20811 MY-26889:some text
我想使用sed或notepad ++或awk只有以MY- [5位数字]开头的文字。
输出应如下所示
MY-27125
MY-27132
MY-26565
MY-26097
MY-27129
MY-27211
MY-27175
MY-27159
MY-26747
MY-20811
MY-26565
MY-27147
MY-27149
MY-26945
MY-26889
答案 0 :(得分:3)
Grep是正确的工具:
grep -oP 'MY-\d{5}' file
答案 1 :(得分:2)
使用gnu awk
awk 'NR>1 {print RS substr($0,1,5)}' RS="MY-" file
MY-27125
MY-27132
MY-26565
MY-26097
MY-27129
MY-27211
MY-26565
MY-27175
MY-27147
MY-27159
MY-27149
MY-26747
MY-26945
MY-20811
MY-26889
答案 2 :(得分:2)
sed -n "/\(MY-[0-9]\{5\}\).*/ s//\1/p" YourFile
在GNU sed上添加-posix
答案 3 :(得分:1)
grep '^MY-' < in.txt | cut -b1-8
MY-27125
MY-27132
MY-26565
MY-26097
MY-27129
MY-27211
MY-27175
MY-27159
MY-26747
MY-20811