highlight a word without affecting the structure of text [duplicate]
This question already has an answer here:
Convince grep to output all lines, not just those with matches
11 answers
$cat contents.txt
cat-1.15
cat-1.15
cat-1.15
cat-1.18
The above output has blank lines
$cat contents.txt | grep cat
results in the word cat being highlighted, but the resultant text is also merged, eliminating blank lines
cat-1.15
cat-1.15
cat-1.15
cat-1.18
How can I grep to highlight without grep affecting the text structure, so that the only difference is the grep term being highlighted ?
awk sed grep
marked as duplicate by steeldriver, Isaac, don_crissti, Gilles, Jeff Schaller Jan 25 at 23:22
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Convince grep to output all lines, not just those with matches
11 answers
$cat contents.txt
cat-1.15
cat-1.15
cat-1.15
cat-1.18
The above output has blank lines
$cat contents.txt | grep cat
results in the word cat being highlighted, but the resultant text is also merged, eliminating blank lines
cat-1.15
cat-1.15
cat-1.15
cat-1.18
How can I grep to highlight without grep affecting the text structure, so that the only difference is the grep term being highlighted ?
awk sed grep
marked as duplicate by steeldriver, Isaac, don_crissti, Gilles, Jeff Schaller Jan 25 at 23:22
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Convince grep to output all lines, not just those with matches
11 answers
$cat contents.txt
cat-1.15
cat-1.15
cat-1.15
cat-1.18
The above output has blank lines
$cat contents.txt | grep cat
results in the word cat being highlighted, but the resultant text is also merged, eliminating blank lines
cat-1.15
cat-1.15
cat-1.15
cat-1.18
How can I grep to highlight without grep affecting the text structure, so that the only difference is the grep term being highlighted ?
awk sed grep
This question already has an answer here:
Convince grep to output all lines, not just those with matches
11 answers
$cat contents.txt
cat-1.15
cat-1.15
cat-1.15
cat-1.18
The above output has blank lines
$cat contents.txt | grep cat
results in the word cat being highlighted, but the resultant text is also merged, eliminating blank lines
cat-1.15
cat-1.15
cat-1.15
cat-1.18
How can I grep to highlight without grep affecting the text structure, so that the only difference is the grep term being highlighted ?
This question already has an answer here:
Convince grep to output all lines, not just those with matches
11 answers
awk sed grep
awk sed grep
edited Jan 25 at 17:09
user607694
asked Jan 25 at 17:07
user607694user607694
504
504
marked as duplicate by steeldriver, Isaac, don_crissti, Gilles, Jeff Schaller Jan 25 at 23:22
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by steeldriver, Isaac, don_crissti, Gilles, Jeff Schaller Jan 25 at 23:22
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
With GNU grep this can be accomplished with the -z
option.
-z, --null-data
Treat input and output data as sequences of lines, each terminated by a zero byte (the ASCII NUL character) instead of a newline. Like the -Z or --null
option, this option can be used with commands like sort -z to process arbitrary file names.
Also this is a UUOC. You can specify an input file with grep.
$ grep --color cat contents.txt
cat-1.15
cat-1.15
cat-1.15
cat-1.18
$ grep --color -z cat contents.txt
cat-1.15
cat-1.15
cat-1.15
cat-1.18
2
Note that this will try to read the whole file into memory. If it is big, a big chunk of memory will be used.
– Isaac
Jan 25 at 19:51
add a comment |
Another possible solution is to provide two patterns to grep
: one will be the actual pattern or term to be searched for, and the second one will be an empty string.
$ grep --color -e 'cat' -e '' testfile.txt
cat-1.15
cat-1.15
cat-1.15
cat-1.18
The -e
option is used for specify multiple patterns. From the manual:
-e PATTERN
,--regexp=PATTERN
Use PATTERN as the pattern. This can be used to specify multiple search patterns, or to protect a pattern beginning with a hyphen (-). (
-e
is specified by POSIX.)
You can also combine those patterns as a single extended regular expression, if required:
$ grep --color -E 'cat|' testfile.txt
cat-1.15
cat-1.15
cat-1.15
cat-1.18
Also, you can simply add another pattern to the list if you need to highlight more than one keyword.
add a comment |
A couple more possibilities with grep:
grep for 0 or more instances of cat
:
grep --color '(cat)*' contents.txt
grep for cat
or the empty string:
grep -E --color 'cat|' contents.txt
(The -E specifies extended regex syntax. egrep
may be used instead of grep -E
here.)
Alternatively you can use sed
to do the colourization manually using ANSI escape codes:
red='c[[1;31m'
default='c[[0m'
sed "s/cat/${red}cat${default}/g" contents.txt
Here the red
and default
shell variables are conveniences only - their values could just as well be placed inline in the sed expression.
add a comment |
I tried with below command
sed -r "/cat/{N;s/^$/&=/}" filename
awk '{if(((length($1)>3)&& ($1 ~ /cat/))||($0~/^$/)){print $0}}' filename
How do these highlight the output?
– wjandrea
Jan 25 at 20:50
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
With GNU grep this can be accomplished with the -z
option.
-z, --null-data
Treat input and output data as sequences of lines, each terminated by a zero byte (the ASCII NUL character) instead of a newline. Like the -Z or --null
option, this option can be used with commands like sort -z to process arbitrary file names.
Also this is a UUOC. You can specify an input file with grep.
$ grep --color cat contents.txt
cat-1.15
cat-1.15
cat-1.15
cat-1.18
$ grep --color -z cat contents.txt
cat-1.15
cat-1.15
cat-1.15
cat-1.18
2
Note that this will try to read the whole file into memory. If it is big, a big chunk of memory will be used.
– Isaac
Jan 25 at 19:51
add a comment |
With GNU grep this can be accomplished with the -z
option.
-z, --null-data
Treat input and output data as sequences of lines, each terminated by a zero byte (the ASCII NUL character) instead of a newline. Like the -Z or --null
option, this option can be used with commands like sort -z to process arbitrary file names.
Also this is a UUOC. You can specify an input file with grep.
$ grep --color cat contents.txt
cat-1.15
cat-1.15
cat-1.15
cat-1.18
$ grep --color -z cat contents.txt
cat-1.15
cat-1.15
cat-1.15
cat-1.18
2
Note that this will try to read the whole file into memory. If it is big, a big chunk of memory will be used.
– Isaac
Jan 25 at 19:51
add a comment |
With GNU grep this can be accomplished with the -z
option.
-z, --null-data
Treat input and output data as sequences of lines, each terminated by a zero byte (the ASCII NUL character) instead of a newline. Like the -Z or --null
option, this option can be used with commands like sort -z to process arbitrary file names.
Also this is a UUOC. You can specify an input file with grep.
$ grep --color cat contents.txt
cat-1.15
cat-1.15
cat-1.15
cat-1.18
$ grep --color -z cat contents.txt
cat-1.15
cat-1.15
cat-1.15
cat-1.18
With GNU grep this can be accomplished with the -z
option.
-z, --null-data
Treat input and output data as sequences of lines, each terminated by a zero byte (the ASCII NUL character) instead of a newline. Like the -Z or --null
option, this option can be used with commands like sort -z to process arbitrary file names.
Also this is a UUOC. You can specify an input file with grep.
$ grep --color cat contents.txt
cat-1.15
cat-1.15
cat-1.15
cat-1.18
$ grep --color -z cat contents.txt
cat-1.15
cat-1.15
cat-1.15
cat-1.18
answered Jan 25 at 17:14
Jesse_bJesse_b
13.7k23471
13.7k23471
2
Note that this will try to read the whole file into memory. If it is big, a big chunk of memory will be used.
– Isaac
Jan 25 at 19:51
add a comment |
2
Note that this will try to read the whole file into memory. If it is big, a big chunk of memory will be used.
– Isaac
Jan 25 at 19:51
2
2
Note that this will try to read the whole file into memory. If it is big, a big chunk of memory will be used.
– Isaac
Jan 25 at 19:51
Note that this will try to read the whole file into memory. If it is big, a big chunk of memory will be used.
– Isaac
Jan 25 at 19:51
add a comment |
Another possible solution is to provide two patterns to grep
: one will be the actual pattern or term to be searched for, and the second one will be an empty string.
$ grep --color -e 'cat' -e '' testfile.txt
cat-1.15
cat-1.15
cat-1.15
cat-1.18
The -e
option is used for specify multiple patterns. From the manual:
-e PATTERN
,--regexp=PATTERN
Use PATTERN as the pattern. This can be used to specify multiple search patterns, or to protect a pattern beginning with a hyphen (-). (
-e
is specified by POSIX.)
You can also combine those patterns as a single extended regular expression, if required:
$ grep --color -E 'cat|' testfile.txt
cat-1.15
cat-1.15
cat-1.15
cat-1.18
Also, you can simply add another pattern to the list if you need to highlight more than one keyword.
add a comment |
Another possible solution is to provide two patterns to grep
: one will be the actual pattern or term to be searched for, and the second one will be an empty string.
$ grep --color -e 'cat' -e '' testfile.txt
cat-1.15
cat-1.15
cat-1.15
cat-1.18
The -e
option is used for specify multiple patterns. From the manual:
-e PATTERN
,--regexp=PATTERN
Use PATTERN as the pattern. This can be used to specify multiple search patterns, or to protect a pattern beginning with a hyphen (-). (
-e
is specified by POSIX.)
You can also combine those patterns as a single extended regular expression, if required:
$ grep --color -E 'cat|' testfile.txt
cat-1.15
cat-1.15
cat-1.15
cat-1.18
Also, you can simply add another pattern to the list if you need to highlight more than one keyword.
add a comment |
Another possible solution is to provide two patterns to grep
: one will be the actual pattern or term to be searched for, and the second one will be an empty string.
$ grep --color -e 'cat' -e '' testfile.txt
cat-1.15
cat-1.15
cat-1.15
cat-1.18
The -e
option is used for specify multiple patterns. From the manual:
-e PATTERN
,--regexp=PATTERN
Use PATTERN as the pattern. This can be used to specify multiple search patterns, or to protect a pattern beginning with a hyphen (-). (
-e
is specified by POSIX.)
You can also combine those patterns as a single extended regular expression, if required:
$ grep --color -E 'cat|' testfile.txt
cat-1.15
cat-1.15
cat-1.15
cat-1.18
Also, you can simply add another pattern to the list if you need to highlight more than one keyword.
Another possible solution is to provide two patterns to grep
: one will be the actual pattern or term to be searched for, and the second one will be an empty string.
$ grep --color -e 'cat' -e '' testfile.txt
cat-1.15
cat-1.15
cat-1.15
cat-1.18
The -e
option is used for specify multiple patterns. From the manual:
-e PATTERN
,--regexp=PATTERN
Use PATTERN as the pattern. This can be used to specify multiple search patterns, or to protect a pattern beginning with a hyphen (-). (
-e
is specified by POSIX.)
You can also combine those patterns as a single extended regular expression, if required:
$ grep --color -E 'cat|' testfile.txt
cat-1.15
cat-1.15
cat-1.15
cat-1.18
Also, you can simply add another pattern to the list if you need to highlight more than one keyword.
edited Jan 26 at 6:03
answered Jan 25 at 17:35
HaxielHaxiel
3,35511021
3,35511021
add a comment |
add a comment |
A couple more possibilities with grep:
grep for 0 or more instances of cat
:
grep --color '(cat)*' contents.txt
grep for cat
or the empty string:
grep -E --color 'cat|' contents.txt
(The -E specifies extended regex syntax. egrep
may be used instead of grep -E
here.)
Alternatively you can use sed
to do the colourization manually using ANSI escape codes:
red='c[[1;31m'
default='c[[0m'
sed "s/cat/${red}cat${default}/g" contents.txt
Here the red
and default
shell variables are conveniences only - their values could just as well be placed inline in the sed expression.
add a comment |
A couple more possibilities with grep:
grep for 0 or more instances of cat
:
grep --color '(cat)*' contents.txt
grep for cat
or the empty string:
grep -E --color 'cat|' contents.txt
(The -E specifies extended regex syntax. egrep
may be used instead of grep -E
here.)
Alternatively you can use sed
to do the colourization manually using ANSI escape codes:
red='c[[1;31m'
default='c[[0m'
sed "s/cat/${red}cat${default}/g" contents.txt
Here the red
and default
shell variables are conveniences only - their values could just as well be placed inline in the sed expression.
add a comment |
A couple more possibilities with grep:
grep for 0 or more instances of cat
:
grep --color '(cat)*' contents.txt
grep for cat
or the empty string:
grep -E --color 'cat|' contents.txt
(The -E specifies extended regex syntax. egrep
may be used instead of grep -E
here.)
Alternatively you can use sed
to do the colourization manually using ANSI escape codes:
red='c[[1;31m'
default='c[[0m'
sed "s/cat/${red}cat${default}/g" contents.txt
Here the red
and default
shell variables are conveniences only - their values could just as well be placed inline in the sed expression.
A couple more possibilities with grep:
grep for 0 or more instances of cat
:
grep --color '(cat)*' contents.txt
grep for cat
or the empty string:
grep -E --color 'cat|' contents.txt
(The -E specifies extended regex syntax. egrep
may be used instead of grep -E
here.)
Alternatively you can use sed
to do the colourization manually using ANSI escape codes:
red='c[[1;31m'
default='c[[0m'
sed "s/cat/${red}cat${default}/g" contents.txt
Here the red
and default
shell variables are conveniences only - their values could just as well be placed inline in the sed expression.
edited Jan 25 at 22:02
answered Jan 25 at 21:49
Digital TraumaDigital Trauma
5,98211730
5,98211730
add a comment |
add a comment |
I tried with below command
sed -r "/cat/{N;s/^$/&=/}" filename
awk '{if(((length($1)>3)&& ($1 ~ /cat/))||($0~/^$/)){print $0}}' filename
How do these highlight the output?
– wjandrea
Jan 25 at 20:50
add a comment |
I tried with below command
sed -r "/cat/{N;s/^$/&=/}" filename
awk '{if(((length($1)>3)&& ($1 ~ /cat/))||($0~/^$/)){print $0}}' filename
How do these highlight the output?
– wjandrea
Jan 25 at 20:50
add a comment |
I tried with below command
sed -r "/cat/{N;s/^$/&=/}" filename
awk '{if(((length($1)>3)&& ($1 ~ /cat/))||($0~/^$/)){print $0}}' filename
I tried with below command
sed -r "/cat/{N;s/^$/&=/}" filename
awk '{if(((length($1)>3)&& ($1 ~ /cat/))||($0~/^$/)){print $0}}' filename
answered Jan 25 at 19:07
Praveen Kumar BSPraveen Kumar BS
1,6351311
1,6351311
How do these highlight the output?
– wjandrea
Jan 25 at 20:50
add a comment |
How do these highlight the output?
– wjandrea
Jan 25 at 20:50
How do these highlight the output?
– wjandrea
Jan 25 at 20:50
How do these highlight the output?
– wjandrea
Jan 25 at 20:50
add a comment |