首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在行尾追加任何字符串,并在特定行数之后继续这样做?

如何在行尾追加任何字符串,并在特定行数之后继续这样做?
EN

Stack Overflow用户
提问于 2016-09-13 16:03:17
回答 6查看 62关注 0票数 0

我想在第一行的末尾加上一个符号“>>”,然后加上第5行,以此类推。1,5,9,13,17,.我正在搜索网页,并浏览了下面的文章,但我无法实现它。请帮帮忙。

How can I append text below the specific number of lines in sed?

代码语言:javascript
复制
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)

产出应该是-

代码语言:javascript
复制
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)
EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2016-09-13 16:07:51

你可以用awk来做

代码语言:javascript
复制
awk '{if ((NR-1) % 5) {print $0} else {print $0 " >>"}}'

我们检查行号减1是否为5的倍数,如果是,则输出行后面跟着>>,否则,我们只输出行。

注意事项:上面的代码每5行输出一次后缀,因为这正是您的示例所需的。

票数 1
EN

Stack Overflow用户

发布于 2016-09-13 16:27:00

你可以用多种方法。sed在选择行时有点奇怪,但它是可行的。例如:

sed

代码语言:javascript
复制
sed -i -e 's/$/ >>/;n;n;n;n' file

您也可以作为perl一行进行此操作:

代码语言:javascript
复制
perl -pi.bak -e 's/(.*)/$1 >>/ if not (( $. - 1 ) % 5)' file
票数 0
EN

Stack Overflow用户

发布于 2016-09-13 17:33:40

你想错了。你应该附加到每一段的第一行末尾,不要担心在任何给定的段落中有多少行。那只是:

代码语言:javascript
复制
$ 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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39474463

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档