在Linux上展开文件

发布于 2024-07-14 01:15:13 字数 122 浏览 2 评论 0原文

我有一个巨大的文本文件,大约 400.000 行,80 个字符宽,在 liux 上。

需要“展开”文件,将四行合并为一行 最终有 1/4 行,每行 80*4 个字符长。

有什么建议么?

I have a huge textfile, approx 400.000 lines 80 charachters wide on liux.

Need to "unfold" the file, merging four lines into one
ending up having 1/4 of the lines, each line 80*4 charachters long.

any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

多情出卖 2024-07-21 01:15:13
perl -pe 'chomp if (++$i % 4);'
perl -pe 'chomp if (++$i % 4);'
冷︶言冷语的世界 2024-07-21 01:15:13

使用 awk 执行此操作的一种更简单的方法是:

awk '{ printf $0 } (NR % 4 == 0) { print }' filename

尽管如果您想防止最终没有尾随换行符,则会变得更复杂一些:

awk '{ printf $0 } (NR % 4 == 0) { print } END { if (NR % 4 != 0) print }' filename

An easier way to do it with awk would be:

awk '{ printf $0 } (NR % 4 == 0) { print }' filename

Although if you wanted to protect against ending up without a trailing newline it gets a little more complicated:

awk '{ printf $0 } (NR % 4 == 0) { print } END { if (NR % 4 != 0) print }' filename
绝情姑娘 2024-07-21 01:15:13

我希望我正确理解你的问题。 你有一个像这样的输入行(除非你的行更长):

abcdef
ghijkl
mnopqr
stuvwx
yz0123
456789
ABCDEF

你想要这样的输出:

abcdefghijklmnopqrstuvwx
yz0123456789ABCDEF

下面的 awk 程序应该这样做:

{ line = line $0 }
(NR % 4) == 0 { print line; line = "" }
END { if (line != "") print line }

像这样运行它:

awk -f merge.awk data.txt

I hope I understood your question correctly. You have an input line like this (except your lines are longer):

abcdef
ghijkl
mnopqr
stuvwx
yz0123
456789
ABCDEF

You want output like this:

abcdefghijklmnopqrstuvwx
yz0123456789ABCDEF

The following awk program should do it:

{ line = line $0 }
(NR % 4) == 0 { print line; line = "" }
END { if (line != "") print line }

Run it like this:

awk -f merge.awk data.txt
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文