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 ...
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
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 ...
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 ...
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 ...
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}')...
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 ...
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) ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
grep × 5109awk × 1112
sed × 972
text-processing × 852
regular-expression × 735
bash × 729
linux × 569
shell-script × 483
find × 369
shell × 354
command-line × 236
search × 151
scripting × 138
files × 129
pipe × 128
cut × 96
string × 91
perl × 82
logs × 78
xargs × 70
macos × 68
ls × 67
wildcards × 66
sort × 64
tail × 63