パターンに一致する文字を含むファイルの行を印刷する方法

パターンに一致する文字を含むファイルの行を印刷する方法

パターンに一致する同じ文字を持つファイルのすべての行を印刷しようとしています。これは私のスキーマです -

CurrentPrincipal[MRC]
CurrentPrincipalLegalEventAssociation

ファイルには次の行があります。

823,agg.listgroup,CurrentPrincipal[MRC],CompanyElementDefinition
d4f170,atom.list,CurrentPrincipal[MRC][Type][*],CompanyElementDefinition
1097,agg.listgroup,CurrentPrincipalLegalEventAssociation,CompanyElementDefinition
c755ad,atom.list,CurrentPrincipal[LegalEventAssociation][Type][*],CompanyElementDefinition
8798c3,atom.list,CurrentPrincipal[MailingAddressStreetLine1][*],CompanyElementDefinition

私のパターンを繰り返して、ファイルから一致する行を印刷しています。私にとって必要なのは、パターンを繰り返すときに一致するCurrentPrincipal[MRC]行だけを取得する必要があるということです。

d4f170,atom.list,CurrentPrincipal[MRC][Type][*],CompanyElementDefinition

モードがあるときCurrentPrincipalLegalEventAssociation私はただ得なければなりません

c755ad,atom.list,CurrentPrincipal[LegalEventAssociation][Type][*],CompanyElementDefinition

[ ]私の要件は、パターンを一致させながら線を無視することです。私は質問をしようと努力しました。追加情報が必要な場合はお知らせください。よろしくお願いします。

ベストアンサー1

[文字範囲を表示するのではなく、文字を文字通り処理したいようです。]あなたはこれを行うことができます逃げるそれらを:

grep 'CurrentPrincipal\[LegalEventAssociation\]' file

前任者。与えられた:

$ cat file
823,agg.listgroup,CurrentPrincipal[MRC],CompanyElementDefinition
d4f170,atom.list,CurrentPrincipal[MRC][Type][*],CompanyElementDefinition
1097,agg.listgroup,CurrentPrincipalLegalEventAssociation,CompanyElementDefinition
c755ad,atom.list,CurrentPrincipal[LegalEventAssociation][Type][*],CompanyElementDefinition
8798c3,atom.list,CurrentPrincipal[MailingAddressStreetLine1][*],CompanyElementDefinition

それから

$ grep 'CurrentPrincipal\[LegalEventAssociation\]' file
c755ad,atom.list,CurrentPrincipal[LegalEventAssociation][Type][*],CompanyElementDefinition

または、 -For--fixed-stringsオプションを使用してgrepに、すべての文字を文字通り処理するように指示します。

$ grep -F 'CurrentPrincipal[LegalEventAssociation]' file
c755ad,atom.list,CurrentPrincipal[LegalEventAssociation][Type][*],CompanyElementDefinition

おすすめ記事