user52470 user52470 - 5 years ago 289
Bash Question

Writing a Bash script pulling every word that is separated by a comma out of a text file

I'm trying to write a Bash script that will read a text file and pull out every word that is followed by a comma on a new line. I tried using

but it prints the whole line that has a comma in it, and I have had the same trouble with
. I have checked the manage for grep, but it seems to be more focused on flags than syntax. This is a normal .txt file, not a CSV there are just commas as appropriate for normal english grammar. Can anyone show me how to set up this script?

For example if the text file contained a list of animals, like so:

The Veterinary clinic treats the following animals: dogs, cats, and birds

the script would display:


Answer Source

Looks like you need to know about grep's -o parameter (only matching). If you consider a word to be a series of letters separated by spaces than this match will do:

grep -o "[a-zA-Z]\+," file
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download