Replace a line start with single space

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
srive99
Posts: 30
Joined: Fri Apr 16, 2010 1:45 pm

Replace a line start with single space

Post by srive99 »

I have following lines in ldap format

cn=some, ou=some, ou=s
ome, o=org
cn=somethingelse, ou=same, o
u=same, o=org
cn=same, ou=some,ou=something,
o=org

I want to do couple of things
1, I want to remove a line that start with space. I'm looking for following output
cn=some, ou=some, ou=s
cn=somethingelse, ou=same, o
cn=same, ou=some,ou=something,

2, I want to join a line start with space to previous line and output should as below

cn=some, ou=some, ou=some,o=org
cn=somethingelse, ou=same, o=org
cn=same, ou=some,ou=something, o=org


Could some one help to do this with regular expression. I need to lot of export, cleans and import the data to ldap.

Thanks
User avatar
kengrubb
Posts: 324
Joined: Thu Dec 11, 2003 5:23 pm
Location: Olympia, WA, USA

Post by kengrubb »

If I understand your goal correctly, thy these three Regex Replace Alls. The Replace with in the first two is nothing, blank, zip, nada.

Code: Select all

Find what: [[:blank:]]+$
Replace with:

Find what: \n
Replace with:

Find what: (cn[=])
Replace with: \n\1
(2[Bb]|[^2].|.[^Bb])

That is the question.
srive99
Posts: 30
Joined: Fri Apr 16, 2010 1:45 pm

Post by srive99 »

I tried but when I try with second RE, it stuck and never come back. Had to to kill the textpad.

Thanks
User avatar
kengrubb
Posts: 324
Joined: Thu Dec 11, 2003 5:23 pm
Location: Olympia, WA, USA

Post by kengrubb »

OK, let's try this. The first Replace with is nothing, to eliminate trailing blanks.

Code: Select all

Find what: [[:blank:]]+$ 
Replace with:

Find what: \n([^c][^n][^=])
Replace with: \1
(2[Bb]|[^2].|.[^Bb])

That is the question.
ben_josephs
Posts: 2461
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

To show your examples with their original layout, including leading white space, enclose them in

Code: Select all

[/color][/b] ... [b][color=blue]
[/color][/b] tags (without the space I've added to stop the tags being interpreted here). I had to examine the source of the page to understand your requirement.

1. To remove each line that starts with a space:
Find what: ^_.*\n [Replace the underscore with a space]
Replace with: [nothing]

[X] Regular expression

Replace All
2. To join each line that starts with space to the line that precedes it:
Find what: \n_ [Replace the underscore with a space]
Replace with: [nothing]

[X] Regular expression

Replace All
[Edit: Error in second regex corrected.]
Last edited by ben_josephs on Wed Dec 28, 2011 7:06 pm, edited 1 time in total.
srive99
Posts: 30
Joined: Fri Apr 16, 2010 1:45 pm

Post by srive99 »

Thanks Ben

First one worked to remove the lines that start with a space but second one did not work to join the lines
that starts with space to the preceding line.
I get the can not find regular expression error. I made sure Regular Expression checkbox selected.

dn: cn=user,ou=test,ou=test,dc=xxxx,
_dc=yyyy,dc=org
memberof: cn=groupone,ou=test,ou=testa,dc=xx
_xx,dc=yyyy,dc=org
memberof: cn=grouptwo,ou=test,ou=testa,dc=xxxx,
_dc=yyyy,dc=org
memberof: cn=groupthree,ou=test,ou=testa,dc=xxxx,d
_c=yyyyy,dc=org
where _ is a space in my file. I need following ouput.
dn: cn=user,ou=test,ou=test,dc=xxxx,dc=yyyy,dc=org
memberof: cn=groupone,ou=test,ou=testa,dc=xxxx,dc=yyyy,dc=org
memberof: cn=grouptwo,ou=test,ou=testa,dc=xxxx,dc=yyyy,dc=org
memberof: cn=groupthree,ou=test,ou=testa,dc=xxxx,dc=yyyyy,dc=org
Thanks again.
ben_josephs
Posts: 2461
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

It does work. Are you using the second replacement as well as the first one? You should be using it instead of the first one. Is there a spurious space at the end of your regex?

Please use code tags, as I suggested, to display leading spaces correctly, not quote tags.
srive99
Posts: 30
Joined: Fri Apr 16, 2010 1:45 pm

Post by srive99 »

I just tried again but no luck. I did not use it with first one as I had to use those on two different files.
As the first one worked, I been working on the second where I need to join the lines with preceding lines.

I'm using following..

Code: Select all

Find what: ^\n-  (space instead of -)
Replace with: n/a
Thanks again.
ben_josephs
Posts: 2461
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

I'm sorry. It's my error: a bad edit when constructing my posting. The regex should be
\n_ [Replace the underscore with a space]

I've corrected my original post.

Apologies again.
srive99
Posts: 30
Joined: Fri Apr 16, 2010 1:45 pm

Post by srive99 »

You are the man. That did the trick.

Thanks a bunch.
Post Reply