Sed 如何替换定长字母数字字符?
我试图用sed替换固定长度的字母数字后面的字符串Sed 如何替换定长字母数字字符?,sed,Sed,我试图用sed替换固定长度的字母数字后面的字符串 testing-1xs-a-2x-782b1x9.abc.txt testing-12a-b-2y-486eee2.bcd.txt testing-1a-c-2z-b62cx7d.cde.txt 我尝试了这个命令-sed's/[a-zA-Z0-9]{7}.//g'file 预期产出: testing-1xs-a-2x testing-12a-b-2y testing-1a-c-2z 你能试试下面的吗 awk --re-interval '{s
testing-1xs-a-2x-782b1x9.abc.txt
testing-12a-b-2y-486eee2.bcd.txt
testing-1a-c-2z-b62cx7d.cde.txt
我尝试了这个命令-sed's/[a-zA-Z0-9]{7}.//g'
file
预期产出:
testing-1xs-a-2x
testing-12a-b-2y
testing-1a-c-2z
你能试试下面的吗
awk --re-interval '{sub(/-[[:alnum:]]{7}.*/,"")} 1' Input_file
sed -E 's/-[[:alnum:]]{7}.*//' Input_file
如果您的awk
是新版本,您可以从上述代码中删除--re interval
在sed
中,您可以尝试以下操作
awk --re-interval '{sub(/-[[:alnum:]]{7}.*/,"")} 1' Input_file
sed -E 's/-[[:alnum:]]{7}.*//' Input_file
在默认BRE中并不特殊,因此使用{}
开关启用ERE(某些版本需要-E
而不是-r
)-E
- 在正则表达式的开头添加了缺少的
-
$ sed 's/-[^-]*$//' ip.txt
testing-1xs-a-2x
testing-12a-b-2y
testing-1a-c-2z
$ cut -d- -f1-4 ip.txt
testing-1xs-a-2x
testing-12a-b-2y
testing-1a-c-2z
谢谢Sandeep,假设有另一行代码,如
testing-1aasdfa-c-2z-b62cx7d.cde.txt
,那么我们如何避免第一行字母数字,并获得第四行的输出,如testing-1aasdfa-c-2z
。让我知道我需要将其作为新问题发布。我尝试了这个“sed-E的/-[:alnum:]{7}.*//2g'文件”,但没有成功。@VIPINKUMAR对于该示例,sed的/-[^-]*$/'
和cut-d--f1-4
都可以工作