shell 正则截取字符串
用sed替换功能
echo 'weihongrao' |sed 's/wei\(hong\)rao/\1/g'
比如我有如下文件 test.txt
12344554534543232534sdfhosnfsdf
wueorhalfsafsdfse
1231231823918231247391274921
djflajsdfasff238427394720349723094jdlfjalf
我要截取连续的15位数字,并且如果没有连续15位数字的话就不要显示该行
[hongrao.whr@v133197.sqa.cm4 wget]$ sed 's/.*\([0-9]\{15\}\).*/\1/g' test.txt
554534543232534
wueorhalfsafsdfse
231247391274921
394720349723094
可以发现wueorhalfsafsdfse这行数据也出来了,这不是我想要的,可以结合grep或者awk来实现
[hongrao.whr@v133197.sqa.cm4 wget]$ sed 's/.*\([0-9]\{15\}\).*/\1/g' test.txt | grep '[0-9]\{15\}'
554534543232534
231247391274921
394720349723094
或者吧grep放在前面
或者使用awk
[hongrao.whr@v133197.sqa.cm4 wget]$ sed 's/.*\([0-9]\{15\}\).*/\1/g' test.txt | awk '{if(length($0)==15){print $0}}'
554534543232534
231247391274921
394720349723094
用sed替换功能
echo 'weihongrao' |sed 's/wei\(hong\)rao/\1/g'
比如我有如下文件 test.txt
12344554534543232534sdfhosnfsdf
wueorhalfsafsdfse
1231231823918231247391274921
djflajsdfasff238427394720349723094jdlfjalf
我要截取连续的15位数字,并且如果没有连续15位数字的话就不要显示该行
[hongrao.whr@v133197.sqa.cm4 wget]$ sed 's/.*\([0-9]\{15\}\).*/\1/g' test.txt
554534543232534
wueorhalfsafsdfse
231247391274921
394720349723094
可以发现wueorhalfsafsdfse这行数据也出来了,这不是我想要的,可以结合grep或者awk来实现
[hongrao.whr@v133197.sqa.cm4 wget]$ sed 's/.*\([0-9]\{15\}\).*/\1/g' test.txt | grep '[0-9]\{15\}'
554534543232534
231247391274921
394720349723094
或者吧grep放在前面
或者使用awk
[hongrao.whr@v133197.sqa.cm4 wget]$ sed 's/.*\([0-9]\{15\}\).*/\1/g' test.txt | awk '{if(length($0)==15){print $0}}'
554534543232534
231247391274921
394720349723094