Use -grep to count number of 6-letter words start with 'bar' and ends with 'i' or 'n'

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I want to count the number of 6-letter words that start with 'bar' and end with 'i' or 'n'.



For example, given this file:



barxxi
barxxc
barxxn
barqwq
barovo


the output should be 2 (integer), since these lines match:



barxxi
barxxn


My code is the following, but it seems to be wrong:



grep -c 'bar??[ni]' /path/file.txt






share|improve this question






















  • I'm learning Unix and I have to accomplish this assignment by only using -wc or -grep.
    – Ulysses
    Jan 31 at 13:41






  • 1




    You seem to be confusing shell globs (where ? means "any single character") with regular expression syntax (where it's a repetition modifier).
    – steeldriver
    Jan 31 at 13:42











  • @MarkPlotnick I was doing a bigger edit when yours came. I rolled yours back in the hope that the final version is clear enough. Feel free to change the post if you think it can be improved.
    – fedorqui
    Jan 31 at 13:47






  • 1




    @fedorqui No problem, your edits made it even better.
    – Mark Plotnick
    Jan 31 at 13:48











  • @steeldriver thanks
    – Ulysses
    Jan 31 at 13:54














up vote
0
down vote

favorite












I want to count the number of 6-letter words that start with 'bar' and end with 'i' or 'n'.



For example, given this file:



barxxi
barxxc
barxxn
barqwq
barovo


the output should be 2 (integer), since these lines match:



barxxi
barxxn


My code is the following, but it seems to be wrong:



grep -c 'bar??[ni]' /path/file.txt






share|improve this question






















  • I'm learning Unix and I have to accomplish this assignment by only using -wc or -grep.
    – Ulysses
    Jan 31 at 13:41






  • 1




    You seem to be confusing shell globs (where ? means "any single character") with regular expression syntax (where it's a repetition modifier).
    – steeldriver
    Jan 31 at 13:42











  • @MarkPlotnick I was doing a bigger edit when yours came. I rolled yours back in the hope that the final version is clear enough. Feel free to change the post if you think it can be improved.
    – fedorqui
    Jan 31 at 13:47






  • 1




    @fedorqui No problem, your edits made it even better.
    – Mark Plotnick
    Jan 31 at 13:48











  • @steeldriver thanks
    – Ulysses
    Jan 31 at 13:54












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I want to count the number of 6-letter words that start with 'bar' and end with 'i' or 'n'.



For example, given this file:



barxxi
barxxc
barxxn
barqwq
barovo


the output should be 2 (integer), since these lines match:



barxxi
barxxn


My code is the following, but it seems to be wrong:



grep -c 'bar??[ni]' /path/file.txt






share|improve this question














I want to count the number of 6-letter words that start with 'bar' and end with 'i' or 'n'.



For example, given this file:



barxxi
barxxc
barxxn
barqwq
barovo


the output should be 2 (integer), since these lines match:



barxxi
barxxn


My code is the following, but it seems to be wrong:



grep -c 'bar??[ni]' /path/file.txt








share|improve this question













share|improve this question




share|improve this question








edited Jan 31 at 13:46









fedorqui

3,87721853




3,87721853










asked Jan 31 at 13:37









Ulysses

496




496











  • I'm learning Unix and I have to accomplish this assignment by only using -wc or -grep.
    – Ulysses
    Jan 31 at 13:41






  • 1




    You seem to be confusing shell globs (where ? means "any single character") with regular expression syntax (where it's a repetition modifier).
    – steeldriver
    Jan 31 at 13:42











  • @MarkPlotnick I was doing a bigger edit when yours came. I rolled yours back in the hope that the final version is clear enough. Feel free to change the post if you think it can be improved.
    – fedorqui
    Jan 31 at 13:47






  • 1




    @fedorqui No problem, your edits made it even better.
    – Mark Plotnick
    Jan 31 at 13:48











  • @steeldriver thanks
    – Ulysses
    Jan 31 at 13:54
















  • I'm learning Unix and I have to accomplish this assignment by only using -wc or -grep.
    – Ulysses
    Jan 31 at 13:41






  • 1




    You seem to be confusing shell globs (where ? means "any single character") with regular expression syntax (where it's a repetition modifier).
    – steeldriver
    Jan 31 at 13:42











  • @MarkPlotnick I was doing a bigger edit when yours came. I rolled yours back in the hope that the final version is clear enough. Feel free to change the post if you think it can be improved.
    – fedorqui
    Jan 31 at 13:47






  • 1




    @fedorqui No problem, your edits made it even better.
    – Mark Plotnick
    Jan 31 at 13:48











  • @steeldriver thanks
    – Ulysses
    Jan 31 at 13:54















I'm learning Unix and I have to accomplish this assignment by only using -wc or -grep.
– Ulysses
Jan 31 at 13:41




I'm learning Unix and I have to accomplish this assignment by only using -wc or -grep.
– Ulysses
Jan 31 at 13:41




1




1




You seem to be confusing shell globs (where ? means "any single character") with regular expression syntax (where it's a repetition modifier).
– steeldriver
Jan 31 at 13:42





You seem to be confusing shell globs (where ? means "any single character") with regular expression syntax (where it's a repetition modifier).
– steeldriver
Jan 31 at 13:42













@MarkPlotnick I was doing a bigger edit when yours came. I rolled yours back in the hope that the final version is clear enough. Feel free to change the post if you think it can be improved.
– fedorqui
Jan 31 at 13:47




@MarkPlotnick I was doing a bigger edit when yours came. I rolled yours back in the hope that the final version is clear enough. Feel free to change the post if you think it can be improved.
– fedorqui
Jan 31 at 13:47




1




1




@fedorqui No problem, your edits made it even better.
– Mark Plotnick
Jan 31 at 13:48





@fedorqui No problem, your edits made it even better.
– Mark Plotnick
Jan 31 at 13:48













@steeldriver thanks
– Ulysses
Jan 31 at 13:54




@steeldriver thanks
– Ulysses
Jan 31 at 13:54










3 Answers
3






active

oldest

votes

















up vote
0
down vote



accepted










Use grep -o which outputs only the matched text, multiple time per line if necessary.



grep -o '<bar[^ ][^ ][ni]>' /path/file.txt | wc -w


The < and > match the beginning and end of a word.
The [^ ] matches a non-space character.






share|improve this answer




















  • yeah this is a good approach
    – Ulysses
    Jan 31 at 13:52

















up vote
0
down vote













Replace your question marks ? with periods . and your grep should work, as long as your input is only one word per line.



grep -c 'bar..[ni]' /path/file.txt





share|improve this answer




















  • sure and why . instead of ? I thought ? was for a single char
    – Ulysses
    Jan 31 at 13:53










  • The question mark character represents a single character in shell globbing syntax, not in regular-expression syntax.
    – user1404316
    Jan 31 at 14:09

















up vote
0
down vote













awk ' $i ~ /[[:space:]]bar.*[in]/) count++ END print count ' filename


The above awk solution should also work. Taking account of multiple entries on each line, awk looks from the first space delimited entry in the file on each line to the last entry (NF) if then pattern matches against regular expressions with ~ and increments a count accordingly. Two if statements are created to take account or a beginning of line (^) and bar as well a space and bar. At the end, the count is printed to screen.






share|improve this answer




















  • He has to accomplish this assignment by only using wc or grep, apparently
    – wurtel
    Jan 31 at 15:06










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%2f420952%2fuse-grep-to-count-number-of-6-letter-words-start-with-bar-and-ends-with-i-o%23new-answer', 'question_page');

);

Post as a guest






























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










Use grep -o which outputs only the matched text, multiple time per line if necessary.



grep -o '<bar[^ ][^ ][ni]>' /path/file.txt | wc -w


The < and > match the beginning and end of a word.
The [^ ] matches a non-space character.






share|improve this answer




















  • yeah this is a good approach
    – Ulysses
    Jan 31 at 13:52














up vote
0
down vote



accepted










Use grep -o which outputs only the matched text, multiple time per line if necessary.



grep -o '<bar[^ ][^ ][ni]>' /path/file.txt | wc -w


The < and > match the beginning and end of a word.
The [^ ] matches a non-space character.






share|improve this answer




















  • yeah this is a good approach
    – Ulysses
    Jan 31 at 13:52












up vote
0
down vote



accepted







up vote
0
down vote



accepted






Use grep -o which outputs only the matched text, multiple time per line if necessary.



grep -o '<bar[^ ][^ ][ni]>' /path/file.txt | wc -w


The < and > match the beginning and end of a word.
The [^ ] matches a non-space character.






share|improve this answer












Use grep -o which outputs only the matched text, multiple time per line if necessary.



grep -o '<bar[^ ][^ ][ni]>' /path/file.txt | wc -w


The < and > match the beginning and end of a word.
The [^ ] matches a non-space character.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 31 at 13:44









wurtel

9,18511024




9,18511024











  • yeah this is a good approach
    – Ulysses
    Jan 31 at 13:52
















  • yeah this is a good approach
    – Ulysses
    Jan 31 at 13:52















yeah this is a good approach
– Ulysses
Jan 31 at 13:52




yeah this is a good approach
– Ulysses
Jan 31 at 13:52












up vote
0
down vote













Replace your question marks ? with periods . and your grep should work, as long as your input is only one word per line.



grep -c 'bar..[ni]' /path/file.txt





share|improve this answer




















  • sure and why . instead of ? I thought ? was for a single char
    – Ulysses
    Jan 31 at 13:53










  • The question mark character represents a single character in shell globbing syntax, not in regular-expression syntax.
    – user1404316
    Jan 31 at 14:09














up vote
0
down vote













Replace your question marks ? with periods . and your grep should work, as long as your input is only one word per line.



grep -c 'bar..[ni]' /path/file.txt





share|improve this answer




















  • sure and why . instead of ? I thought ? was for a single char
    – Ulysses
    Jan 31 at 13:53










  • The question mark character represents a single character in shell globbing syntax, not in regular-expression syntax.
    – user1404316
    Jan 31 at 14:09












up vote
0
down vote










up vote
0
down vote









Replace your question marks ? with periods . and your grep should work, as long as your input is only one word per line.



grep -c 'bar..[ni]' /path/file.txt





share|improve this answer












Replace your question marks ? with periods . and your grep should work, as long as your input is only one word per line.



grep -c 'bar..[ni]' /path/file.txt






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 31 at 13:47









user1404316

2,314520




2,314520











  • sure and why . instead of ? I thought ? was for a single char
    – Ulysses
    Jan 31 at 13:53










  • The question mark character represents a single character in shell globbing syntax, not in regular-expression syntax.
    – user1404316
    Jan 31 at 14:09
















  • sure and why . instead of ? I thought ? was for a single char
    – Ulysses
    Jan 31 at 13:53










  • The question mark character represents a single character in shell globbing syntax, not in regular-expression syntax.
    – user1404316
    Jan 31 at 14:09















sure and why . instead of ? I thought ? was for a single char
– Ulysses
Jan 31 at 13:53




sure and why . instead of ? I thought ? was for a single char
– Ulysses
Jan 31 at 13:53












The question mark character represents a single character in shell globbing syntax, not in regular-expression syntax.
– user1404316
Jan 31 at 14:09




The question mark character represents a single character in shell globbing syntax, not in regular-expression syntax.
– user1404316
Jan 31 at 14:09










up vote
0
down vote













awk ' $i ~ /[[:space:]]bar.*[in]/) count++ END print count ' filename


The above awk solution should also work. Taking account of multiple entries on each line, awk looks from the first space delimited entry in the file on each line to the last entry (NF) if then pattern matches against regular expressions with ~ and increments a count accordingly. Two if statements are created to take account or a beginning of line (^) and bar as well a space and bar. At the end, the count is printed to screen.






share|improve this answer




















  • He has to accomplish this assignment by only using wc or grep, apparently
    – wurtel
    Jan 31 at 15:06














up vote
0
down vote













awk ' $i ~ /[[:space:]]bar.*[in]/) count++ END print count ' filename


The above awk solution should also work. Taking account of multiple entries on each line, awk looks from the first space delimited entry in the file on each line to the last entry (NF) if then pattern matches against regular expressions with ~ and increments a count accordingly. Two if statements are created to take account or a beginning of line (^) and bar as well a space and bar. At the end, the count is printed to screen.






share|improve this answer




















  • He has to accomplish this assignment by only using wc or grep, apparently
    – wurtel
    Jan 31 at 15:06












up vote
0
down vote










up vote
0
down vote









awk ' $i ~ /[[:space:]]bar.*[in]/) count++ END print count ' filename


The above awk solution should also work. Taking account of multiple entries on each line, awk looks from the first space delimited entry in the file on each line to the last entry (NF) if then pattern matches against regular expressions with ~ and increments a count accordingly. Two if statements are created to take account or a beginning of line (^) and bar as well a space and bar. At the end, the count is printed to screen.






share|improve this answer












awk ' $i ~ /[[:space:]]bar.*[in]/) count++ END print count ' filename


The above awk solution should also work. Taking account of multiple entries on each line, awk looks from the first space delimited entry in the file on each line to the last entry (NF) if then pattern matches against regular expressions with ~ and increments a count accordingly. Two if statements are created to take account or a beginning of line (^) and bar as well a space and bar. At the end, the count is printed to screen.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 31 at 13:59









Raman Sailopal

1,18117




1,18117











  • He has to accomplish this assignment by only using wc or grep, apparently
    – wurtel
    Jan 31 at 15:06
















  • He has to accomplish this assignment by only using wc or grep, apparently
    – wurtel
    Jan 31 at 15:06















He has to accomplish this assignment by only using wc or grep, apparently
– wurtel
Jan 31 at 15:06




He has to accomplish this assignment by only using wc or grep, apparently
– wurtel
Jan 31 at 15:06












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f420952%2fuse-grep-to-count-number-of-6-letter-words-start-with-bar-and-ends-with-i-o%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?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay