Unix command to grep only a substring starting with “xyz…” up to the first space?
I have a text:
This is my=test and class=76
This is my=test and class=78
This is my=test2 and class=76
This is my=test3 and class=75
This is my=test1 and class=79.
I want to grep all the word starting with "class=" with the values without printing the whole line the output should be:
class=76
class=78
class=76
class=75
class=79
any command that can help me on this?
I tried this :
grep -E '(^|s+)class=(?=s|$)' file
but was not getting any output.
regex unix grep
add a comment |
I have a text:
This is my=test and class=76
This is my=test and class=78
This is my=test2 and class=76
This is my=test3 and class=75
This is my=test1 and class=79.
I want to grep all the word starting with "class=" with the values without printing the whole line the output should be:
class=76
class=78
class=76
class=75
class=79
any command that can help me on this?
I tried this :
grep -E '(^|s+)class=(?=s|$)' file
but was not getting any output.
regex unix grep
1
Possible duplicate of Can grep show only words that match search pattern?
– tripleee
Nov 22 '18 at 8:39
Possible duplicate of stackoverflow.com/questions/45965192/…
– tripleee
Nov 22 '18 at 8:39
add a comment |
I have a text:
This is my=test and class=76
This is my=test and class=78
This is my=test2 and class=76
This is my=test3 and class=75
This is my=test1 and class=79.
I want to grep all the word starting with "class=" with the values without printing the whole line the output should be:
class=76
class=78
class=76
class=75
class=79
any command that can help me on this?
I tried this :
grep -E '(^|s+)class=(?=s|$)' file
but was not getting any output.
regex unix grep
I have a text:
This is my=test and class=76
This is my=test and class=78
This is my=test2 and class=76
This is my=test3 and class=75
This is my=test1 and class=79.
I want to grep all the word starting with "class=" with the values without printing the whole line the output should be:
class=76
class=78
class=76
class=75
class=79
any command that can help me on this?
I tried this :
grep -E '(^|s+)class=(?=s|$)' file
but was not getting any output.
regex unix grep
regex unix grep
edited Nov 22 '18 at 10:36
Wiktor Stribiżew
319k16140222
319k16140222
asked Nov 22 '18 at 8:20
A. GuptaA. Gupta
277
277
1
Possible duplicate of Can grep show only words that match search pattern?
– tripleee
Nov 22 '18 at 8:39
Possible duplicate of stackoverflow.com/questions/45965192/…
– tripleee
Nov 22 '18 at 8:39
add a comment |
1
Possible duplicate of Can grep show only words that match search pattern?
– tripleee
Nov 22 '18 at 8:39
Possible duplicate of stackoverflow.com/questions/45965192/…
– tripleee
Nov 22 '18 at 8:39
1
1
Possible duplicate of Can grep show only words that match search pattern?
– tripleee
Nov 22 '18 at 8:39
Possible duplicate of Can grep show only words that match search pattern?
– tripleee
Nov 22 '18 at 8:39
Possible duplicate of stackoverflow.com/questions/45965192/…
– tripleee
Nov 22 '18 at 8:39
Possible duplicate of stackoverflow.com/questions/45965192/…
– tripleee
Nov 22 '18 at 8:39
add a comment |
2 Answers
2
active
oldest
votes
Your (^|s+)class=(?=s|$) pattern is not POSIX compliant because it contains a positive lookahead (?=s|$) that is meant to match a location that is followed with a whitespace or end of string position. As you want to match digits right after class=, this lookahead makes no sense even in a PCRE regex. The (^|s+) group is meant to match start of string or 1 or more whitespaces, but it seems a mere word boundary will do here.
You may use
grep -oE '<class=[^ ]+' file
See the online demo
Details
o- enables the output mode, only outputs matches
E- enables POSIX ERE syntax
<- a word boundary (alsobcan be used instead)
class=- a literal string
[^ ]+- 1 or more chars other than space
Equivalent BRE POSIX version:
grep -o '<class=[^ ]*' file
Tested with grep (GNU grep) 2.27.
Thank you so much that worked,Now, lets say i want to get the string after "class=" but before the first space occurence. Then what would be the modified command?
– A. Gupta
Nov 22 '18 at 9:11
grep -oE '<class=[^[:space:]]+' file. OrS+, or[^ ]+.
– Wiktor Stribiżew
Nov 22 '18 at 9:18
add a comment |
Using Perl-oneliner
> data="This is my=test and class=76 This is my=test and class=78 This is my=test2 and n class=76 This is my=test3 and class=75 This is my=test1 and class=79."
> perl -0777 -ne ' { while(/(class=(d+))/g) { print "$1n" } } ' <<< "$data"
class=76
class=78
class=76
class=75
class=79
>
Works, even if you have the data in a file
> echo "$data" > gupta.txt
> perl -0777 -ne ' { while(/(class=(d+))/g) { print "$1n" } } ' gupta.txt
class=76
class=78
class=76
class=75
class=79
>
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f53426555%2funix-command-to-grep-only-a-substring-starting-with-xyz-up-to-the-first-sp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your (^|s+)class=(?=s|$) pattern is not POSIX compliant because it contains a positive lookahead (?=s|$) that is meant to match a location that is followed with a whitespace or end of string position. As you want to match digits right after class=, this lookahead makes no sense even in a PCRE regex. The (^|s+) group is meant to match start of string or 1 or more whitespaces, but it seems a mere word boundary will do here.
You may use
grep -oE '<class=[^ ]+' file
See the online demo
Details
o- enables the output mode, only outputs matches
E- enables POSIX ERE syntax
<- a word boundary (alsobcan be used instead)
class=- a literal string
[^ ]+- 1 or more chars other than space
Equivalent BRE POSIX version:
grep -o '<class=[^ ]*' file
Tested with grep (GNU grep) 2.27.
Thank you so much that worked,Now, lets say i want to get the string after "class=" but before the first space occurence. Then what would be the modified command?
– A. Gupta
Nov 22 '18 at 9:11
grep -oE '<class=[^[:space:]]+' file. OrS+, or[^ ]+.
– Wiktor Stribiżew
Nov 22 '18 at 9:18
add a comment |
Your (^|s+)class=(?=s|$) pattern is not POSIX compliant because it contains a positive lookahead (?=s|$) that is meant to match a location that is followed with a whitespace or end of string position. As you want to match digits right after class=, this lookahead makes no sense even in a PCRE regex. The (^|s+) group is meant to match start of string or 1 or more whitespaces, but it seems a mere word boundary will do here.
You may use
grep -oE '<class=[^ ]+' file
See the online demo
Details
o- enables the output mode, only outputs matches
E- enables POSIX ERE syntax
<- a word boundary (alsobcan be used instead)
class=- a literal string
[^ ]+- 1 or more chars other than space
Equivalent BRE POSIX version:
grep -o '<class=[^ ]*' file
Tested with grep (GNU grep) 2.27.
Thank you so much that worked,Now, lets say i want to get the string after "class=" but before the first space occurence. Then what would be the modified command?
– A. Gupta
Nov 22 '18 at 9:11
grep -oE '<class=[^[:space:]]+' file. OrS+, or[^ ]+.
– Wiktor Stribiżew
Nov 22 '18 at 9:18
add a comment |
Your (^|s+)class=(?=s|$) pattern is not POSIX compliant because it contains a positive lookahead (?=s|$) that is meant to match a location that is followed with a whitespace or end of string position. As you want to match digits right after class=, this lookahead makes no sense even in a PCRE regex. The (^|s+) group is meant to match start of string or 1 or more whitespaces, but it seems a mere word boundary will do here.
You may use
grep -oE '<class=[^ ]+' file
See the online demo
Details
o- enables the output mode, only outputs matches
E- enables POSIX ERE syntax
<- a word boundary (alsobcan be used instead)
class=- a literal string
[^ ]+- 1 or more chars other than space
Equivalent BRE POSIX version:
grep -o '<class=[^ ]*' file
Tested with grep (GNU grep) 2.27.
Your (^|s+)class=(?=s|$) pattern is not POSIX compliant because it contains a positive lookahead (?=s|$) that is meant to match a location that is followed with a whitespace or end of string position. As you want to match digits right after class=, this lookahead makes no sense even in a PCRE regex. The (^|s+) group is meant to match start of string or 1 or more whitespaces, but it seems a mere word boundary will do here.
You may use
grep -oE '<class=[^ ]+' file
See the online demo
Details
o- enables the output mode, only outputs matches
E- enables POSIX ERE syntax
<- a word boundary (alsobcan be used instead)
class=- a literal string
[^ ]+- 1 or more chars other than space
Equivalent BRE POSIX version:
grep -o '<class=[^ ]*' file
Tested with grep (GNU grep) 2.27.
edited Nov 22 '18 at 9:19
answered Nov 22 '18 at 8:25
Wiktor StribiżewWiktor Stribiżew
319k16140222
319k16140222
Thank you so much that worked,Now, lets say i want to get the string after "class=" but before the first space occurence. Then what would be the modified command?
– A. Gupta
Nov 22 '18 at 9:11
grep -oE '<class=[^[:space:]]+' file. OrS+, or[^ ]+.
– Wiktor Stribiżew
Nov 22 '18 at 9:18
add a comment |
Thank you so much that worked,Now, lets say i want to get the string after "class=" but before the first space occurence. Then what would be the modified command?
– A. Gupta
Nov 22 '18 at 9:11
grep -oE '<class=[^[:space:]]+' file. OrS+, or[^ ]+.
– Wiktor Stribiżew
Nov 22 '18 at 9:18
Thank you so much that worked,Now, lets say i want to get the string after "class=" but before the first space occurence. Then what would be the modified command?
– A. Gupta
Nov 22 '18 at 9:11
Thank you so much that worked,Now, lets say i want to get the string after "class=" but before the first space occurence. Then what would be the modified command?
– A. Gupta
Nov 22 '18 at 9:11
grep -oE '<class=[^[:space:]]+' file. Or S+, or [^ ]+.– Wiktor Stribiżew
Nov 22 '18 at 9:18
grep -oE '<class=[^[:space:]]+' file. Or S+, or [^ ]+.– Wiktor Stribiżew
Nov 22 '18 at 9:18
add a comment |
Using Perl-oneliner
> data="This is my=test and class=76 This is my=test and class=78 This is my=test2 and n class=76 This is my=test3 and class=75 This is my=test1 and class=79."
> perl -0777 -ne ' { while(/(class=(d+))/g) { print "$1n" } } ' <<< "$data"
class=76
class=78
class=76
class=75
class=79
>
Works, even if you have the data in a file
> echo "$data" > gupta.txt
> perl -0777 -ne ' { while(/(class=(d+))/g) { print "$1n" } } ' gupta.txt
class=76
class=78
class=76
class=75
class=79
>
add a comment |
Using Perl-oneliner
> data="This is my=test and class=76 This is my=test and class=78 This is my=test2 and n class=76 This is my=test3 and class=75 This is my=test1 and class=79."
> perl -0777 -ne ' { while(/(class=(d+))/g) { print "$1n" } } ' <<< "$data"
class=76
class=78
class=76
class=75
class=79
>
Works, even if you have the data in a file
> echo "$data" > gupta.txt
> perl -0777 -ne ' { while(/(class=(d+))/g) { print "$1n" } } ' gupta.txt
class=76
class=78
class=76
class=75
class=79
>
add a comment |
Using Perl-oneliner
> data="This is my=test and class=76 This is my=test and class=78 This is my=test2 and n class=76 This is my=test3 and class=75 This is my=test1 and class=79."
> perl -0777 -ne ' { while(/(class=(d+))/g) { print "$1n" } } ' <<< "$data"
class=76
class=78
class=76
class=75
class=79
>
Works, even if you have the data in a file
> echo "$data" > gupta.txt
> perl -0777 -ne ' { while(/(class=(d+))/g) { print "$1n" } } ' gupta.txt
class=76
class=78
class=76
class=75
class=79
>
Using Perl-oneliner
> data="This is my=test and class=76 This is my=test and class=78 This is my=test2 and n class=76 This is my=test3 and class=75 This is my=test1 and class=79."
> perl -0777 -ne ' { while(/(class=(d+))/g) { print "$1n" } } ' <<< "$data"
class=76
class=78
class=76
class=75
class=79
>
Works, even if you have the data in a file
> echo "$data" > gupta.txt
> perl -0777 -ne ' { while(/(class=(d+))/g) { print "$1n" } } ' gupta.txt
class=76
class=78
class=76
class=75
class=79
>
answered Nov 22 '18 at 9:24
stack0114106stack0114106
3,8282420
3,8282420
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f53426555%2funix-command-to-grep-only-a-substring-starting-with-xyz-up-to-the-first-sp%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 Can grep show only words that match search pattern?
– tripleee
Nov 22 '18 at 8:39
Possible duplicate of stackoverflow.com/questions/45965192/…
– tripleee
Nov 22 '18 at 8:39