Re: One Liner to double quote every field in a delimited file
by Tux (Canon) on Jan 31, 2014 at 21:43 UTC
|
I am still testing, but the next release of Text::CSV_XS will offer that as
$ perl -MText::CSV_XS=csv -we'csv (in => csv (file => "file.csv"), out
+ => *STDOUT, always_quote => 1)'
I expect to release somewhere next week.
A longer one-liner that works with Text::CSV_XS right now is:
$ perl -MText::CSV_XS -we'$c=Text::CSV_XS->new;$a=$c->getline_all(\*AR
+GV);$c->eol($/);$c->always_quote(1);$c->print(\*STDOUT,$_)for@$a' fil
+e.csv
Enjoy, Have FUN! H.Merijn
| [reply] [d/l] [select] |
Re: One Liner to double quote every field in a delimited file
by davido (Cardinal) on Jan 31, 2014 at 22:01 UTC
|
perl -MText::CSV -lpe 'BEGIN{$c=Text::CSV->new({always_quote=>1})} $c-
+>parse($_); $c->combine($c->fields); $_=$c->string' textfile.csv
...or a little more brief...
perl -MText::CSV -lne 'BEGIN{$c=Text::CSV->new({always_quote=>1})} $c-
+>parse($_); $c->print(\*STDOUT,[$c->fields])' textfile.csv
Just substitute "Text::CSV_XS" for Text::CSV if you prefer the XS version, though it's probably not going to make much difference in one-liner world. Watch out for newlines embedded in the input CSV. ;)
An even better one liner would be to convert whichever version like to an actual script, save it in your path somewhere, and then invoke it as:
cat infile.csv | quotecsv > newfile.csv
| [reply] [d/l] [select] |
Re: One Liner to double quote every field in a delimited fle (Updated!)
by BrowserUk (Patriarch) on Jan 31, 2014 at 21:24 UTC
|
perl -pe"s[,][\",\"]g;s[^][\"];s[$][\"];" infile > outfile
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] |
|
|
$ perl ./browseruk.pl
"Joe","43,anytown,anystate,zipcode"
You forgot a "g" modifier.
s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
+.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
| [reply] [d/l] [select] |
|
|
| [reply] |
|
|
| [reply] [d/l] [select] |
|
|
| [reply] |
|
|
Awesome!! Thanks! I wasn't sure how to combine multiple statements into one command..
| [reply] |
|
|
sub one_command {
... multiple statements here ...
}
| [reply] [d/l] |
Re: One Liner to double quote every field in a delimited file (perl -lanse)
by Anonymous Monk on Jan 31, 2014 at 23:08 UTC
|
$ perl -F, -lanse " print join q{,}, map { qq{\x22$_\x22} } @F "
ro,sham,bo
"ro","sham","bo"
| [reply] [d/l] |
Re: One Liner to double quote every field in a delimited file
by Skeeve (Parson) on Feb 01, 2014 at 15:38 UTC
|
Joe,43,anytown,anystate,zipcode,71",40°26'47"N 079°58'36"W
s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
+.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
| [reply] [d/l] [select] |
|
|
| [reply] [d/l] [select] |
|
|
chomp($_=<DATA>);
$_= join(',', map { s/"/""/g; '"' . $_ . '"' } split /,/) . "\n";
s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
+.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
| [reply] [d/l] [select] |