1

我有一个文件如下:

Country:  USA
Currency: Dollars
Country:  Canada
Currency: Canadian
          Dollars
Country:  Australia
Currency: Australian
          Dollars
Country:  France
Currency: Euro

我必须合并这两行并获得如下输出:

Country:  USA
Currency: Dollars
Country:  Canada
Currency: Canadian Dollars
Country:  Australia
Currency: Australian Dollars
Country:  France
Currency: Euro

我尝试使用tr空格替换换行符,但是没有用。有人可以帮忙吗。

谢谢。

4

1 回答 1

1
sed ':r;$!{N;br};s/\n[[:space:]]*\([[:alpha:]]\+\n\)/ \1/g' filename

例子:

$ echo 'Country:  USA
> Currency: Dollars
> Country:  Canada
> Currency: Canadian
>           Dollars
> Country:  Australia
> Currency: Australian
>           Dollars
> Country:  France
> Currency: Euro' | sed ':r;$!{N;br};s/\n[[:space:]]*\([[:alpha:]]\+\n\)/ \1/g'
Country:  USA
Currency: Dollars
Country:  Canada
Currency: Canadian Dollars
Country:  Australia
Currency: Australian Dollars
Country:  France
Currency: Euro

说明:r;$!{N;br};将所有文件读入模式空间,然后搜索所有看起来像一些空格后跟一些字母的行,并删除前面的换行符和多余的空格。有关更多信息,请参阅此sed参考

于 2012-08-07T09:41:43.700 回答