在文本文件中的每个 n:th 行前添加文本
prepend text to every n:th line in a textfile
这个 sed 命令行脚本在文件的每一行前面加上文本:
sed -i 's/^/to be prepended/g' text.txt
我怎样才能让它只在第 nth 行执行?
我正在处理测序数据,在 "norma" 多重 fasta 格式中,首先是一个以 > 开头的标识符行,然后是其他文本。
下一行以随机 DNA 序列开始,如 "AATTGCC" 等等,当该字符串完成其新行和新标识符时,我如何将文本(附加碱基)添加到序列的开头线?
只需使用以下 GNU sed
语法:
sed '0~Ns/^/to be prepended/'
# ^^^
# set N to the number you want!
例如,将 HA 添加到 4 的倍数的行号前:
$ seq 10 | sed '0~4s/^/HA/'
1
2
3
HA4
5
6
7
HA8
9
10
或表格 4N+1
:
$ seq 10 | sed '1~4s/^/HA/'
HA1
2
3
4
HA5
6
7
8
HA9
10
来自sed
manual → 3.2. Selecting lines with sed
:
first~step
This GNU extension matches every stepth line starting with line first. In particular, lines will be selected when there exists a non-negative n such that the current line-number equals first + (n * step). Thus, to select the odd-numbered lines, one would use 1~2; to pick every third line starting with the second, ‘2~3’ would be used; to pick every fifth line starting with the tenth, use ‘10~5’; and ‘50~0’ is just an obscure way of saying 50.
顺便说一下,不需要使用/g
进行全局替换,因为^
每行只需替换一次。
$ seq 10 | perl -pe's/^/to be prepended / unless $. % 3'
1
2
to be prepended 3
4
5
to be prepended 6
7
8
to be prepended 9
10
$ seq 10 | perl -pe's/^/to be prepended / unless $. % 3 - 1'
to be prepended 1
2
3
to be prepended 4
5
6
to be prepended 7
8
9
to be prepended 10
$ seq 10 | perl -pe's/^/to be prepended / unless $. % 3 - 2'
1
to be prepended 2
3
4
to be prepended 5
6
7
to be prepended 8
9
10
你有一个想法。
seq 15|awk -v line=4 'NR%line==0{[=10=]="Prepend this text : " [=10=]}1'
1
2
3
Prepend this text : 4
5
6
7
Prepend this text : 8
9
10
11
Prepend this text : 12
13
14
15
这个 sed 命令行脚本在文件的每一行前面加上文本:
sed -i 's/^/to be prepended/g' text.txt
我怎样才能让它只在第 nth 行执行?
我正在处理测序数据,在 "norma" 多重 fasta 格式中,首先是一个以 > 开头的标识符行,然后是其他文本。
下一行以随机 DNA 序列开始,如 "AATTGCC" 等等,当该字符串完成其新行和新标识符时,我如何将文本(附加碱基)添加到序列的开头线?
只需使用以下 GNU sed
语法:
sed '0~Ns/^/to be prepended/'
# ^^^
# set N to the number you want!
例如,将 HA 添加到 4 的倍数的行号前:
$ seq 10 | sed '0~4s/^/HA/'
1
2
3
HA4
5
6
7
HA8
9
10
或表格 4N+1
:
$ seq 10 | sed '1~4s/^/HA/'
HA1
2
3
4
HA5
6
7
8
HA9
10
来自sed
manual → 3.2. Selecting lines with sed
:
first~step
This GNU extension matches every stepth line starting with line first. In particular, lines will be selected when there exists a non-negative n such that the current line-number equals first + (n * step). Thus, to select the odd-numbered lines, one would use 1~2; to pick every third line starting with the second, ‘2~3’ would be used; to pick every fifth line starting with the tenth, use ‘10~5’; and ‘50~0’ is just an obscure way of saying 50.
顺便说一下,不需要使用/g
进行全局替换,因为^
每行只需替换一次。
$ seq 10 | perl -pe's/^/to be prepended / unless $. % 3'
1
2
to be prepended 3
4
5
to be prepended 6
7
8
to be prepended 9
10
$ seq 10 | perl -pe's/^/to be prepended / unless $. % 3 - 1'
to be prepended 1
2
3
to be prepended 4
5
6
to be prepended 7
8
9
to be prepended 10
$ seq 10 | perl -pe's/^/to be prepended / unless $. % 3 - 2'
1
to be prepended 2
3
4
to be prepended 5
6
7
to be prepended 8
9
10
你有一个想法。
seq 15|awk -v line=4 'NR%line==0{[=10=]="Prepend this text : " [=10=]}1'
1
2
3
Prepend this text : 4
5
6
7
Prepend this text : 8
9
10
11
Prepend this text : 12
13
14
15