deleting csv columns

Find, replace, find in files, replace in files, regular expressions

deleting csv columns

Postby woofy1 » Wed Jan 24, 2007 11:49 pm

I've read some other column posts and met my basic needs. But I'd like to know if I can do it faster.

I want to:
1. open a csv file
2. delete the first 3 columns
3. keep column 4
4. delete the columns after column 4

I've accomplished this by converting to fixed width columns, then doing two deletes. Do I have to convert to fixed width? Can't I delete variable length columns?

Thanks
User avatar
woofy1
Newbie
 
Posts: 1
Joined: Wed Jan 24, 2007 12:00 am

Re: deleting csv columns

Postby Bego » Thu Jan 25, 2007 8:15 am

The following Unix style regular expression does the job:

replace:
^.*\,.*\,.*\,(.*),.*$
with:
\1

or - even better - if you prefer the Perl-regexp-style:
replace:
^.*?\,.*?\,.*?\,(.*?),.*
with:
$1

^ means: Start of line (might be omitted here)
? means: non-greedy, just up to the NEXT Komma.
() means: my first variable = $1
.* means: all characters


Code: Select all
next, columns, stays,BECAUSEITS4, IM, gonna, be, deleted, too

Code: Select all
BECAUSEITS4


rds Bego
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Re: deleting csv columns

Postby Bego » Fri Aug 01, 2008 3:39 am

I found a smarter, more readable(?) approach. Here is it in a macro, you can "extract" the find/rep from there:
The macro is called "delCol7":

Code: Select all
InsertMode
ColumnModeOff
HexOff
PerlReOn
Find RegExp "^(([^;]*;){6})[^;]*;"
Replace All "$1"
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany


Return to Find/Replace/Regular Expressions

cron