我想在第一行的末尾加上一个符号“>>”,然后加上第5行,以此类推。1,5,9,13,17,.我正在搜索网页,并浏览了下面的文章,但我无法实现它。请帮帮忙。
How can I append text below the specific number of lines in sed?
retentive
good at remembering
The child was very sharp, and her memory was extremely retentive.
— Rowlands, Effie Adelaide
unconscionable
greatly exceeding bounds of reason or moderation
For generations in the New York City public schools, this has become the norm with devastating consequences rooted in unconscionable levels of student failure.
— New York Times (Nov 4, 2011)产出应该是-
retentive >>
good at remembering
The child was very sharp, and her memory was extremely retentive.
— Rowlands, Effie Adelaide
unconscionable >>
greatly exceeding bounds of reason or moderation
For generations in the New York City public schools, this has become the norm with devastating consequences rooted in unconscionable levels of student failure.
— New York Times (Nov 4, 2011)发布于 2016-09-13 16:07:51
你可以用awk来做
awk '{if ((NR-1) % 5) {print $0} else {print $0 " >>"}}'我们检查行号减1是否为5的倍数,如果是,则输出行后面跟着>>,否则,我们只输出行。
注意事项:上面的代码每5行输出一次后缀,因为这正是您的示例所需的。
发布于 2016-09-13 16:27:00
你可以用多种方法。sed在选择行时有点奇怪,但它是可行的。例如:
sed
sed -i -e 's/$/ >>/;n;n;n;n' file您也可以作为perl一行进行此操作:
perl -pi.bak -e 's/(.*)/$1 >>/ if not (( $. - 1 ) % 5)' file发布于 2016-09-13 17:33:40
你想错了。你应该附加到每一段的第一行末尾,不要担心在任何给定的段落中有多少行。那只是:
$ awk -v RS= -v ORS='\n\n' '{sub(/\n/," >>&")}1' file
retentive >>
good at remembering
The child was very sharp, and her memory was extremely retentive.
— Rowlands, Effie Adelaide
unconscionable >>
greatly exceeding bounds of reason or moderation
For generations in the New York City public schools, this has become the norm with devastating consequences rooted in unconscionable levels of student failure.
— New York Times (Nov 4, 2011)https://stackoverflow.com/questions/39474463
复制相似问题