-
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
-
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
==> [url]http://www.frappr.com/cusm[/url] <==
-
Re: searching csv file
On Sat, 15 Sep 2007, willjay wrote:
[color=blue]
> 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.[/color]
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: [email]ras@anzio.com[/email]
company e-mail: [email]rsi@anzio.com[/email]
voice: (US) 503-624-0360 (9:00-6:00 Pacific Time)
fax: (US) 503-624-0760
web: [url]http://www.anzio.com[/url]
-
Re: searching csv file
willjay wrote:[color=blue]
> 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
>
>[/color]
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
-
Re: searching csv file
On Sep 16, 3:09 am, "Steve M. Fabac, Jr." <smfa...@att.net> wrote:[color=blue]
> willjay wrote:[color=green]
> > I have a large csv file that I need to break apart the file, looks
> > like this.[/color]
>[color=green]
> > How do you search for a specific pattern with in a specific field?[/color]
>[color=green]
> > "text1","text2","text3","text4"[/color]
>[color=green]
> > i can do:[/color]
>[color=green]
> > grep ',"text3",' foo > newfoo[/color]
>[color=green]
> > however i need to make sure that the 3rd field is always searched not
> > just an occurrance anywhere within the file.[/color]
>[color=green]
> > help appreciated.[/color]
>[color=green]
> > wj[/color]
>
> 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 -[/color]
awk balks with
awk: input record `"1","TEXT DATA...' too long
source line 1 of program << $24 ~ /"GA"/ { pri ... >>
wj
-
Re: searching csv file
willjay wrote:[color=blue]
> On Sep 16, 3:09 am, "Steve M. Fabac, Jr." <smfa...@att.net> wrote:[color=green]
>> willjay wrote:[color=darkred]
>>> 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[/color]
>> 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 -[/color]
>
> awk balks with
>
> awk: input record `"1","TEXT DATA...' too long
> source line 1 of program << $24 ~ /"GA"/ { pri ... >>
>
> wj
>
>[/color]
Try gawk:
[color=blue]
> 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.[/color]
/usr/gnu/bin/gawk -> /opt/K/SCO/gnutools/5.0.7Kj/usr/gnu/bin/gawk
--
Steve Fabac
S.M. Fabac & Associates
816/765-1670