Skip to main content
9 votes

Grep with output of not the complete line

Using GNU grep: $ grep -oP '\w*blue\w*(\s*\w+)?' input.txt blue house blue mouse This shows the complete word containing the match and the following word (if any). Note that with \w, underscores are ...
cas's user avatar
  • 83.9k
7 votes
Accepted

Grep with output of not the complete line

-E for extended regex grep -oE 'blue.{0,6}' f.txt -o with escape characters grep -o 'blue.\{0,6\}' f.txt grep & cut grep -o 'blue.*' f.txt | cut -c1-10 Perl-RegEx grep -Po 'blue.{0,6}' f.txt
ReflectYourCharacter's user avatar
5 votes

Bug or not?: grep stops finding more matches after "Binary file ... matches"

If I look at the Unix standard, as defined by the Open Group, it explicitly states: The input files shall be text files. What grep should do is it finds a non-text character is not described. The ...
Ljm Dullaart's user avatar
  • 5,053
2 votes

Grep with output of not the complete line

$ cat file This is the blue tips of blue teeth of a blue mouse in a blue house $ grep -Eo 'blue.{0,10}' file blue tips of b blue mouse in blue house See how we're missing blue teeth... above as the ...
Stéphane Chazelas's user avatar
2 votes

Bug or not?: grep stops finding more matches after "Binary file ... matches"

This comes down to buffering. If the first NUL (\0) character is far enough down the file so that it isn't included in the first buffer that grep fills for searching (or perhaps for storing the ...
terdon's user avatar
  • 252k
2 votes

How to construct a command from the output of another command?

Using printf, awk (with : , a colon followed by a space, as the field separator), and readarray: $ readarray -t a <(srvctl config database -d MYDB | awk -F': ' '/:/ {print $2}')...
cas's user avatar
  • 83.9k
2 votes

How to construct a command from the output of another command?

This awk command will take the output from your srvctl command - or at least one that's formatted similarly to the one posted in the question - and generate a command you can use to repeat the ...
Chris Davies's user avatar
1 vote

How to construct a command from the output of another command?

srvctl add database -d MYDB -o /path/to/home is not a Linux command, it's code in the language of most Unix shells that most Unix shells interpret as executing a file (something like /usr/bin/srvctl) ...
Stéphane Chazelas's user avatar
1 vote

Grep with output of not the complete line

Just use awk instead of grep, e.g. using any awk in any shell on every Unix box: $ awk 'match($0,/blue/){print substr($0,RSTART,10)}' f.txt blue house blue mouse The above assumes you just want to ...
Ed Morton's user avatar
  • 35.8k

Only top scored, non community-wiki answers of a minimum length are eligible