查找和替换以模式开头的行 [英] Finding and replacing lines that begin with a pattern
本文介绍了查找和替换以模式开头的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在文件 file.txt 中有这样的文本
I have a text in a file file.txt like this
xxxxxxxxxxxxxxx
xxxxxxxxxxxxxxx
a b c // delimited by tab
xxxxxxxxxxxxxxx
xxxxxxxxxxxxxxx
我知道使用 sed
我可以查找和替换文件中的文本.如果一行以 b(由制表符分隔)开头,我需要用 d e f 替换它.所以上面的文件将是
I know using sed
I can find and replace text in a file. If a line starts with a b(seperated by a tab) I need to replace it with d e f. So the above file will be
xxxxxxxxxxxxxxx
xxxxxxxxxxxxxxx
d e f // delimited by tab
xxxxxxxxxxxxxxx
xxxxxxxxxxxxxxx
我可以这样做来查找和替换,我只想要那些行以 b 开头并替换整行的实例.
I can do this to find and replace, I want only those instances where the line starts with a b and replace the whole line.
sed -i 's/a/\t/\b/\t\/c/REPLACED TEXT/g' file.TXT
推荐答案
使用 ^
符号表示一行的开始:
Use a ^
symbol to represent the beginning of a line:
sed -i 's/^a\tb.*$/REPLACED TEXT/g' file.TXT
说明:
^
表示行/输入的开始\t
表示制表符.
表示任意字符*
表示零个或多个前面的表达式$
表示行/输入结束
^
means beginning of line/input\t
means tab symbol.
means any character*
means zero or more of the preceeding expression$
means end of line/input
这篇关于查找和替换以模式开头的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文