I want to filter all lines from a file that contain mySearchString
and after that group them together and count them.
Example find all lines that contain 9791
AB-9791___Foo
AB-9791___Foo
DE-9791___Bar
AB-0001___Foo
Using $ grep "9791" myFile.txt
gives this result
AB-9791___Foo
AB-9791___Foo
DE-9791___Bar
// 0001 was filtered out
This result should be grouped and counted (like SQL Group by Count
) like this
AB-9791___Foo 2
DE-9791___BAR 1
This answer uses perl but perl is not installed on our machines.
What tool is usefull ( grep, awk, sed, or other) to achieve the second part to group and count?
Update with test records
In my test file Test_2.txt
these lines are written
AB-9791___Foo
DE-9791___Bar
AB-0001___Foo
AB-9791___Foo
AB-9791___Foo
AB-9791___Foo
DE-9791___Bar
DE-9791___Bar
DE-9791___Bar
I copy und pasted each AB-9791___Foo
line so they should be identical.
Running $ grep '9791' Test_grep_uniq_sort.txt | uniq -c
gave this result
1 AB-9791___Foo
1 DE-9791___Bar // expected: 4 actual: 1, 2, 1
3 AB-9791___Foo // expected: 4 actual: 1, 3
2 DE-9791___Bar
1 DE-9791___Bar
Running $ sort Test_2.txt > Test_2_sort_0.txt
and then using grep | uniq
on Test_2_sort_0.txt
did almost return the expected output.
$ grep '9791' Test_2_sort_0.txt | uniq -c
4 AB-9791___Foo
1 DE-9791___Bar // this is due to a missing line break / line feed
3 DE-9791___Bar
After adding a line break / line feed by hand everything did work