gpt4 book ai didi

bash - 用shell脚本批量处理 "unfill"段

转载 作者:行者123 更新时间:2023-11-29 09:18:04 24 4
gpt4 key购买 nike

我有一堆文本文件,在 80 个字符处有硬换行符。我想“取消填充”(使用 emacs 术语)这些段落,这样每个段落都是一行,以便更容易地将这些文件中的文本复制和粘贴到其他应用程序中。有没有办法用 shell 脚本做到这一点?

例如,我有如下所示的输入文本:

Call me Ishmael. Some years ago- never mind how long precisely- having little or 
no money in my purse, and nothing particular to interest me on shore, I thought
I would sail about a little and see the watery part of the world. It is a way I
have of driving off the spleen and regulating the circulation. Whenever I find
myself growing grim about the mouth; whenever it is a damp, drizzly November in
my soul; whenever I find myself involuntarily pausing before coffin warehouses,
and bringing up the rear of every funeral I meet; and especially whenever my
hypos get such an upper hand of me, that it requires a strong moral principle
to prevent me from deliberately stepping into the street, and methodically
knocking people's hats off- then, I account it high time to get to sea as soon
as I can. This is my substitute for pistol and ball. With a philosophical
flourish Cato throws himself upon his sword; I quietly take to the ship.
There is nothing surprising in this. If they but knew it, almost all men in
their degree, some time or other, cherish very nearly the same feelings towards
the ocean with me.

There now is your insular city of the Manhattoes, belted round by wharves as
Indian isles by coral reefs- commerce surrounds it with her surf. Right and
left, the streets take you waterward. Its extreme downtown is the battery,
where that noble mole is washed by waves, and cooled by breezes, which a few
hours previous were out of sight of land. Look at the crowds of water-gazers
there.

我希望输出文本看起来像:

Call me Ishmael. Some years ago- never mind how long precisely- having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen and regulating the circulation. Whenever I find myself growing grim about the mouth; whenever it is a damp, drizzly November in my soul; whenever I find myself involuntarily pausing before coffin warehouses, and bringing up the rear of every funeral I meet; and especially whenever my hypos get such an upper hand of me, that it requires a strong moral principle to prevent me from deliberately stepping into the street, and methodically knocking people's hats off- then, I account it high time to get to sea as soon as I can. This is my substitute for pistol and ball. With a philosophical flourish Cato throws himself upon his sword; I quietly take to the ship. There is nothing surprising in this. If they but knew it, almost all men in their degree, some time or other, cherish very nearly the same feelings towards the ocean with me.

There now is your insular city of the Manhattoes, belted round by wharves as Indian isles by coral reefs- commerce surrounds it with her surf. Right and left, the streets take you waterward. Its extreme downtown is the battery, where that noble mole is washed by waves, and cooled by breezes, which a few hours previous were out of sight of land. Look at the crowds of water-gazers there.

有没有办法使用 shell 脚本来做到这一点?我觉得 sed 应该能够做到这一点,但我不确定具体的命令是什么让它连接段落,而不是拆分它们。

最佳答案

使用 (g)awk

awk -vRS= -vORS= '{gsub("\n","")}{print $0 RT}' file

按段落拆分记录并从记录中删除所有换行符。

用 perl

perl -pe '/^$/?print:chomp' file

关于bash - 用shell脚本批量处理 "unfill"段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38474547/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com