2

次のような数行のファイルがあります。

*wordX*-Sentence1.;Sentence2.;Sentence3.;Sentence4.

これらのセンテンスの 1 つには、wordX が含まれている場合と含まれていない場合があります。私が望むのは、ファイルをトリミングして次のようにすることです。

*wordX*-Sentence1.;Sentence2.

Sentence3 が最初に wordX を含む場所です。

sed/awkでこれを行うにはどうすればよいですか?

編集:

サンプル ファイルは次のとおりです。

*WordA*-This sentence does not contain what i want.%Neither does this one.;Not here either.;Not here.;Here is WordA.;But not here.
*WordB*-WordA here.;WordB here, time to delete everything.;Including this sentece.
*WordC*-WordA, WordB. %Sample sentence one.;Sample Sentence 2.;Sample sentence 3.;Sample sentence 4.;WordC.;Discard this.

そして、ここに目的の出力があります:

*WordA*-This sentence does not contain what i want.%Neither does this one.;Not here either.;Not here.
*WordB*-WordA here.
*WordC*-WordA, WordB. %Sample sentence one.;Sample Sentence 2.;Sample sentence 3.;Sample sentence 4.
4

3 に答える 3

1

このタスクは awk に適しています。次の awk コマンドを使用します。

awk -F ";" '/^ *\*.*?\*/ {printf("%s;%s\n", $1, $2)}' inFile

これは、一致させようとしている単語が常にアスタリスクで囲まれていることを前提としています*

于 2013-05-08T19:08:39.437 に答える
0
sed -r -e 's/\.;/\n/g' \
       -e 's/-/\n/' \
       -e 's/^(\*([^*]*).*\n)[^\n]*\2.*/\1/' \
       -e 's/\n/-/' \
       -e 's/\n/.;/g' \
       -e 's/;$//'

(編集:最初の文で一致を処理するために-:スワップを追加しました。)\n

于 2013-05-09T15:06:53.970 に答える
0

これはうまくいくかもしれません(GNU sed):

sed -r 's/-/;/;:a;s/^(\*([^*]+)\*.*);[^;]+\2.*/\1;/;ta;s/;/-/;s/;$//' file

-次のwordXを に変換します;wordX(行の後ろから前に向かって作業)を含む文章を削除します。元を置き換えます。-最後のを削除し;ます。

于 2013-05-08T21:07:29.743 に答える