複数のファイルで特定の文字が見つかるたびに行をストリーミングします。

複数のファイルで特定の文字が見つかるたびに行をストリーミングします。

以下は、1行に変換したいファイルの例です。

-
Jun 6th
something2
09:00
some text blah blah
something1
Jun 6th
something1
09:00
some text xxx
something1

この行をcsvと同じ1行にインポートしようとしています。例:

Jun 6th, something2, 09:00, some text blah blah, something1
Jun 6th, something1, 09:00, some text xxx, something1

ベストアンサー1

次のSED文を試すことができます。

sed -ne '/^–/{g; /./!b; s/\n//; s/\n/, /g; p; z; h; b}; H' INPUTFILE

説明する:

/^–/{                 -->  if line starts with char "–", then:
    g                 -->      copy hold space to pattern space
    /./!b             -->      empty line? restart cycle
    s/\n//            -->      get rid of first newline
    s/\n/, /g         -->      replace all other newlines by ", "
    p                 -->      print pattern space
    z                 -->      erase pattern space
    h                 -->      erase hold space
    b                 -->      start new cycle
    }
H                     -->  otherwise, append newline + pattern space to hold space

入力する:


Jun 6th
something2
09:00
some text blah blah
some other thing2
Jun 7th
something1
10:30
some text xxx
some other thing1
Jun 9th
something3
12:15
some text yyy
some other thing3
Jun 8th
something4
07:05
some text zzz
some other thing4

出力:

Jun 6th, something2, 09:00, some text blah blah, some other thing2
Jun 7th, something1, 10:30, some text xxx, some other thing1
Jun 9th, something3, 12:15, some text yyy, some other thing3
Jun 8th, something4, 07:05, some text zzz, some other thing4

お役に立てば幸いです。

おすすめ記事