Use -grep to count number of 6-letter words start with 'bar' and ends with 'i' or 'n'
Clash 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
grep wc
add a comment |Â
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
grep wc
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
add a comment |Â
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
grep wc
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
grep wc
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
add a comment |Â
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
add a comment |Â
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.
yeah this is a good approach
â Ulysses
Jan 31 at 13:52
add a comment |Â
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
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
add a comment |Â
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.
He has to accomplish this assignment by only using wc or grep, apparently
â wurtel
Jan 31 at 15:06
add a comment |Â
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.
yeah this is a good approach
â Ulysses
Jan 31 at 13:52
add a comment |Â
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.
yeah this is a good approach
â Ulysses
Jan 31 at 13:52
add a comment |Â
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.
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.
answered Jan 31 at 13:44
wurtel
9,18511024
9,18511024
yeah this is a good approach
â Ulysses
Jan 31 at 13:52
add a comment |Â
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
add a comment |Â
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
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
add a comment |Â
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
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
add a comment |Â
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
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
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
add a comment |Â
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
add a comment |Â
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.
He has to accomplish this assignment by only using wc or grep, apparently
â wurtel
Jan 31 at 15:06
add a comment |Â
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.
He has to accomplish this assignment by only using wc or grep, apparently
â wurtel
Jan 31 at 15:06
add a comment |Â
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.
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.
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
add a comment |Â
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
add a comment |Â
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%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
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
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