How to print the count of pattern at each line?
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
I need to print the count of a matching string at the end of each line.
An example for matching foo
:
foo,bar,foo,foo
bar,foo,bar,bar
foo,foo,bar,bar
Result :
foo,bar,foo,foo,3
bar,foo,bar,bar,1
foo,foo,bar,bar,2
I have checked this link(How to count the number of a specific character in each line?) but no luck.
linux shell-script awk csv
add a comment |Â
up vote
6
down vote
favorite
I need to print the count of a matching string at the end of each line.
An example for matching foo
:
foo,bar,foo,foo
bar,foo,bar,bar
foo,foo,bar,bar
Result :
foo,bar,foo,foo,3
bar,foo,bar,bar,1
foo,foo,bar,bar,2
I have checked this link(How to count the number of a specific character in each line?) but no luck.
linux shell-script awk csv
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
I need to print the count of a matching string at the end of each line.
An example for matching foo
:
foo,bar,foo,foo
bar,foo,bar,bar
foo,foo,bar,bar
Result :
foo,bar,foo,foo,3
bar,foo,bar,bar,1
foo,foo,bar,bar,2
I have checked this link(How to count the number of a specific character in each line?) but no luck.
linux shell-script awk csv
I need to print the count of a matching string at the end of each line.
An example for matching foo
:
foo,bar,foo,foo
bar,foo,bar,bar
foo,foo,bar,bar
Result :
foo,bar,foo,foo,3
bar,foo,bar,bar,1
foo,foo,bar,bar,2
I have checked this link(How to count the number of a specific character in each line?) but no luck.
linux shell-script awk csv
edited Aug 25 at 21:32
Jeff Schaller
32k849109
32k849109
asked Aug 25 at 17:44
dgear01
334
334
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
11
down vote
accepted
We can use awk with gsub to get the count of occurrence.
awk 'print $0","gsub(/foo/,"")' file
Output:
foo,bar,foo,foo,3
bar,foo,bar,bar,1
foo,foo,bar,bar,2
1
Worked like charm!!! thanks.
â dgear01
Aug 25 at 17:53
add a comment |Â
up vote
1
down vote
Perhaps with a mixture of bash
and grep
$ while read -r line; do
echo -n "$line -> "
grep -o foo <<<"$line" | wc -l
done < /path/to/my-input-file
foo,bar,foo,foo -> 3
bar,foo,bar,bar -> 1
foo,foo,bar,bar -> 2
add a comment |Â
up vote
0
down vote
In case anyone also wanted something in Python. Specify the filename and the magic word to count over.
#!/usr/bin/python3
# magic_word_count.py
# Takes a filename and magic word and prints the number of times the word
# appears on each line of the file.
#
# ./magic_word_count.py myfile.txt foo
#
import sys
filename = sys.argv[1]
magic_word = sys.argv[2]
with open(filename, 'r') as f:
for line in f.readlines():
words = line.strip().split(',')
print(len([word for word in words if word == magic_word]))
Usage:
$ cat myfile.txt
foo,bar,foo,foo
bar,foo,bar,bar
foo,foo,bar,bar
$ ./magic_word_count.py myfile.txt foo
3
1
2
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
11
down vote
accepted
We can use awk with gsub to get the count of occurrence.
awk 'print $0","gsub(/foo/,"")' file
Output:
foo,bar,foo,foo,3
bar,foo,bar,bar,1
foo,foo,bar,bar,2
1
Worked like charm!!! thanks.
â dgear01
Aug 25 at 17:53
add a comment |Â
up vote
11
down vote
accepted
We can use awk with gsub to get the count of occurrence.
awk 'print $0","gsub(/foo/,"")' file
Output:
foo,bar,foo,foo,3
bar,foo,bar,bar,1
foo,foo,bar,bar,2
1
Worked like charm!!! thanks.
â dgear01
Aug 25 at 17:53
add a comment |Â
up vote
11
down vote
accepted
up vote
11
down vote
accepted
We can use awk with gsub to get the count of occurrence.
awk 'print $0","gsub(/foo/,"")' file
Output:
foo,bar,foo,foo,3
bar,foo,bar,bar,1
foo,foo,bar,bar,2
We can use awk with gsub to get the count of occurrence.
awk 'print $0","gsub(/foo/,"")' file
Output:
foo,bar,foo,foo,3
bar,foo,bar,bar,1
foo,foo,bar,bar,2
answered Aug 25 at 17:47
SivaPrasath
1
1
1
Worked like charm!!! thanks.
â dgear01
Aug 25 at 17:53
add a comment |Â
1
Worked like charm!!! thanks.
â dgear01
Aug 25 at 17:53
1
1
Worked like charm!!! thanks.
â dgear01
Aug 25 at 17:53
Worked like charm!!! thanks.
â dgear01
Aug 25 at 17:53
add a comment |Â
up vote
1
down vote
Perhaps with a mixture of bash
and grep
$ while read -r line; do
echo -n "$line -> "
grep -o foo <<<"$line" | wc -l
done < /path/to/my-input-file
foo,bar,foo,foo -> 3
bar,foo,bar,bar -> 1
foo,foo,bar,bar -> 2
add a comment |Â
up vote
1
down vote
Perhaps with a mixture of bash
and grep
$ while read -r line; do
echo -n "$line -> "
grep -o foo <<<"$line" | wc -l
done < /path/to/my-input-file
foo,bar,foo,foo -> 3
bar,foo,bar,bar -> 1
foo,foo,bar,bar -> 2
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Perhaps with a mixture of bash
and grep
$ while read -r line; do
echo -n "$line -> "
grep -o foo <<<"$line" | wc -l
done < /path/to/my-input-file
foo,bar,foo,foo -> 3
bar,foo,bar,bar -> 1
foo,foo,bar,bar -> 2
Perhaps with a mixture of bash
and grep
$ while read -r line; do
echo -n "$line -> "
grep -o foo <<<"$line" | wc -l
done < /path/to/my-input-file
foo,bar,foo,foo -> 3
bar,foo,bar,bar -> 1
foo,foo,bar,bar -> 2
answered Aug 25 at 20:56
shalomb
1585
1585
add a comment |Â
add a comment |Â
up vote
0
down vote
In case anyone also wanted something in Python. Specify the filename and the magic word to count over.
#!/usr/bin/python3
# magic_word_count.py
# Takes a filename and magic word and prints the number of times the word
# appears on each line of the file.
#
# ./magic_word_count.py myfile.txt foo
#
import sys
filename = sys.argv[1]
magic_word = sys.argv[2]
with open(filename, 'r') as f:
for line in f.readlines():
words = line.strip().split(',')
print(len([word for word in words if word == magic_word]))
Usage:
$ cat myfile.txt
foo,bar,foo,foo
bar,foo,bar,bar
foo,foo,bar,bar
$ ./magic_word_count.py myfile.txt foo
3
1
2
add a comment |Â
up vote
0
down vote
In case anyone also wanted something in Python. Specify the filename and the magic word to count over.
#!/usr/bin/python3
# magic_word_count.py
# Takes a filename and magic word and prints the number of times the word
# appears on each line of the file.
#
# ./magic_word_count.py myfile.txt foo
#
import sys
filename = sys.argv[1]
magic_word = sys.argv[2]
with open(filename, 'r') as f:
for line in f.readlines():
words = line.strip().split(',')
print(len([word for word in words if word == magic_word]))
Usage:
$ cat myfile.txt
foo,bar,foo,foo
bar,foo,bar,bar
foo,foo,bar,bar
$ ./magic_word_count.py myfile.txt foo
3
1
2
add a comment |Â
up vote
0
down vote
up vote
0
down vote
In case anyone also wanted something in Python. Specify the filename and the magic word to count over.
#!/usr/bin/python3
# magic_word_count.py
# Takes a filename and magic word and prints the number of times the word
# appears on each line of the file.
#
# ./magic_word_count.py myfile.txt foo
#
import sys
filename = sys.argv[1]
magic_word = sys.argv[2]
with open(filename, 'r') as f:
for line in f.readlines():
words = line.strip().split(',')
print(len([word for word in words if word == magic_word]))
Usage:
$ cat myfile.txt
foo,bar,foo,foo
bar,foo,bar,bar
foo,foo,bar,bar
$ ./magic_word_count.py myfile.txt foo
3
1
2
In case anyone also wanted something in Python. Specify the filename and the magic word to count over.
#!/usr/bin/python3
# magic_word_count.py
# Takes a filename and magic word and prints the number of times the word
# appears on each line of the file.
#
# ./magic_word_count.py myfile.txt foo
#
import sys
filename = sys.argv[1]
magic_word = sys.argv[2]
with open(filename, 'r') as f:
for line in f.readlines():
words = line.strip().split(',')
print(len([word for word in words if word == magic_word]))
Usage:
$ cat myfile.txt
foo,bar,foo,foo
bar,foo,bar,bar
foo,foo,bar,bar
$ ./magic_word_count.py myfile.txt foo
3
1
2
answered Aug 25 at 23:57
user1717828
1,59611125
1,59611125
add a comment |Â
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%2f464818%2fhow-to-print-the-count-of-pattern-at-each-line%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