linux - 如何在 linux 中删除 awk 中的尾随\n

标签 linux awk ksh

我有一个这样的输入文件:

Apr 24 2014;
is;
a;
sample;
;
Jun 24 2014 123;
may 25 2014;
is;
b;
sample;
;
Dec 21 2014 987

...我想要这样的输出:

Apr 24 2014;is;a;sample;;Jun 24 2014 123
may 25 2014;is;b;sample;;Dec 21 2014 987

(我正在使用 ksh)

最佳答案

对于包含以下内容的数据文件:

$ cat file
Apr 24 2014;
is;
a;
sample;
;
Jun 24 2014 123
may 25 2014;
is;
b;
sample;
;
Dec 21 2014 987

你可以这样做:

$ awk '{ORS=(NR%6?"":RS)}1' file
Apr 24 2014;is;a;sample;;Jun 24 2014 123
may 25 2014;is;b;sample;;Dec 21 2014 987

我们将输出字段分隔符设置为在每第 6 条记录后打印一个新行(默认值为 RS)。

您可以通过说将输出重定向到另一个文件

awk '{ORS=(NR%6?"":RS)}1' file > newfile

或执行以下操作对当前文件进行更改

awk '{ORS=(NR%6?"":RS)}1' file > tmp && mv tmp file

关于linux - 如何在 linux 中删除 awk 中的尾随\n,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23401800/

相关文章:

python - 在空行中重复数据,直到出现非空行

bash - 当我们将命令放在美元符号和括号 : $(command) 中时,它在 shell 中意味着什么

ssh - 如何在没有密码的情况下通过 SSH 登录

html - 使用 driver.find_element_by_class_name ("btn-success") 不工作 (Python)

linux - Dockerfile在一个阶段从源代码构建,然后在第二阶段复制并安装

linux - dhcpv6 返回错误子网

linux - 在 Linux 上使用 bluealsa 录制蓝牙耳机的音频

perl - 在 perl 中重新格式化文本

mysql - 将 MySQL "INSERT"命令转换为文本

bash - unix shell子字符串?