メモ帳: 各行の先頭に文字列を含む最後の行を付けるにはどうすればよいですか?

メモ帳: 各行の先頭に文字列を含む最後の行を付けるにはどうすればよいですか?

チャット ログを扱っており、それをフォーマットしたいと考えています。

# 記号を含めて、次のようになります。

Tuesday, February 24, 2015
##Person1 (21:22:01): hello
##Person2 (21:22:37): hi
Wednesday, February 25, 2015
##Person1 (13:12:43): hey
##Person2 (13:13:04): hey

日付は新しい日ごとにのみ投稿されますが、スプレッドシートで使用できるように、次のようにフォーマットしたいと思います。

Tuesday, February 24, 2015
Tuesday, February 24, 2015##Person1 (21:22:01): hey
Tuesday, February 24, 2015##Person2 (21:22:37): hi
Wednesday, February 25, 2015
Wednesday, February 25, 2015##Person1 (13:12:43): hey
Wednesday, February 25, 2015##Person2 (13:13:04): hey

その後、## 文字列を含まない行を簡単に削除して、日付のみの行を取り除くことができます。

\d{1,2}, 201\d{1}$Notepad++ で、日付の文字列 ( など) を含む最新の行全体を、その下の各行の先頭 (次のインスタンスまで)に追加する方法はありますか?

答え1

残念ながら、これは Notepad++ では実行できません。

ここに、目的を果たす Perl ワンライナーがあります。

perl -ane '$date = $1 if /^(\w+,\h+\w+\h+\d\d?,\h+20\d\d)/;s/^(?=##)/$date/ && print;' file.txt

ファイルをその場で置き換える場合は、次を使用します。

perl -i -ane '$date = $1 if /^(\w+,\h+\w+\h+\d\d?,\h+20\d\d)/;s/^(?=##)/$date/ && print;' file.txt

出力:

Tuesday, February 24, 2015##Person1 (21:22:01): hello
Tuesday, February 24, 2015##Person2 (21:22:37): hi
Wednesday, February 25, 2015##Person1 (13:12:43): hey
Wednesday, February 25, 2015##Person2 (13:13:04): hey

正規表現の説明:

/               # delimiter
  ^             # beginning of line
  (             # start group 1
    \w+         # 1 or more word character
    ,           # a comma
    \h+         # 1 or more horizontal spaces
    \w+         # 1 or more word character
    \h+         # 1 or more horizontal spaces
    \d\d?       # 1 or 2 digits
    ,           # a comma
    \h+         # 1 or more horizontal spaces
    20\d\d      # 20 and 2 digits
  )             # end group 1
/               # delimiter


s/              # substitute, delimiter
  ^             # beginning of line
  (?=##)        # positive lookahead, zero-length assertion that make sure we have ## at the beginning
/               # delimiter
  $date         # the date found with the preceding  regex
/               # delimiter

関連情報