Fetch a number of variable length from a file using pattern matching?
Clash Royale CLAN TAG#URR8PPP
up vote
-1
down vote
favorite
I have many files on UNIX and wish to fetch number in that file associated with a specified pattern.
Most of the file will have a unique pattern in file like below
some text abc
some text abc
some text abc
(3 rows)
The number could vary from file to file, it could be 35644 or any numeric value as well.
I want to find that number using grep or sed not awk. so far I was able to get this. I want to do this using single line unix command including redirection if applicable.
grep 'rows' file.txt
Edit - Removed the requirement if pattern is not found what should happen.
text-processing sed grep hp-ux
add a comment |Â
up vote
-1
down vote
favorite
I have many files on UNIX and wish to fetch number in that file associated with a specified pattern.
Most of the file will have a unique pattern in file like below
some text abc
some text abc
some text abc
(3 rows)
The number could vary from file to file, it could be 35644 or any numeric value as well.
I want to find that number using grep or sed not awk. so far I was able to get this. I want to do this using single line unix command including redirection if applicable.
grep 'rows' file.txt
Edit - Removed the requirement if pattern is not found what should happen.
text-processing sed grep hp-ux
So, are you saying that your example file is four lines long, and the fourth line contains the text(3â¯rows)
?â And that you want to extract the3
from the(3â¯rows)
line (regardless of the actual number of lines in the file)?â Do you guarantee that only one row in the file contains the wordrow
â or that only one line will begin with(
, followed by a sequence of digits, followed by a space androws)
?âÂÂ(If there are only two lines in the file, will the second one say(1â¯row)
or(1â¯rows)
?)âÂÂPlease do not respond in comments; edit your question to make it clearer and more complete.
â G-Man
Jan 22 at 6:34
That one row will begin with(numbercount rows)
and will have this pattern. It might possible rows might come as string somewhere else but not as this pattern(numbercount rows)
.
â Minsec
Jan 22 at 6:44
add a comment |Â
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I have many files on UNIX and wish to fetch number in that file associated with a specified pattern.
Most of the file will have a unique pattern in file like below
some text abc
some text abc
some text abc
(3 rows)
The number could vary from file to file, it could be 35644 or any numeric value as well.
I want to find that number using grep or sed not awk. so far I was able to get this. I want to do this using single line unix command including redirection if applicable.
grep 'rows' file.txt
Edit - Removed the requirement if pattern is not found what should happen.
text-processing sed grep hp-ux
I have many files on UNIX and wish to fetch number in that file associated with a specified pattern.
Most of the file will have a unique pattern in file like below
some text abc
some text abc
some text abc
(3 rows)
The number could vary from file to file, it could be 35644 or any numeric value as well.
I want to find that number using grep or sed not awk. so far I was able to get this. I want to do this using single line unix command including redirection if applicable.
grep 'rows' file.txt
Edit - Removed the requirement if pattern is not found what should happen.
text-processing sed grep hp-ux
edited Jan 22 at 7:04
cas
37.7k44393
37.7k44393
asked Jan 22 at 6:15
Minsec
11
11
So, are you saying that your example file is four lines long, and the fourth line contains the text(3â¯rows)
?â And that you want to extract the3
from the(3â¯rows)
line (regardless of the actual number of lines in the file)?â Do you guarantee that only one row in the file contains the wordrow
â or that only one line will begin with(
, followed by a sequence of digits, followed by a space androws)
?âÂÂ(If there are only two lines in the file, will the second one say(1â¯row)
or(1â¯rows)
?)âÂÂPlease do not respond in comments; edit your question to make it clearer and more complete.
â G-Man
Jan 22 at 6:34
That one row will begin with(numbercount rows)
and will have this pattern. It might possible rows might come as string somewhere else but not as this pattern(numbercount rows)
.
â Minsec
Jan 22 at 6:44
add a comment |Â
So, are you saying that your example file is four lines long, and the fourth line contains the text(3â¯rows)
?â And that you want to extract the3
from the(3â¯rows)
line (regardless of the actual number of lines in the file)?â Do you guarantee that only one row in the file contains the wordrow
â or that only one line will begin with(
, followed by a sequence of digits, followed by a space androws)
?âÂÂ(If there are only two lines in the file, will the second one say(1â¯row)
or(1â¯rows)
?)âÂÂPlease do not respond in comments; edit your question to make it clearer and more complete.
â G-Man
Jan 22 at 6:34
That one row will begin with(numbercount rows)
and will have this pattern. It might possible rows might come as string somewhere else but not as this pattern(numbercount rows)
.
â Minsec
Jan 22 at 6:44
So, are you saying that your example file is four lines long, and the fourth line contains the text
(3â¯rows)
?â And that you want to extract the 3
from the (3â¯rows)
line (regardless of the actual number of lines in the file)?â Do you guarantee that only one row in the file contains the word row
â or that only one line will begin with (
, followed by a sequence of digits, followed by a space and rows)
?âÂÂ(If there are only two lines in the file, will the second one say (1â¯row)
or (1â¯rows)
?)âÂÂPlease do not respond in comments; edit your question to make it clearer and more complete.â G-Man
Jan 22 at 6:34
So, are you saying that your example file is four lines long, and the fourth line contains the text
(3â¯rows)
?â And that you want to extract the 3
from the (3â¯rows)
line (regardless of the actual number of lines in the file)?â Do you guarantee that only one row in the file contains the word row
â or that only one line will begin with (
, followed by a sequence of digits, followed by a space and rows)
?âÂÂ(If there are only two lines in the file, will the second one say (1â¯row)
or (1â¯rows)
?)âÂÂPlease do not respond in comments; edit your question to make it clearer and more complete.â G-Man
Jan 22 at 6:34
That one row will begin with
(numbercount rows)
and will have this pattern. It might possible rows might come as string somewhere else but not as this pattern (numbercount rows)
.â Minsec
Jan 22 at 6:44
That one row will begin with
(numbercount rows)
and will have this pattern. It might possible rows might come as string somewhere else but not as this pattern (numbercount rows)
.â Minsec
Jan 22 at 6:44
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
0
down vote
sed -n -e 's/^(([0-9]+) rows)$/1/p'
This prints the digits immediately following the (
on lines that contain only (nnn rows)
(where nnn is any positive integer).
No other lines are printed.
I tried this by specifying file name with pattern didn'print anything. sed -n -e's/^(([0-9]+) rows)$/1/p' file.txt
â Minsec
Jan 22 at 6:54
then your file doesn't match what you specified in your question. did your files come from a windows machine? (i.e. do they have CR+LF line endings rather than unix-style LF-only?). Try addingr?
immediately before the$
in the sed script - that will match either unix or windows line-endings. or convert them to unix text files withfromdos
or similar.
â cas
Jan 22 at 6:58
file is created in unix only with LF style.
â Minsec
Jan 22 at 7:00
then what you stated in your question is incorrect - it does not accurately describe your files. I tested it before I posted using the sample text you provided and it printed3
.
â cas
Jan 22 at 7:01
I'm on HP UNIX will sed command differ all versions of UNIX? As command is getting executed successfully but doesn't print anything. My requirement is clear and same as what I have posted.
â Minsec
Jan 22 at 7:03
 |Â
show 4 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
sed -n -e 's/^(([0-9]+) rows)$/1/p'
This prints the digits immediately following the (
on lines that contain only (nnn rows)
(where nnn is any positive integer).
No other lines are printed.
I tried this by specifying file name with pattern didn'print anything. sed -n -e's/^(([0-9]+) rows)$/1/p' file.txt
â Minsec
Jan 22 at 6:54
then your file doesn't match what you specified in your question. did your files come from a windows machine? (i.e. do they have CR+LF line endings rather than unix-style LF-only?). Try addingr?
immediately before the$
in the sed script - that will match either unix or windows line-endings. or convert them to unix text files withfromdos
or similar.
â cas
Jan 22 at 6:58
file is created in unix only with LF style.
â Minsec
Jan 22 at 7:00
then what you stated in your question is incorrect - it does not accurately describe your files. I tested it before I posted using the sample text you provided and it printed3
.
â cas
Jan 22 at 7:01
I'm on HP UNIX will sed command differ all versions of UNIX? As command is getting executed successfully but doesn't print anything. My requirement is clear and same as what I have posted.
â Minsec
Jan 22 at 7:03
 |Â
show 4 more comments
up vote
0
down vote
sed -n -e 's/^(([0-9]+) rows)$/1/p'
This prints the digits immediately following the (
on lines that contain only (nnn rows)
(where nnn is any positive integer).
No other lines are printed.
I tried this by specifying file name with pattern didn'print anything. sed -n -e's/^(([0-9]+) rows)$/1/p' file.txt
â Minsec
Jan 22 at 6:54
then your file doesn't match what you specified in your question. did your files come from a windows machine? (i.e. do they have CR+LF line endings rather than unix-style LF-only?). Try addingr?
immediately before the$
in the sed script - that will match either unix or windows line-endings. or convert them to unix text files withfromdos
or similar.
â cas
Jan 22 at 6:58
file is created in unix only with LF style.
â Minsec
Jan 22 at 7:00
then what you stated in your question is incorrect - it does not accurately describe your files. I tested it before I posted using the sample text you provided and it printed3
.
â cas
Jan 22 at 7:01
I'm on HP UNIX will sed command differ all versions of UNIX? As command is getting executed successfully but doesn't print anything. My requirement is clear and same as what I have posted.
â Minsec
Jan 22 at 7:03
 |Â
show 4 more comments
up vote
0
down vote
up vote
0
down vote
sed -n -e 's/^(([0-9]+) rows)$/1/p'
This prints the digits immediately following the (
on lines that contain only (nnn rows)
(where nnn is any positive integer).
No other lines are printed.
sed -n -e 's/^(([0-9]+) rows)$/1/p'
This prints the digits immediately following the (
on lines that contain only (nnn rows)
(where nnn is any positive integer).
No other lines are printed.
answered Jan 22 at 6:50
cas
37.7k44393
37.7k44393
I tried this by specifying file name with pattern didn'print anything. sed -n -e's/^(([0-9]+) rows)$/1/p' file.txt
â Minsec
Jan 22 at 6:54
then your file doesn't match what you specified in your question. did your files come from a windows machine? (i.e. do they have CR+LF line endings rather than unix-style LF-only?). Try addingr?
immediately before the$
in the sed script - that will match either unix or windows line-endings. or convert them to unix text files withfromdos
or similar.
â cas
Jan 22 at 6:58
file is created in unix only with LF style.
â Minsec
Jan 22 at 7:00
then what you stated in your question is incorrect - it does not accurately describe your files. I tested it before I posted using the sample text you provided and it printed3
.
â cas
Jan 22 at 7:01
I'm on HP UNIX will sed command differ all versions of UNIX? As command is getting executed successfully but doesn't print anything. My requirement is clear and same as what I have posted.
â Minsec
Jan 22 at 7:03
 |Â
show 4 more comments
I tried this by specifying file name with pattern didn'print anything. sed -n -e's/^(([0-9]+) rows)$/1/p' file.txt
â Minsec
Jan 22 at 6:54
then your file doesn't match what you specified in your question. did your files come from a windows machine? (i.e. do they have CR+LF line endings rather than unix-style LF-only?). Try addingr?
immediately before the$
in the sed script - that will match either unix or windows line-endings. or convert them to unix text files withfromdos
or similar.
â cas
Jan 22 at 6:58
file is created in unix only with LF style.
â Minsec
Jan 22 at 7:00
then what you stated in your question is incorrect - it does not accurately describe your files. I tested it before I posted using the sample text you provided and it printed3
.
â cas
Jan 22 at 7:01
I'm on HP UNIX will sed command differ all versions of UNIX? As command is getting executed successfully but doesn't print anything. My requirement is clear and same as what I have posted.
â Minsec
Jan 22 at 7:03
I tried this by specifying file name with pattern didn'print anything. sed -n -e
's/^(([0-9]+) rows)$/1/p' file.txt
â Minsec
Jan 22 at 6:54
I tried this by specifying file name with pattern didn'print anything. sed -n -e
's/^(([0-9]+) rows)$/1/p' file.txt
â Minsec
Jan 22 at 6:54
then your file doesn't match what you specified in your question. did your files come from a windows machine? (i.e. do they have CR+LF line endings rather than unix-style LF-only?). Try adding
r?
immediately before the $
in the sed script - that will match either unix or windows line-endings. or convert them to unix text files with fromdos
or similar.â cas
Jan 22 at 6:58
then your file doesn't match what you specified in your question. did your files come from a windows machine? (i.e. do they have CR+LF line endings rather than unix-style LF-only?). Try adding
r?
immediately before the $
in the sed script - that will match either unix or windows line-endings. or convert them to unix text files with fromdos
or similar.â cas
Jan 22 at 6:58
file is created in unix only with LF style.
â Minsec
Jan 22 at 7:00
file is created in unix only with LF style.
â Minsec
Jan 22 at 7:00
then what you stated in your question is incorrect - it does not accurately describe your files. I tested it before I posted using the sample text you provided and it printed
3
.â cas
Jan 22 at 7:01
then what you stated in your question is incorrect - it does not accurately describe your files. I tested it before I posted using the sample text you provided and it printed
3
.â cas
Jan 22 at 7:01
I'm on HP UNIX will sed command differ all versions of UNIX? As command is getting executed successfully but doesn't print anything. My requirement is clear and same as what I have posted.
â Minsec
Jan 22 at 7:03
I'm on HP UNIX will sed command differ all versions of UNIX? As command is getting executed successfully but doesn't print anything. My requirement is clear and same as what I have posted.
â Minsec
Jan 22 at 7:03
 |Â
show 4 more comments
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f418758%2ffetch-a-number-of-variable-length-from-a-file-using-pattern-matching%23new-answer', 'question_page');
);
Post as a guest
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
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
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
So, are you saying that your example file is four lines long, and the fourth line contains the text
(3â¯rows)
?â And that you want to extract the3
from the(3â¯rows)
line (regardless of the actual number of lines in the file)?â Do you guarantee that only one row in the file contains the wordrow
â or that only one line will begin with(
, followed by a sequence of digits, followed by a space androws)
?âÂÂ(If there are only two lines in the file, will the second one say(1â¯row)
or(1â¯rows)
?)âÂÂPlease do not respond in comments; edit your question to make it clearer and more complete.â G-Man
Jan 22 at 6:34
That one row will begin with
(numbercount rows)
and will have this pattern. It might possible rows might come as string somewhere else but not as this pattern(numbercount rows)
.â Minsec
Jan 22 at 6:44