split lines according to condition

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, MudGuard

Post Reply
mixtape
Posts: 9
Joined: Fri Mar 04, 2011 9:42 am

split lines according to condition

Post by mixtape »

Hi,

I want to split a line after a certain number of ["].

How to i do this?

Thanks in advance!

//A.
ben_josephs
Posts: 2464
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

Do you want to insert a newline into each line after its nth double quote symbol, for some n?

If so, and if, say, n = 6, try:

Use "Posix" regular expression syntax:
Configure | Preferences | Editor

[X] Use POSIX regular expression syntax
Search | Replace... (<F8>):
Find what: ^([^"]*"){6}
Replace with: \0\n

[X] Regular expression

Replace All
mixtape
Posts: 9
Joined: Fri Mar 04, 2011 9:42 am

Post by mixtape »

Thanks!

But i might have done this the wrong way... see i have some rows that are separated with [enter] when they're not suppose to.

So can i do a search and replace looking for n number of symbols and then replace the [enter] with nothing?

Thanks again!
ben_josephs
Posts: 2464
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

Do you mean that some newlines are added that you don't want?

Please explain precisely, with examples, where my suggestion works as you want it to and where it doesn't.
mixtape
Posts: 9
Joined: Fri Mar 04, 2011 9:42 am

Post by mixtape »

Sorry about that. The problem was, since i had rows where there were not suppose to be an [enter] i thought that maybe if i remove all the [enters] including the onces at the end of every row where they're suppose to be and then separate that long one line every number of symbols that i desired.

Problem with that was that the line were over 33 milj characters long. not popular.

so this is how it looks;

ROW 1 "field1";"field2";"field3";"field4" [enter] <--- ok enter
ROW 2 "field1";"field2";"field3";"[enter] <---not ok enter
"field3";"field4"; [enter] <--- ok enter
ROW 3 [....]

is this understandable?

//A.
ben_josephs
Posts: 2464
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

Just tell us where you want the newlines and where you don't. Give examples of text before and after the change.
mixtape
Posts: 9
Joined: Fri Mar 04, 2011 9:42 am

Post by mixtape »

This is how it is now

"field1";"field2";"field3";"field4";"field5"[enter]
;"field6"[enter]

This is how i want it;

"field1";"field2";"field3";"field4";"field5";"field6"[enter]

//A.
ben_josephs
Posts: 2464
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

You really are not being clear. Do you mean that you want a newline after every (say) twelfth double quote symbol, regardless of where the newlines are initially?

If so you might do this in two steps:

1. Insert a newline after every second double quote symbol if there isn't already one there:
Find what: (([^"]*"){2})(.)
Replace with: \1\n\3

[X] Regular expression

Replace All
2. Remove every sixth newline:
Find what: (.+)\n(.+)\n(.+)\n(.+)\n(.+)\n(.+)
Replace with: \1\2\3\4\5\6

[X] Regular expression

Replace All
These assume you are using "Posix" regular expression syntax:
Configure | Preferences | Editor

[X] Use POSIX regular expression syntax
If this is not what you want, you will have to explain your requirements more clearly.
mixtape
Posts: 9
Joined: Fri Mar 04, 2011 9:42 am

Post by mixtape »

Ok. I'll try to explain it in another way, thanks for being patient by the way.

I have a set of data that contains say 20 fields (columns) on every row. Every row is separated with an "enter"

But, some of the rows are splitted after only the 10th field having the other 10 fields (on that supposed row), continuing on the next row. So that row has to "enters" instead of one.

Now, what i want to do is remove the "split" so that the row of the 20 fields are intact again. hence, remove the enter after the 10th field.

fields, like i said before are quoted with " and separated with ;
ben_josephs
Posts: 2464
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

That is entirely different from what you asked for initially.

Do all the continuation lines begin with a semicolon? If so, try
Find what: \n;
Replace with: ;

[X] Regular expression

Replace All
mixtape
Posts: 9
Joined: Fri Mar 04, 2011 9:42 am

Post by mixtape »

No but it starts with ";"

So thanks alot! This worked like charm! You are a lifesaver!

Thank you!
Post Reply