Re: Parsing a text file with mutuple seperator
[email protected] ("John W. Krahn") Tue, 08 Apr 2008 22:07:50 -0700
| Newsgroups | perl.scripts |
|---|---|
| Message-ID | <[email protected]> |
[email protected] wrote: > Hi all, Hello, > I am writing a perl script to parse a file. The data in the file is > seperated by space/tab. However, certain fields may be empty or > consist of mutiple words and are double quoted and this makes it > difficut for me to do a split. > > Example of data: > "" "This is 2nd field" > 3 4 > 1 2 > "" 4 > 1 2 "The field may consist of (meta) > characters" "" $ echo '"" "This is 2nd field" 3 4 1 2 "" 4 1 2 "The field may consist of (meta) characters" ""' | \ perl -lne' my @x = /"[^"]*"|\S+/g; print "Number of fields: " . @x . " ", map " >$_<", @x; ' Number of fields: 4 >""< >"This is 2nd field"< >3< >4< Number of fields: 4 >1< >2< >""< >4< Number of fields: 4 >1< >2< >"The field may consist of (meta) characters"< >""< John -- Perl isn't a toolbox, but a small machine shop where you can special-order certain sorts of tools at low cost and in short order. -- Larry Wall