我有一堆包含这样的东西的txt文件:
text_i_need_to_remove{text_i_need_to_retain} text_i need_to_remove{text_i_need_to_retain} ...如何在花括号(和花括号本身)之前删除文本并仅保留text_i_need_to_retain ?
I have a bunch of txt-files containing stuff like this:
text_i_need_to_remove{text_i_need_to_retain} text_i need_to_remove{text_i_need_to_retain} ...How do I remove text before curly braces (and curly braces themselves) and retain just only text_i_need_to_retain?
最满意答案
在行尾删除最多{或}所有内容
:%s/.*{\|}$//g从bash shell,您可以使用sed和awk等文本处理工具。 假设文件名为ip.txt
1)使用sed ,这与我们在vim使用的正则表达式非常相似。 -i标志允许在适当的位置进行更改,即它会修改输入文件本身。
$ sed -i 's/.*{\|}$//g' ip.txt2)使用awk ,可以再次使用替换,或者在这种情况下,在大括号上拆分行并仅使用第二列。
$ awk -F'{|}' '{print $2}' ip.txt > tmp && mv tmp ip.txt如果你有GNU awk ,就会有-i inplace选项进行就地编辑
$ gawk -i inplace -F'{|}' '{print $2}' ip.txt要更改当前目录中的所有文件,请使用
sed -i 's/.*{\|}$//g' *或者,如果他们有共同的扩展名,比如.txt ,请使用
sed -i 's/.*{\|}$//g' *.txtDeleting everything upto { or } at end of line
:%s/.*{\|}$//gFrom bash shell, you can use text processing tools like sed and awk. Assume file is named ip.txt
1) With sed, which is pretty similar to regex we used inside vim. The -i flag allows to make change in place, i.e it modifies the input file itself.
$ sed -i 's/.*{\|}$//g' ip.txt2) With awk, one can again use substitution or in this case, split the line on curly brackets and use only the second column.
$ awk -F'{|}' '{print $2}' ip.txt > tmp && mv tmp ip.txtIf you have GNU awk, there is -i inplace option for in place editing
$ gawk -i inplace -F'{|}' '{print $2}' ip.txtTo make changed to all files in current directory, use
sed -i 's/.*{\|}$//g' *Or if they have common extension, say .txt, use
sed -i 's/.*{\|}$//g' *.txt更多推荐
发布评论