Regex 正则表达式:如何将截断字符替换为给定字符?
我需要使用Regex 正则表达式:如何将截断字符替换为给定字符?,regex,sed,Regex,Sed,我需要使用sed截断行中的所有字符(在本例中为]) 典型线路: *15:36:40,038 [Some text] More text ...* *15:57:40,038 [123] More text ....* 把另一个放在一边,把所有的东西都换掉,直到]没有东西。实现这一目标最简单的方法是什么 在提出建议时,我非常感谢对regex(或bashcut命令的使用)的解释,以进一步了解我的知识 非常感谢解决方案 你必须这样做: sed 's/[^]]*\]//' 示例 $ echo 15:
sed
截断行中的所有字符(在本例中为]
)
典型线路:
*15:36:40,038 [Some text] More text ...*
*15:57:40,038 [123] More text ....*
把另一个放在一边,把所有的东西都换掉,直到]
没有东西。实现这一目标最简单的方法是什么
在提出建议时,我非常感谢对regex(或bashcut
命令的使用)的解释,以进一步了解我的知识
非常感谢解决方案 你必须这样做:
sed 's/[^]]*\]//'
示例
$ echo 15:36:40,038 [Some text] More text ... | sed 's/[^]]*\]//'
More text ...
解释
表示除[^]]*
]
表示\]
]
awk -F']' '{print $NF}' file
例如:
kent$ echo "15:36:40,038 [Some text] More text ... "|awk -F']' '{print $NF}'
More text ...
kent$ echo "15:36:40,038 [Some text] xxx yyy [some other foo] More text ... "|awk -F']' '{print $NF}'
More text ...
您的cut示例:
kent$ echo "15:36:40,038 [Some text] More text ... "|cut -d] -f2
More text ...