linux - 如何在行尾附加任何字符串并在特定行数之后继续执行?

标签 linux awk sed vi

我想在第 1 行和第 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)

最佳答案

你可以用 awk 来做:

awk '{if ((NR-1) % 5) {print $0} else {print $0 " >>"}}'

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

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

关于linux - 如何在行尾附加任何字符串并在特定行数之后继续执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39474463/

相关文章:

python - 当安装了 'make altinstall' 时,如何干净地删除 Python?

linux - C 编程 Fork 示例

linux - Yocto 中是否有类似于 buildroot 的任何 .config 文件

linux - 使用列名称而不是数字来过滤条件

linux - cat 只写入文件 evert 4.1kb

c - 如何在信号驱动的恶魔中暂停主循环?

linux - AWK 的多个字段分隔符

linux - 替换 JSON 配置文件中的日期

linux - sed:无效的范围结束

regex - sed 命令适用于命令行,但不适用于 bash 脚本