grep or other regexp to get value of output
How to use grep or any other tool to get a specific value in an output
In the below output I need to get the value 255.00 in the line with Minimum: 255.00 (1.0000)
A pattern like: Channel Statistics:s+Gray:s+Minimum: +([d.]+)
Image: test.tif
Format: TIFF (Tagged Image File Format)
Geometry: 2525x1785
Class: DirectClass
Type: bilevel
Depth: 1 bits-per-pixel component
Channel Depths:
Gray: 1 bits
Channel Statistics:
Gray:
Minimum: 255.00 (1.0000)
Maximum: 255.00 (1.0000)
Mean: 255.00 (1.0000)
Standard Deviation: 0.00 (0.0000)
Filesize: 581
Interlace: No
Orientation: Unknown
Background Color: white
Border Color: #DFDFDF
Matte Color: #BDBDBD
linux debian regex grep
add a comment |
How to use grep or any other tool to get a specific value in an output
In the below output I need to get the value 255.00 in the line with Minimum: 255.00 (1.0000)
A pattern like: Channel Statistics:s+Gray:s+Minimum: +([d.]+)
Image: test.tif
Format: TIFF (Tagged Image File Format)
Geometry: 2525x1785
Class: DirectClass
Type: bilevel
Depth: 1 bits-per-pixel component
Channel Depths:
Gray: 1 bits
Channel Statistics:
Gray:
Minimum: 255.00 (1.0000)
Maximum: 255.00 (1.0000)
Mean: 255.00 (1.0000)
Standard Deviation: 0.00 (0.0000)
Filesize: 581
Interlace: No
Orientation: Unknown
Background Color: white
Border Color: #DFDFDF
Matte Color: #BDBDBD
linux debian regex grep
add a comment |
How to use grep or any other tool to get a specific value in an output
In the below output I need to get the value 255.00 in the line with Minimum: 255.00 (1.0000)
A pattern like: Channel Statistics:s+Gray:s+Minimum: +([d.]+)
Image: test.tif
Format: TIFF (Tagged Image File Format)
Geometry: 2525x1785
Class: DirectClass
Type: bilevel
Depth: 1 bits-per-pixel component
Channel Depths:
Gray: 1 bits
Channel Statistics:
Gray:
Minimum: 255.00 (1.0000)
Maximum: 255.00 (1.0000)
Mean: 255.00 (1.0000)
Standard Deviation: 0.00 (0.0000)
Filesize: 581
Interlace: No
Orientation: Unknown
Background Color: white
Border Color: #DFDFDF
Matte Color: #BDBDBD
linux debian regex grep
How to use grep or any other tool to get a specific value in an output
In the below output I need to get the value 255.00 in the line with Minimum: 255.00 (1.0000)
A pattern like: Channel Statistics:s+Gray:s+Minimum: +([d.]+)
Image: test.tif
Format: TIFF (Tagged Image File Format)
Geometry: 2525x1785
Class: DirectClass
Type: bilevel
Depth: 1 bits-per-pixel component
Channel Depths:
Gray: 1 bits
Channel Statistics:
Gray:
Minimum: 255.00 (1.0000)
Maximum: 255.00 (1.0000)
Mean: 255.00 (1.0000)
Standard Deviation: 0.00 (0.0000)
Filesize: 581
Interlace: No
Orientation: Unknown
Background Color: white
Border Color: #DFDFDF
Matte Color: #BDBDBD
linux debian regex grep
linux debian regex grep
asked Dec 1 at 11:24
clarkk
941313
941313
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Using perl, you can do the following. It captures the numeric value after minimum: inside the block Channel Statistics: and prints it:
perl -0 -ne '/Channel Statistics:s+Gray:s+Minimum:h+([d.]+)/ && print $1,"n"' file
Output: (for given example)
255.00
Explanation:
-0 # specifies the input record separator. If there are no digits, the null character is the separator. The whole file is read in a single string.
-n # Iterate over the file
-e # execute the command line
Regex:
/ # regex delimiter
Channel Statistics: # literally
s+ # 1 or more any kind of spaces
Gray: # literally
s+ # 1 or more any kind of spaces
Minimum: # literally
h+ # 1 or more horizontal spaces
( # start group 1
[d.]+ # 1 or more digit or dot
) # end group
/ # regex delimiter
add a comment |
With sed
sed -rn 's/^s+Minimum:s+([0-9.]+).+$/1/p' image.data
In slow-mo:
-rtellssedwe use th extened regexp "syntax"
-ntellssedto not print non-matching lines
s/^s+Minimum:s+([0-9.]+).+$/1/matches your target line and replaces it with just the value you are after
ptellssedto print the result
If you need to disambiguate by taking in account the content of previous lines, it is just slightly more complicated:
sed -r ':a;N;$!ba; s/^.*Gray:s*ns+Minimum:s+([0-9.]+).+$/1/' image.data
where:
:a;N;$!ba;is a loop in thesedlanguage that loads the whole file at once
-nis no longer necessary since there are no other lines we don't want to print- the final
pis no longer necessary either since we don't use-n
add a comment |
This is very simplistic, assuming that the string 'Minimum:' occurs exactly once in your input:
awk '/Minimum:/ {print $2}'
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
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%2fsuperuser.com%2fquestions%2f1379939%2fgrep-or-other-regexp-to-get-value-of-output%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
Using perl, you can do the following. It captures the numeric value after minimum: inside the block Channel Statistics: and prints it:
perl -0 -ne '/Channel Statistics:s+Gray:s+Minimum:h+([d.]+)/ && print $1,"n"' file
Output: (for given example)
255.00
Explanation:
-0 # specifies the input record separator. If there are no digits, the null character is the separator. The whole file is read in a single string.
-n # Iterate over the file
-e # execute the command line
Regex:
/ # regex delimiter
Channel Statistics: # literally
s+ # 1 or more any kind of spaces
Gray: # literally
s+ # 1 or more any kind of spaces
Minimum: # literally
h+ # 1 or more horizontal spaces
( # start group 1
[d.]+ # 1 or more digit or dot
) # end group
/ # regex delimiter
add a comment |
Using perl, you can do the following. It captures the numeric value after minimum: inside the block Channel Statistics: and prints it:
perl -0 -ne '/Channel Statistics:s+Gray:s+Minimum:h+([d.]+)/ && print $1,"n"' file
Output: (for given example)
255.00
Explanation:
-0 # specifies the input record separator. If there are no digits, the null character is the separator. The whole file is read in a single string.
-n # Iterate over the file
-e # execute the command line
Regex:
/ # regex delimiter
Channel Statistics: # literally
s+ # 1 or more any kind of spaces
Gray: # literally
s+ # 1 or more any kind of spaces
Minimum: # literally
h+ # 1 or more horizontal spaces
( # start group 1
[d.]+ # 1 or more digit or dot
) # end group
/ # regex delimiter
add a comment |
Using perl, you can do the following. It captures the numeric value after minimum: inside the block Channel Statistics: and prints it:
perl -0 -ne '/Channel Statistics:s+Gray:s+Minimum:h+([d.]+)/ && print $1,"n"' file
Output: (for given example)
255.00
Explanation:
-0 # specifies the input record separator. If there are no digits, the null character is the separator. The whole file is read in a single string.
-n # Iterate over the file
-e # execute the command line
Regex:
/ # regex delimiter
Channel Statistics: # literally
s+ # 1 or more any kind of spaces
Gray: # literally
s+ # 1 or more any kind of spaces
Minimum: # literally
h+ # 1 or more horizontal spaces
( # start group 1
[d.]+ # 1 or more digit or dot
) # end group
/ # regex delimiter
Using perl, you can do the following. It captures the numeric value after minimum: inside the block Channel Statistics: and prints it:
perl -0 -ne '/Channel Statistics:s+Gray:s+Minimum:h+([d.]+)/ && print $1,"n"' file
Output: (for given example)
255.00
Explanation:
-0 # specifies the input record separator. If there are no digits, the null character is the separator. The whole file is read in a single string.
-n # Iterate over the file
-e # execute the command line
Regex:
/ # regex delimiter
Channel Statistics: # literally
s+ # 1 or more any kind of spaces
Gray: # literally
s+ # 1 or more any kind of spaces
Minimum: # literally
h+ # 1 or more horizontal spaces
( # start group 1
[d.]+ # 1 or more digit or dot
) # end group
/ # regex delimiter
answered Dec 1 at 13:11
Toto
3,40591226
3,40591226
add a comment |
add a comment |
With sed
sed -rn 's/^s+Minimum:s+([0-9.]+).+$/1/p' image.data
In slow-mo:
-rtellssedwe use th extened regexp "syntax"
-ntellssedto not print non-matching lines
s/^s+Minimum:s+([0-9.]+).+$/1/matches your target line and replaces it with just the value you are after
ptellssedto print the result
If you need to disambiguate by taking in account the content of previous lines, it is just slightly more complicated:
sed -r ':a;N;$!ba; s/^.*Gray:s*ns+Minimum:s+([0-9.]+).+$/1/' image.data
where:
:a;N;$!ba;is a loop in thesedlanguage that loads the whole file at once
-nis no longer necessary since there are no other lines we don't want to print- the final
pis no longer necessary either since we don't use-n
add a comment |
With sed
sed -rn 's/^s+Minimum:s+([0-9.]+).+$/1/p' image.data
In slow-mo:
-rtellssedwe use th extened regexp "syntax"
-ntellssedto not print non-matching lines
s/^s+Minimum:s+([0-9.]+).+$/1/matches your target line and replaces it with just the value you are after
ptellssedto print the result
If you need to disambiguate by taking in account the content of previous lines, it is just slightly more complicated:
sed -r ':a;N;$!ba; s/^.*Gray:s*ns+Minimum:s+([0-9.]+).+$/1/' image.data
where:
:a;N;$!ba;is a loop in thesedlanguage that loads the whole file at once
-nis no longer necessary since there are no other lines we don't want to print- the final
pis no longer necessary either since we don't use-n
add a comment |
With sed
sed -rn 's/^s+Minimum:s+([0-9.]+).+$/1/p' image.data
In slow-mo:
-rtellssedwe use th extened regexp "syntax"
-ntellssedto not print non-matching lines
s/^s+Minimum:s+([0-9.]+).+$/1/matches your target line and replaces it with just the value you are after
ptellssedto print the result
If you need to disambiguate by taking in account the content of previous lines, it is just slightly more complicated:
sed -r ':a;N;$!ba; s/^.*Gray:s*ns+Minimum:s+([0-9.]+).+$/1/' image.data
where:
:a;N;$!ba;is a loop in thesedlanguage that loads the whole file at once
-nis no longer necessary since there are no other lines we don't want to print- the final
pis no longer necessary either since we don't use-n
With sed
sed -rn 's/^s+Minimum:s+([0-9.]+).+$/1/p' image.data
In slow-mo:
-rtellssedwe use th extened regexp "syntax"
-ntellssedto not print non-matching lines
s/^s+Minimum:s+([0-9.]+).+$/1/matches your target line and replaces it with just the value you are after
ptellssedto print the result
If you need to disambiguate by taking in account the content of previous lines, it is just slightly more complicated:
sed -r ':a;N;$!ba; s/^.*Gray:s*ns+Minimum:s+([0-9.]+).+$/1/' image.data
where:
:a;N;$!ba;is a loop in thesedlanguage that loads the whole file at once
-nis no longer necessary since there are no other lines we don't want to print- the final
pis no longer necessary either since we don't use-n
edited Dec 1 at 17:17
answered Dec 1 at 16:49
xenoid
3,5883718
3,5883718
add a comment |
add a comment |
This is very simplistic, assuming that the string 'Minimum:' occurs exactly once in your input:
awk '/Minimum:/ {print $2}'
add a comment |
This is very simplistic, assuming that the string 'Minimum:' occurs exactly once in your input:
awk '/Minimum:/ {print $2}'
add a comment |
This is very simplistic, assuming that the string 'Minimum:' occurs exactly once in your input:
awk '/Minimum:/ {print $2}'
This is very simplistic, assuming that the string 'Minimum:' occurs exactly once in your input:
awk '/Minimum:/ {print $2}'
answered 2 days ago
Jim L.
913
913
add a comment |
add a comment |
Thanks for contributing an answer to Super User!
- 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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2fsuperuser.com%2fquestions%2f1379939%2fgrep-or-other-regexp-to-get-value-of-output%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