n番目の一致する行の前後のすべての内容を印刷します。

n番目の一致する行の前後のすべての内容を印刷します。

2行目までのすべての内容を印刷したいです。スタートwith * i(アスタリスク、スペース、小文字I)は、この行とこの行を含む、その後のすべての項目をそれぞれ除外します。

たとえば、次のテキストファイルがあるとします。

* misc: go to the park
with your dog 
* important: sell badges
the bigger the pricier
24 left right now
* important: go to the mall
get clothes
* important: finish homework 

まずこれを印刷したいです。

* misc: go to the park
with your dog
* important: sell badges
the bigger the pricier
24 left right now

それからこれ:

* important: go to the mall
get clothes
* important: finish homework 

sedを使用してこれを行うにはどうすればよいですか?

頑張った

sed  '/\* [^i]/,$   { /\* [^i]/,$ d}' /path/to/txt/

しかし、以前のすべての内容を印刷します。初めて一致する行。

各部分を抽出するには、2つの別々のスクリプトが必要です。

ベストアンサー1

私が提案するawk処理(私はこれがより柔軟で強力であると信じています):

awk '/^\* i/ && ++c == 2{ print r ORS; r=""; c=0 }
     { r=(r? r ORS:"")$0 }
     END{ print r }' file

出力:

* misc: go to the park
with your dog 
* important: sell badges
the bigger the pricier
24 left right now

* important: go to the mall
get clothes
* important: finish homework

現在の簡単なケース(追加ロジックなし)の場合、次のように短縮できます。

awk '/^\* i/ && ++c == 2{ print "" }1' file

必要な部分を個別に抽出するには、まだ単一のコマンドを使用しますが、条件付き値(最初の部分、前の部分)、または(2番目の部分、次の部分)を受け入れるawk動的パラメータを使用します。part12

計画:

awk -v part=[12] '/^\* i/ && ++c == 2{ if (part == 1) exit; else f=1 } part == 1 || (part == 2 && f)' FILE

使用法:

- 印刷「これから」部分:

$ awk -v part=1 '/^\* i/ && ++c==2{ if (part==1) exit; else f=1 }
>         part==1 || (part==2 && f)' file
* misc: go to the park
with your dog 
* important: sell badges
the bigger the pricier
24 left right now

- 印刷「後ろに」部分:

$ awk -v part=2 '/^\* i/ && ++c==2{ if (part==1) exit; else f=1 }
>         part==1 || (part==2 && f)' file
* important: go to the mall
get clothes
* important: finish homework

おすすめ記事