Fetch a number of variable length from a file using pattern matching?

The name of the pictureThe name of the pictureThe name of the pictureClash 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.







share|improve this question






















  • 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















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.







share|improve this question






















  • 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













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.







share|improve this question














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.









share|improve this question













share|improve this question




share|improve this question








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 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

















  • 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
















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











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.






share|improve this answer




















  • 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











  • 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











  • 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











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',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















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






























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.






share|improve this answer




















  • 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











  • 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











  • 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















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.






share|improve this answer




















  • 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











  • 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











  • 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













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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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 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










  • 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 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











  • 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











  • 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













 

draft saved


draft discarded


























 


draft saved


draft discarded














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













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?