[X-Unix] parsing help?

Wing Wong wingedpower at gmail.com
Thu Apr 27 12:47:51 PDT 2006


On 4/27/06, Russell McGaha <RussellMcGaha at hughes.net> wrote:
> All;
>         I guess what I REALY need to do, is change the pattern
> "<return>,,, ... ," to "," . Is there a simple way to do this?
> because of the <return> char, I'm not sure how to correctly do this.
>
> Russell

Russell,

Can you post an actual couple of lines of the actual data and then a
couple of lines of what you would like that data to actually look
like? The various commas and dots are confusing to read and interpret
accurately.

>From what you just posted, I'm hearing:

You have a series of lines in the exported csv file that takes the form of:

<CR/LF>,,,,,,,,,,,
<CR/LF>,,,,,,,,,,,
<CR/LF>,,,,,,,,,,,

And you want to reduce the commas to a single comma?

# To reduce a string of commas into one comma:
cat source.csv | sed -e 's/[,][,]*/,/g' > single_comma.csv

# To get rid of the CR/LF and reduce the string of commas to one comma:
cat source.csv | sed -e 's/[\n][,][,]*/,/g' > no_crlf_single_comma.csv

Not sure if that is what you really want to do. If you are looking to
change the order of the various parameters, you can use awk to do
that.

IF, your file is actually looking something like this:

,,,,,,,,,^M
,,,,,,,,,^M
,,,,,,,,,^M
,,,,,,,,,^M

Ie, a an exported csv file using DOS/WIN cr/lf conventions, and you
just want to get rid of the ^M's, but want to retain the line breaks:

cat source.csv | tr '^V^M' '\012' > convertedlinefeeds.csv
# The ^V is CTRL-V and the ^M is hitting the ENTER key after the CTRL-V

or, you can use the dos2unix binary to convert the linefeeds for you.

But like I noted above, if you can send a sample of what the actual
lines look like and a sample of what you'd like the lines to look
like, it would be much easier to give example code snippets.

Wing

--
Wing Wong
wingedpower at gmail.com


More information about the X-Unix mailing list