searching csv file - SCO

This is a discussion on searching csv file - SCO ; I have a large csv file that I need to break apart the file, looks like this. How do you search for a specific pattern with in a specific field? "text1","text2","text3","text4" i can do: grep ',"text3",' foo > newfoo however ...

+ Reply to Thread
Results 1 to 6 of 6

Thread: searching csv file

  1. searching csv file

    I have a large csv file that I need to break apart the file, looks
    like this.

    How do you search for a specific pattern with in a specific field?

    "text1","text2","text3","text4"

    i can do:

    grep ',"text3",' foo > newfoo

    however i need to make sure that the 3rd field is always searched not
    just an occurrance anywhere within the file.

    help appreciated.

    wj


  2. Re: searching csv file

    willjay typed (on Sat, Sep 15, 2007 at 03:32:06PM -0700):
    | I have a large csv file that I need to break apart the file, looks
    | like this.
    |
    | How do you search for a specific pattern with in a specific field?
    |
    | "text1","text2","text3","text4"
    |
    | i can do:
    |
    | grep ',"text3",' foo > newfoo
    |
    | however i need to make sure that the 3rd field is always searched not
    | just an occurrance anywhere within the file.
    |
    | help appreciated.

    grep '.*,.*,.*pattern_to_find.*,' file

    Haven't tried it, but seems logical as I type it...

    --
    JP
    ==> http://www.frappr.com/cusm <==

  3. Re: searching csv file

    On Sat, 15 Sep 2007, willjay wrote:

    > I have a large csv file that I need to break apart the file, looks
    > like this.
    >
    > How do you search for a specific pattern with in a specific field?
    >
    > "text1","text2","text3","text4"
    >
    > i can do:
    >
    > grep ',"text3",' foo > newfoo
    >
    > however i need to make sure that the 3rd field is always searched not
    > just an occurrance anywhere within the file.


    I believe you can use the "cut" command to pull out the third field. But
    if you want to output the whole line, I don't think it would work.

    Regards,
    .....Bob Rasmussen, President, Rasmussen Software, Inc.

    personal e-mail: ras@anzio.com
    company e-mail: rsi@anzio.com
    voice: (US) 503-624-0360 (9:00-6:00 Pacific Time)
    fax: (US) 503-624-0760
    web: http://www.anzio.com

  4. Re: searching csv file

    willjay wrote:
    > I have a large csv file that I need to break apart the file, looks
    > like this.
    >
    > How do you search for a specific pattern with in a specific field?
    >
    > "text1","text2","text3","text4"
    >
    > i can do:
    >
    > grep ',"text3",' foo > newfoo
    >
    > however i need to make sure that the 3rd field is always searched not
    > just an occurrance anywhere within the file.
    >
    > help appreciated.
    >
    > wj
    >
    >


    Looks to me like a job for AWK and not grep.

    awk -F "," ' $3 ~ /pattern you want/ { print $0 } ' foo > newfoo

    The above will print the entire record where field 3 contains "pattern you want."
    If you only want field 3, then change "print $0" to "print $3."


    One problem is the ","text with, in it","

    Its better to get rid of "," and change it to just | with a sed
    command:

    sed 's/","/|/g' foo | \
    awk -F "|" ' $3 ~ /pattern you want/ { print $0 } ' | \
    sed 's/|/","/g' > newfoo


    --
    Steve Fabac
    S.M. Fabac & Associates
    816/765-1670

  5. Re: searching csv file

    On Sep 16, 3:09 am, "Steve M. Fabac, Jr." wrote:
    > willjay wrote:
    > > I have a large csv file that I need to break apart the file, looks
    > > like this.

    >
    > > How do you search for a specific pattern with in a specific field?

    >
    > > "text1","text2","text3","text4"

    >
    > > i can do:

    >
    > > grep ',"text3",' foo > newfoo

    >
    > > however i need to make sure that the 3rd field is always searched not
    > > just an occurrance anywhere within the file.

    >
    > > help appreciated.

    >
    > > wj

    >
    > Looks to me like a job for AWK and not grep.
    >
    > awk -F "," ' $3 ~ /pattern you want/ { print $0 } ' foo > newfoo
    >
    > The above will print the entire record where field 3 contains "pattern you want."
    > If you only want field 3, then change "print $0" to "print $3."
    >
    > One problem is the ","text with, in it","
    >
    > Its better to get rid of "," and change it to just | with a sed
    > command:
    >
    > sed 's/","/|/g' foo | \
    > awk -F "|" ' $3 ~ /pattern you want/ { print $0 } ' | \
    > sed 's/|/","/g' > newfoo
    >
    > --
    > Steve Fabac
    > S.M. Fabac & Associates
    > 816/765-1670- Hide quoted text -
    >
    > - Show quoted text -


    awk balks with

    awk: input record `"1","TEXT DATA...' too long
    source line 1 of program << $24 ~ /"GA"/ { pri ... >>

    wj


  6. Re: searching csv file

    willjay wrote:
    > On Sep 16, 3:09 am, "Steve M. Fabac, Jr." wrote:
    >> willjay wrote:
    >>> I have a large csv file that I need to break apart the file, looks
    >>> like this.
    >>> How do you search for a specific pattern with in a specific field?
    >>> "text1","text2","text3","text4"
    >>> i can do:
    >>> grep ',"text3",' foo > newfoo
    >>> however i need to make sure that the 3rd field is always searched not
    >>> just an occurrance anywhere within the file.
    >>> help appreciated.
    >>> wj

    >> Looks to me like a job for AWK and not grep.
    >>
    >> awk -F "," ' $3 ~ /pattern you want/ { print $0 } ' foo > newfoo
    >>
    >> The above will print the entire record where field 3 contains "pattern you want."
    >> If you only want field 3, then change "print $0" to "print $3."
    >>
    >> One problem is the ","text with, in it","
    >>
    >> Its better to get rid of "," and change it to just | with a sed
    >> command:
    >>
    >> sed 's/","/|/g' foo | \
    >> awk -F "|" ' $3 ~ /pattern you want/ { print $0 } ' | \
    >> sed 's/|/","/g' > newfoo
    >>
    >> --
    >> Steve Fabac
    >> S.M. Fabac & Associates
    >> 816/765-1670- Hide quoted text -
    >>
    >> - Show quoted text -

    >
    > awk balks with
    >
    > awk: input record `"1","TEXT DATA...' too long
    > source line 1 of program << $24 ~ /"GA"/ { pri ... >>
    >
    > wj
    >
    >


    Try gawk:

    > Many versions of awk have various implementation limits, on things such as:
    >
    > Number of fields per record
    >
    > Number of characters per input record
    >
    > Number of characters per output record
    >
    > Number of characters per field
    >
    > Number of characters per printf string
    >
    > Number of characters in literal string
    >
    > Number of characters in character class
    >
    > Number of files open
    >
    > Number of pipes open
    >
    > The ability to handle 8-bit characters and characters
    > that are all zero (ASCII NUL)
    >
    > gawk does not have limits on any of these items, other
    > than those imposed by the machine architecture and/or
    > the operating system.





    /usr/gnu/bin/gawk -> /opt/K/SCO/gnutools/5.0.7Kj/usr/gnu/bin/gawk


    --
    Steve Fabac
    S.M. Fabac & Associates
    816/765-1670

+ Reply to Thread