highlight a word without affecting the structure of text
$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
add a comment |
$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
1
Possible duplicate of Convince grep to output all lines, not just those with matches
– steeldriver
1 hour ago
add a comment |
$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
$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
awk sed grep
edited 2 hours ago
user607694
asked 3 hours ago
user607694user607694
354
354
1
Possible duplicate of Convince grep to output all lines, not just those with matches
– steeldriver
1 hour ago
add a comment |
1
Possible duplicate of Convince grep to output all lines, not just those with matches
– steeldriver
1 hour ago
1
1
Possible duplicate of Convince grep to output all lines, not just those with matches
– steeldriver
1 hour ago
Possible duplicate of Convince grep to output all lines, not just those with matches
– steeldriver
1 hour ago
add a comment |
3 Answers
3
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
1
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
17 mins ago
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 |
I tried with below command
sed -r "/cat/{N;s/^$/&=/}" filename
awk '{if(((length($1)>3)&& ($1 ~ /cat/))||($0~/^$/)){print $0}}' filename
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f496717%2fhighlight-a-word-without-affecting-the-structure-of-text%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
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
1
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
17 mins ago
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
1
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
17 mins ago
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 2 hours ago
Jesse_bJesse_b
12.3k23065
12.3k23065
1
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
17 mins ago
add a comment |
1
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
17 mins ago
1
1
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
17 mins ago
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
17 mins ago
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.
answered 2 hours ago
HaxielHaxiel
1,8401510
1,8401510
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
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
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 1 hour ago
Praveen Kumar BSPraveen Kumar BS
1,366138
1,366138
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f496717%2fhighlight-a-word-without-affecting-the-structure-of-text%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Possible duplicate of Convince grep to output all lines, not just those with matches
– steeldriver
1 hour ago