2016-05-19 26 views
0

は、分散ワークフローおよびバージョン管理に適しています。具体的には、1行に1文ずつ書式を設定するのが好きです。テキストの書式設定

私の問題は、私はこのフォーマットの方針に従わないことに変換する一部のレガシーファイルを持っていると私は自動化された方法でそれらを変換したいということです。これはsedおよび/またはawkのいくつかの組み合わせで簡単なものでなければならないような気がしますが、私はいくつかの問題を抱えています。

awk ' /^$/ { print "\n"; } /./ { printf("%s", $0); } END { print; } ' <filename> | sed -e $'s/\. /\.\\\n/g' 

これは私のほとんどを取得します。私は次のようにこれまでのところで持って

私は

This is some unformatted text that does not have a sentence on one line. 

This is a new unformatted paragraph that does not follow the rule either. 

This line \\ 
has a break in it. 

This is some unformatted 
text that does not have a sentence on one line. 

This is a new unformatted paragraph 
that does not follow the rule either. 

This line \\ has a break in it. 

を変換しようとしています

sed/awkそこには道がありますが、私は\\ fol改行文字で正しく動作するようになりました。

あなたのヘルプは大歓迎です。

+0

解決策は問題を解決して解決しますが、どちらの処理を説明することができますか? @sjsam – Mosby

+0

@Edモートン、上記を参照してください – Mosby

答えて

1

入力

$ cat text 
This is some unformatted 
text that does not have a sentence on one line. 

This is a new unformatted paragraph 
that does not follow the rule either. 

This line \\ has a break in it. 

This line too \\ contains break. 
This is a normal line. 

スクリプト

$ awk 'BEGIN{RS=".";} 
{$0=gensub(/([[:print:]?])\n/,"\\1 ","g"); 
$0=gensub(/(\\\\) /,"\\1\n","g"); 
printf "%s.",$0} 
END{printf "\n"}' text 

出力

This is some unformatted text that does not have a sentence on one line. 

This is a new unformatted paragraph that does not follow the rule either. 

This line \\ 
has a break in it. 

This line too \\ 
contains break. 
This is a normal line . 

注:これは、GNU AWKたと仮定しています。

1
$ awk -v RS= -v ORS='\n\n' -F'\\\\\\\\[[:space:]]*' -v OFS='\n' '{gsub(/\n/," "); $1=$1}1' file 
This is some unformatted text that does not have a sentence on one line. 

This is a new unformatted paragraph that does not follow the rule either. 

This line 
has a break in it. 
関連する問題