批量"未填写"带有shell脚本的段落

时间:2016-07-20 07:08:38

标签: bash shell sed

我有一堆文本文件,其中包含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应该能够做到这一点,但我不确定具体的命令是什么让它加入段落,而不是拆分它们。

3 个答案:

答案 0 :(得分:4)

这是一个纯粹的bash解决方案:

#!/bin/bash

while read -r
do
    if [[ -n $REPLY ]]
    then
        echo -n "$REPLY"
    else
        echo -e "\n$REPLY"
    fi
done < "gash.txt"

read删除了换行符和尾随空格。如果有剩余数据,那么我们会在没有换行符的情况下回显,否则我们会使用额外换行符进行回显。 REPLYread使用的默认变量。

答案 1 :(得分:4)

使用(g)awk

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

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

使用perl

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

答案 2 :(得分:1)

使用perl

的解决方案
perl -i.bak -pe 's/^$/\n/ ; s/(.+)\n/$1/' *.txt
  • -i.bak就地编辑以及创建备份文件,如果以后需要它们或者命令出错。如果不需要备份,请使用-i
  • s/^$/\n/加倍空行
  • s/(.+)\n/$1/删除非空行的换行符