[英]How to add a character to specific lines using grep/awk/sed
I want to search a file and add \\1 to the end of every line that begins with @. 我想搜索一个文件,并将\\ 1添加到以@开头的每一行的末尾。 One issue is grep doesn't like "\\1".
一个问题是grep不喜欢“ \\ 1”。 This is a few lines of how the file looks:
这是文件外观的几行:
@SRR1248080.1 1 length=90
TCTCACTGGCTGACTGAAGGCATGTCTAGTATTCAGAGTTTGCTACGATTTGGTACCGCTTTCGCAGCCC
GCACCGAAACAGTGCTTTACCCCTAGACAGCTCATAGTCAACCGCTGCGCCTCAACGCATTTCGGGGAGA
ACCAGCTAGCTCCGAGTTCGATTGGTATTTCACCCCTAACCACAGCTCATCCGCTGATTTTTCAACATCA
@SRR1249238.1 1 length=173
GCACCGAAACAGTGCTTTACCCCTAGACAGCTCATAGTCAACCGCTGCGCCTCAACGCATTTCGGGGAGA
ACCAGCTAGCTCCGAGTTCGATTGGTATTTCACCCCTAACCACAGCTCATCCGCTGATTTTTCAACATCA
I'd like it to read: 我想读:
@SRR1248080.1 1 length=90\1
TCTCACTGGCTGACTGAAGGCATGTCTAGTATTCAGAGTTTGCTACGATTTGGTACCGCTTTCGCAGCCC
GCACCGAAACAGTGCTTTACCCCTAGACAGCTCATAGTCAACCGCTGCGCCTCAACGCATTTCGGGGAGA
ACCAGCTAGCTCCGAGTTCGATTGGTATTTCACCCCTAACCACAGCTCATCCGCTGATTTTTCAACATCA
@SRR1249238.1 1 length=173\1
GCACCGAAACAGTGCTTTACCCCTAGACAGCTCATAGTCAACCGCTGCGCCTCAACGCATTTCGGGGAGA
ACCAGCTAGCTCCGAGTTCGATTGGTATTTCACCCCTAACCACAGCTCATCCGCTGATTTTTCAACATCA
Adding \\1
to the end of every line that begins with a @
is bread & butter for sed
. 在以
@
开头的每行末尾添加\\1
是sed
面包和黄油。 You simply specify a regular expression matching the lines to modify, and you can use an s
command to sub in your tail for the zero-length end of line: 您只需指定一个与要修改的行匹配的正则表达式,就可以使用
s
命令在行尾零行末尾进行细分:
sed '/^@/ s/$/\\1/' input_file
Note the doubling of the backslash to suppress its special meaning to sed
. 请注意,将反斜杠加倍以抑制
sed
特殊含义。
You might want to skip subbing lines that have already been subbed, perhaps by a previous run. 您可能想跳过已经被替换的底线,也许是之前的运行。 That's not hard either: just match those lines and use an
n
command to output them without further processing: 这也不难:只需匹配这些行并使用
n
命令即可输出它们而无需进一步处理:
sed '/\\1$/ n; /^@/ s/$/\\1/' input_file
Note that the sed
script is enclosed in single quotes, not double quotes; 注意,
sed
脚本用单引号引起来,而不用双引号引起来; it makes a difference (to the shell) here. 在这里(对外壳)有所不同。
@aeli: Try(if you are interested in awk solution) too: @aeli:也尝试(如果您对awk解决方案感兴趣):
awk '/^@/{print $0"\\1";next} 1' Input_file
It is simple, look for a line which is getting started from @ if yes then print complete line along with "\\1" and do next(means skip all other mentioned actions). 很简单,查找从@开始的一行(如果是),然后打印完整的行以及“ \\ 1”,然后执行下一步(意味着跳过所有其他提到的动作)。 Mentioning 1 means: in awk it works on condition then action method, so I am putting 1 here which is making condition TRUE here and mentioning no actin here so by default print of current line will happen.
提到1意味着:在awk中,它在条件然后是操作方法上起作用,因此我在这里放置1,这在此处使条件为TRUE,并且在这里没有提及actin,因此默认情况下将打印当前行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.