0

I have a file called "polls.csv" that I have to use the cat command to return a certain part of. Each line of the file looks like this: Utah,5,27,64,4,Sep 13,,,,,,,5,American Res. Group

Here is an example of the cat output: https://i.stack.imgur.com/C4WRI.png

How could I run a cat command and only return the polling organization, "American Res. Group" in the example line, for each line. I'm assuming it would look similar to: cat polls.csv /^[]$/

Also how could I have it count a total of the amount of different polling organizations?

John Avena
  • 3
  • 1
  • 1
  • 3

2 Answers2

1

I don't think cat command will do the job alone. I guess you should have a look at gnu core utils package where you will find a lot of tools: http://www.gnu.org/software/coreutils/manual/html_node/index.html

One simple way of doing it is:

cat polls.csv |sed -s "s/.*,//"

sed -s "s/.*,//" replaces everything before last comma with nothing, hence leaving polling organisation.

For your second question, you can continue to use sort -u which will sort everthing uniquely and then do wc -l to count, for example:

cat polls.csv|sed -s "s/.*,//"|sort -u|wc -l

For consonant check, it is a separate problem definition. There are many ways to check it. Here's my way on top of my head:

# /bin/python
import sys
import re

consonants = "bcdfghjklmnpqrstvwxyz"

def count_consonants(word):
    count = 0
    for x in word:
        if x in consonants:
            count += 1
    return count

while True:
    line = sys.stdin.readline()
    if line:
        line = line.rstrip()
        line = re.sub(" +", " ", line)
        words = line.split(" ")
        for w in words:
            c = count_consonants(w)
            if c >= 6:
                print w
    else:
        break

Please save it as script.py and Then do it:

cat polls.csv|sed -s "s/.*,//"|sort -u|python script.py

or

cat words|python script.py
chfw
  • 4,502
  • 2
  • 29
  • 32
  • Could you help me with something else? I have a file named words that is a list of a bunch of different words. How could I output all of the words that have six (or more) consonants (non-vowels) in a row. – John Avena Sep 07 '14 at 22:59
0

Oh, I misunderstood your requirement. try this:

awk -F, '{print $NF}' polls.csv
Kent
  • 189,393
  • 32
  • 233
  • 301