2012-05-07 18 views
20

複数のファイルで単語を検索し、結果ごとに1行だけを返すか、行全体ではなく制限された文字数(たとえば40〜80文字)デフォルトではgrep制限された文字 - 1行

grep -sR 'wp-content' . 

file_1.sql:3309:blog/wp-content 
file_1.sql:3509:blog/wp-content 
file_2.sql:309:blog/wp-content 

は現在、私は以下を参照してください。

grep -sR 'wp-content' . 

file_1.sql:3309:blog/wp-content-Progressively predominate impactful systems without resource-leveling best practices. Uniquely maximize virtual channels and inexpensive results. Uniquely procrastinate multifunctional leadership skills without visionary systems. Continually redefine prospective deliverables without. 
file_1.sql:3509:blog/wp-content-Progressively predominate impactful systems without resource-leveling best practices. Uniquely maximize virtual channels and inexpensive results. Uniquely procrastinate multifunctional leadership skills without visionary systems. Continually redefine prospective deliverables without. 
file_2.sql:309:blog/wp-content-Progressively predominate impactful systems without resource-leveling best practices. Uniquely maximize virtual channels and inexpensive results. Uniquely procrastinate multifunctional leadership skills without visionary systems. Continually redefine prospective deliverables without. 
+0

http://theunixshell.blogspot.in/2012/12/print-first-80-characters-in-line.html – Vijay

答えて

17
egrep -Rso '.{0,40}wp-content.{0,40}' *.sh 

これはRadio-Symphonie-Orchestraではなく、-o(nly matching)です。

パターンの前後に最大40文字です。注:* e * grep。

+0

素晴らしい作品です。ありがとうございます。 –

+1

'egrep'は廃止予定です。代わりに 'grep -E'を使用してください。 – ruuter

+0

@ruuter:私の最近のlinux(xubuntu 15.10)では、egrepは 'exec grep -E" $ @ "'です。 AFAIKこれは長い間のケースです。 –

4

あなたは'^.*wp-content'に正規表現を変更する場合は、egrep -oを使用することができます。例えば、

egrep -sRo '^.*wp-content' . 

-oフラグのegrepのみ一致する行の部分を印刷します。したがって、行の先頭からwp-contentに一致すると、最初のコードブロックにサンプル出力が得られます。

あなたはgrepの組み合わせを使用して、私が使用するあなたの例を使用して

を切ることができる

+0

コマンドを正しく理解していない可能性がありますが、機能しなかった方法。 –

+0

あなたのコンテンツがあなたの事例にあるものなら、それは 'wp-content'まですべてを出力するはずです。これは、最初のコードブロックで同じ出力にする必要があります。しかし、他の答えで判断すると、それはあなたが実際にしたいことかもしれません。 –

+0

@PatrickMaciel変更を見る。私は '-o'フラグの説明を拡張しました。 –

16

grep -sRn 'wp-content' .|cut -c -40 
grep -sRn 'wp-content' .|cut -c -80 

それぞれあなたの最初の40個のまたは80文字を与えるだろう。

編集:

また、あなたが使用できること、grepの中のフラグをtheresの:

-m NUM, --max-count=NUM 
      Stop reading a file after NUM matching lines. 

この私が以前に書いたものを組み合わせて:

grep -sRnm 1 'wp-content' .|cut -c -40 
grep -sRnm 1 'wp-content' .|cut -c -80 

を与える必要があることファイルごとに最初に表示され、最初の40または80文字のみが表示されます。

+0

正規表現を変更する必要がないので、私はこのオプションを本当に好んでいます。 –

関連する問題