find the Integer value after a string from a file
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{
margin-bottom:0;
}
I want to get the integer value after a specific string from a file and sort all the integers.
Ex -
I have a file with thousands of lines with string weight,
-weight 100
-weight 200
-weight 20
I want to get all the integer values in sorted order.
command-line text-processing
add a comment
|
I want to get the integer value after a specific string from a file and sort all the integers.
Ex -
I have a file with thousands of lines with string weight,
-weight 100
-weight 200
-weight 20
I want to get all the integer values in sorted order.
command-line text-processing
add a comment
|
I want to get the integer value after a specific string from a file and sort all the integers.
Ex -
I have a file with thousands of lines with string weight,
-weight 100
-weight 200
-weight 20
I want to get all the integer values in sorted order.
command-line text-processing
I want to get the integer value after a specific string from a file and sort all the integers.
Ex -
I have a file with thousands of lines with string weight,
-weight 100
-weight 200
-weight 20
I want to get all the integer values in sorted order.
command-line text-processing
command-line text-processing
edited May 29 at 8:00
terdon♦
74.7k14 gold badges151 silver badges235 bronze badges
74.7k14 gold badges151 silver badges235 bronze badges
asked May 28 at 13:11
asad_nitpasad_nitp
132 bronze badges
132 bronze badges
add a comment
|
add a comment
|
3 Answers
3
active
oldest
votes
If you are searching for the longest string of numbers that is the last thing on the line, you can just use grep
:
$ grep -oP 'd+s*$' file
100
200
20
The -o
tells grep
to only print the matching portion of the line and the -P
enables Perl Compatible Regular Expressions. PCREs let us use d+
for "one or more digits" and s*
for "0 or more whitespace characters". So, all together, that command will print the longest stretch of numbers found at the end of the line.
If you need them sorted, just pass through sort
:
$ grep -oP 'd+s*$' file | sort -n
20
100
200
If, instead, you need to anchor your pattern using a specific string, use:
$ grep -oP -- '-weights+Kd+' file | sort -n
20
100
200
The K
tells grep
not to include anything matched up to this point, so the command above will only print the longest stretch of numbers after the -weight
and 0 or more spaces.
Note that if you want to also include negative numbers or decimals, you will need:
grep -oP -- '-weights+K[0-9,-]+' file | sort -n
For example:
$ cat file
-weight 100
-weight 200
-weight 20
-weight -29
-weight -32.4
$ grep -oP -- '-weights+K[0-9,-]+' file | sort -n
-32
-29
20
100
200
i had to remove K to print the output, otherwise there was'nt any output on the screen.
– asad_nitp
May 29 at 6:44
@asad_nitp that doesn't make sense, without theK
you would be getting the whole line.
– terdon♦
May 29 at 8:00
add a comment
|
Try this:
cut -d ' ' -f2 inputfile|sort -n
-d ' '
- set delimiter to space-f2
- get second columnsort -n
- sort result as numbers
Test result:
20
100
200
this works when there are only two columns, so i grep'd the -weight and value and then sorted using this method.
– asad_nitp
May 29 at 6:47
With a correct example answer will be different, so you will not need to grep. Provide an example then ping me, and I will change the answer.
– LeonidMew
May 29 at 8:28
add a comment
|
Try this sed command:
sed -E 's/^.*weights([[:digit:]]*).*/1/' sort.txt | sort -n
Or
sed -E 's/.*weights([0-9]*).*/1/' sort.txt | sort -n
Test case:
-weight 100
-weight 200
-weight 20
Results:
20
100
200
Note:- You didn't give a good test case so I assumed mine.
Info:
^.*weights([[:digit:]])
: Read each line and capture the numbers after the word "weight" with the "space" following.
| sort -n
: Pipe the output into the sort command and sort by numeric value.
For the input shown, something likesed 's/[^0-9]*//'
would be sufficient I think
– steeldriver
May 28 at 14:00
add a comment
|
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
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/4.0/"u003ecc by-sa 4.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%2faskubuntu.com%2fquestions%2f1146841%2ffind-the-integer-value-after-a-string-from-a-file%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
If you are searching for the longest string of numbers that is the last thing on the line, you can just use grep
:
$ grep -oP 'd+s*$' file
100
200
20
The -o
tells grep
to only print the matching portion of the line and the -P
enables Perl Compatible Regular Expressions. PCREs let us use d+
for "one or more digits" and s*
for "0 or more whitespace characters". So, all together, that command will print the longest stretch of numbers found at the end of the line.
If you need them sorted, just pass through sort
:
$ grep -oP 'd+s*$' file | sort -n
20
100
200
If, instead, you need to anchor your pattern using a specific string, use:
$ grep -oP -- '-weights+Kd+' file | sort -n
20
100
200
The K
tells grep
not to include anything matched up to this point, so the command above will only print the longest stretch of numbers after the -weight
and 0 or more spaces.
Note that if you want to also include negative numbers or decimals, you will need:
grep -oP -- '-weights+K[0-9,-]+' file | sort -n
For example:
$ cat file
-weight 100
-weight 200
-weight 20
-weight -29
-weight -32.4
$ grep -oP -- '-weights+K[0-9,-]+' file | sort -n
-32
-29
20
100
200
i had to remove K to print the output, otherwise there was'nt any output on the screen.
– asad_nitp
May 29 at 6:44
@asad_nitp that doesn't make sense, without theK
you would be getting the whole line.
– terdon♦
May 29 at 8:00
add a comment
|
If you are searching for the longest string of numbers that is the last thing on the line, you can just use grep
:
$ grep -oP 'd+s*$' file
100
200
20
The -o
tells grep
to only print the matching portion of the line and the -P
enables Perl Compatible Regular Expressions. PCREs let us use d+
for "one or more digits" and s*
for "0 or more whitespace characters". So, all together, that command will print the longest stretch of numbers found at the end of the line.
If you need them sorted, just pass through sort
:
$ grep -oP 'd+s*$' file | sort -n
20
100
200
If, instead, you need to anchor your pattern using a specific string, use:
$ grep -oP -- '-weights+Kd+' file | sort -n
20
100
200
The K
tells grep
not to include anything matched up to this point, so the command above will only print the longest stretch of numbers after the -weight
and 0 or more spaces.
Note that if you want to also include negative numbers or decimals, you will need:
grep -oP -- '-weights+K[0-9,-]+' file | sort -n
For example:
$ cat file
-weight 100
-weight 200
-weight 20
-weight -29
-weight -32.4
$ grep -oP -- '-weights+K[0-9,-]+' file | sort -n
-32
-29
20
100
200
i had to remove K to print the output, otherwise there was'nt any output on the screen.
– asad_nitp
May 29 at 6:44
@asad_nitp that doesn't make sense, without theK
you would be getting the whole line.
– terdon♦
May 29 at 8:00
add a comment
|
If you are searching for the longest string of numbers that is the last thing on the line, you can just use grep
:
$ grep -oP 'd+s*$' file
100
200
20
The -o
tells grep
to only print the matching portion of the line and the -P
enables Perl Compatible Regular Expressions. PCREs let us use d+
for "one or more digits" and s*
for "0 or more whitespace characters". So, all together, that command will print the longest stretch of numbers found at the end of the line.
If you need them sorted, just pass through sort
:
$ grep -oP 'd+s*$' file | sort -n
20
100
200
If, instead, you need to anchor your pattern using a specific string, use:
$ grep -oP -- '-weights+Kd+' file | sort -n
20
100
200
The K
tells grep
not to include anything matched up to this point, so the command above will only print the longest stretch of numbers after the -weight
and 0 or more spaces.
Note that if you want to also include negative numbers or decimals, you will need:
grep -oP -- '-weights+K[0-9,-]+' file | sort -n
For example:
$ cat file
-weight 100
-weight 200
-weight 20
-weight -29
-weight -32.4
$ grep -oP -- '-weights+K[0-9,-]+' file | sort -n
-32
-29
20
100
200
If you are searching for the longest string of numbers that is the last thing on the line, you can just use grep
:
$ grep -oP 'd+s*$' file
100
200
20
The -o
tells grep
to only print the matching portion of the line and the -P
enables Perl Compatible Regular Expressions. PCREs let us use d+
for "one or more digits" and s*
for "0 or more whitespace characters". So, all together, that command will print the longest stretch of numbers found at the end of the line.
If you need them sorted, just pass through sort
:
$ grep -oP 'd+s*$' file | sort -n
20
100
200
If, instead, you need to anchor your pattern using a specific string, use:
$ grep -oP -- '-weights+Kd+' file | sort -n
20
100
200
The K
tells grep
not to include anything matched up to this point, so the command above will only print the longest stretch of numbers after the -weight
and 0 or more spaces.
Note that if you want to also include negative numbers or decimals, you will need:
grep -oP -- '-weights+K[0-9,-]+' file | sort -n
For example:
$ cat file
-weight 100
-weight 200
-weight 20
-weight -29
-weight -32.4
$ grep -oP -- '-weights+K[0-9,-]+' file | sort -n
-32
-29
20
100
200
answered May 28 at 14:34
terdon♦terdon
74.7k14 gold badges151 silver badges235 bronze badges
74.7k14 gold badges151 silver badges235 bronze badges
i had to remove K to print the output, otherwise there was'nt any output on the screen.
– asad_nitp
May 29 at 6:44
@asad_nitp that doesn't make sense, without theK
you would be getting the whole line.
– terdon♦
May 29 at 8:00
add a comment
|
i had to remove K to print the output, otherwise there was'nt any output on the screen.
– asad_nitp
May 29 at 6:44
@asad_nitp that doesn't make sense, without theK
you would be getting the whole line.
– terdon♦
May 29 at 8:00
i had to remove K to print the output, otherwise there was'nt any output on the screen.
– asad_nitp
May 29 at 6:44
i had to remove K to print the output, otherwise there was'nt any output on the screen.
– asad_nitp
May 29 at 6:44
@asad_nitp that doesn't make sense, without the
K
you would be getting the whole line.– terdon♦
May 29 at 8:00
@asad_nitp that doesn't make sense, without the
K
you would be getting the whole line.– terdon♦
May 29 at 8:00
add a comment
|
Try this:
cut -d ' ' -f2 inputfile|sort -n
-d ' '
- set delimiter to space-f2
- get second columnsort -n
- sort result as numbers
Test result:
20
100
200
this works when there are only two columns, so i grep'd the -weight and value and then sorted using this method.
– asad_nitp
May 29 at 6:47
With a correct example answer will be different, so you will not need to grep. Provide an example then ping me, and I will change the answer.
– LeonidMew
May 29 at 8:28
add a comment
|
Try this:
cut -d ' ' -f2 inputfile|sort -n
-d ' '
- set delimiter to space-f2
- get second columnsort -n
- sort result as numbers
Test result:
20
100
200
this works when there are only two columns, so i grep'd the -weight and value and then sorted using this method.
– asad_nitp
May 29 at 6:47
With a correct example answer will be different, so you will not need to grep. Provide an example then ping me, and I will change the answer.
– LeonidMew
May 29 at 8:28
add a comment
|
Try this:
cut -d ' ' -f2 inputfile|sort -n
-d ' '
- set delimiter to space-f2
- get second columnsort -n
- sort result as numbers
Test result:
20
100
200
Try this:
cut -d ' ' -f2 inputfile|sort -n
-d ' '
- set delimiter to space-f2
- get second columnsort -n
- sort result as numbers
Test result:
20
100
200
edited May 28 at 13:59
answered May 28 at 13:51
LeonidMewLeonidMew
1,99012 silver badges28 bronze badges
1,99012 silver badges28 bronze badges
this works when there are only two columns, so i grep'd the -weight and value and then sorted using this method.
– asad_nitp
May 29 at 6:47
With a correct example answer will be different, so you will not need to grep. Provide an example then ping me, and I will change the answer.
– LeonidMew
May 29 at 8:28
add a comment
|
this works when there are only two columns, so i grep'd the -weight and value and then sorted using this method.
– asad_nitp
May 29 at 6:47
With a correct example answer will be different, so you will not need to grep. Provide an example then ping me, and I will change the answer.
– LeonidMew
May 29 at 8:28
this works when there are only two columns, so i grep'd the -weight and value and then sorted using this method.
– asad_nitp
May 29 at 6:47
this works when there are only two columns, so i grep'd the -weight and value and then sorted using this method.
– asad_nitp
May 29 at 6:47
With a correct example answer will be different, so you will not need to grep. Provide an example then ping me, and I will change the answer.
– LeonidMew
May 29 at 8:28
With a correct example answer will be different, so you will not need to grep. Provide an example then ping me, and I will change the answer.
– LeonidMew
May 29 at 8:28
add a comment
|
Try this sed command:
sed -E 's/^.*weights([[:digit:]]*).*/1/' sort.txt | sort -n
Or
sed -E 's/.*weights([0-9]*).*/1/' sort.txt | sort -n
Test case:
-weight 100
-weight 200
-weight 20
Results:
20
100
200
Note:- You didn't give a good test case so I assumed mine.
Info:
^.*weights([[:digit:]])
: Read each line and capture the numbers after the word "weight" with the "space" following.
| sort -n
: Pipe the output into the sort command and sort by numeric value.
For the input shown, something likesed 's/[^0-9]*//'
would be sufficient I think
– steeldriver
May 28 at 14:00
add a comment
|
Try this sed command:
sed -E 's/^.*weights([[:digit:]]*).*/1/' sort.txt | sort -n
Or
sed -E 's/.*weights([0-9]*).*/1/' sort.txt | sort -n
Test case:
-weight 100
-weight 200
-weight 20
Results:
20
100
200
Note:- You didn't give a good test case so I assumed mine.
Info:
^.*weights([[:digit:]])
: Read each line and capture the numbers after the word "weight" with the "space" following.
| sort -n
: Pipe the output into the sort command and sort by numeric value.
For the input shown, something likesed 's/[^0-9]*//'
would be sufficient I think
– steeldriver
May 28 at 14:00
add a comment
|
Try this sed command:
sed -E 's/^.*weights([[:digit:]]*).*/1/' sort.txt | sort -n
Or
sed -E 's/.*weights([0-9]*).*/1/' sort.txt | sort -n
Test case:
-weight 100
-weight 200
-weight 20
Results:
20
100
200
Note:- You didn't give a good test case so I assumed mine.
Info:
^.*weights([[:digit:]])
: Read each line and capture the numbers after the word "weight" with the "space" following.
| sort -n
: Pipe the output into the sort command and sort by numeric value.
Try this sed command:
sed -E 's/^.*weights([[:digit:]]*).*/1/' sort.txt | sort -n
Or
sed -E 's/.*weights([0-9]*).*/1/' sort.txt | sort -n
Test case:
-weight 100
-weight 200
-weight 20
Results:
20
100
200
Note:- You didn't give a good test case so I assumed mine.
Info:
^.*weights([[:digit:]])
: Read each line and capture the numbers after the word "weight" with the "space" following.
| sort -n
: Pipe the output into the sort command and sort by numeric value.
edited May 28 at 14:07
answered May 28 at 13:31
George UdosenGeorge Udosen
24.3k11 gold badges60 silver badges82 bronze badges
24.3k11 gold badges60 silver badges82 bronze badges
For the input shown, something likesed 's/[^0-9]*//'
would be sufficient I think
– steeldriver
May 28 at 14:00
add a comment
|
For the input shown, something likesed 's/[^0-9]*//'
would be sufficient I think
– steeldriver
May 28 at 14:00
For the input shown, something like
sed 's/[^0-9]*//'
would be sufficient I think– steeldriver
May 28 at 14:00
For the input shown, something like
sed 's/[^0-9]*//'
would be sufficient I think– steeldriver
May 28 at 14:00
add a comment
|
Thanks for contributing an answer to Ask Ubuntu!
- 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%2faskubuntu.com%2fquestions%2f1146841%2ffind-the-integer-value-after-a-string-from-a-file%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