Remove the same line from multiple files?

General questions about using TextPad

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

Post Reply
GIS_USER
Posts: 7
Joined: Wed Jul 07, 2004 3:18 pm

Remove the same line from multiple files?

Post by GIS_USER »

Hello!

I've got over 100 files which all need line 29 removed. The catch is that line 29 is each document is different, so a simple search and replace accross all files will not work.

The good thing is that it's the same line on all documents. I just can't figure out the correct way for TextPad remove that line number across all the files.

Thanks in advance! :)

-Pete
ruari
Posts: 8
Joined: Mon Jul 10, 2006 1:28 pm
Location: London, England

Post by ruari »

Actually, you could do it with search and replace by making use of \i to generate sequence numbers. For example:
  1. Make a backup of your files, just in case this goes wrong! ;-)
  2. Drag all the files into a Textpad window (to open them all).
  3. Press F8 to open the search dialog and check the radio button 'All Documents'.
  4. Search for $ and replace with xkz\i. (You will be asked to confirm that you want to apply this to all documents)
  5. Search for .*xkz29$\n and replace with nothing (this deletes line 29).
  6. Search for xkz[0-9]+$ and replace with nothing (this will remove the crap at the end of all the other lines that was introduced by the first search and replace).
  7. Close Textpad and choose 'Save All'.
Note: 'xkz' are just some random characters that are unlikely to appear together naturally. You could use any combination of characters that are unlikely to appear together in your document.
GIS_USER
Posts: 7
Joined: Wed Jul 07, 2004 3:18 pm

Post by GIS_USER »

Hot Dog! It Worked! Thanks for the help!
ruari
Posts: 8
Joined: Mon Jul 10, 2006 1:28 pm
Location: London, England

Post by ruari »

Cool, glad it worked for you! :-D

One thought though, if you have to do this again in the future you may want to replace the search in line 5 from .*xkz29$\n to \n.*xkz29$ (to ensure it will be deleted) if there is any possiblity that line 29 is the last line. If the files are always over 29 lines or if you won't need to do this again, then no need to worry!
ruari
Posts: 8
Joined: Mon Jul 10, 2006 1:28 pm
Location: London, England

Post by ruari »

You could also do this with Wildedit despite the fact that it does not (appear to) support generating squence numbers.
  1. Search for .* and replace with tkx$0
  2. Search for ^tkx.*?\r\n(.*?\r\n){27} and replace with $0tkx.
  3. Search for \r\ntkx.*?$ and replace with nothing.
  4. Search for ^tkx and replace with nothing.
Note: \r\n assumes the files are PC formatted, if not you'll need to adjust this accordingly. I picked tkx as I guessed these characters were unlikely to appear together in most documents, you could swap them with something else if you feel they might appear in your document (particularly if they might appear at the start of a line). Also prior to starting make sure 'Regular Expression' and 'Replacement Format' are ticked.
Last edited by ruari on Wed Jul 12, 2006 12:29 pm, edited 2 times in total.
ben_josephs
Posts: 2461
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

What about this in WildEdit?
Find what: ((?:.*\r\n){28})(.*\r\n)((?:.*\r\n)*)
Replace with: $1$3

[X] Regular expression
[X] Replacement format

Options
[X] '.' does not match a newline character
You can't put \n inside parentheses in a TextPad regular expression.
ruari
Posts: 8
Joined: Mon Jul 10, 2006 1:28 pm
Location: London, England

Post by ruari »

ben_josephs, very nice, I bow before you! ;-)

Just a thought. If you usually have the option " '.' does not match a newline character" switched off you could use non-greedy repeats to avoid having to toggle it on first and off after you are done. e.g. ((?:.*?\r\n){28})(.*?\r\n)(.*) as the search. This also makes the regex shorter as you can simplify the end part.

That said I must admit I don't actually understand how '?:' bit works (although it certainly does work on a couple of test files). Would you care to break it down for this mere mortal?
ruari
Posts: 8
Joined: Mon Jul 10, 2006 1:28 pm
Location: London, England

Post by ruari »

I just had a look at 'Non-Marking Parentheses' in the Wildedit help file. It all makes sense now! ;-)
ben_josephs
Posts: 2461
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

I'm glad you found the opportunity to look up something useful. :-)

As for non-greedy repeats, you're right, and that style is possibly faster, too, as it enables the recogniser to avoid some back-tracking. It's a matter fo taste, really. I'm so used to dot not matching newlines (at least within TextPad), and so used to the rule that the longest match (usually) wins, that I tend to do this sort of thing the old way without thinking about it.

I don't use WildEdit, by the way, as its functionality isn't incorporated into TextPad. I use Perl for the sort of thing that I might otherwise use WildEdit for. I just dig it out occasionally to check answers to questions about its style of regex.
ruari
Posts: 8
Joined: Mon Jul 10, 2006 1:28 pm
Location: London, England

Will textpad incorporate Wildedit?

Post by ruari »

ben_josephs wrote:I'm glad you found the opportunity to look up something useful. :-)
Yes, I'm sure that will come in handy in the future.
ben_josephs wrote:I don't use WildEdit, by the way, as its functionality isn't incorporated into TextPad. I use Perl for the sort of thing that I might otherwise use WildEdit for. I just dig it out occasionally to check answers to questions about its style of regex.
That's very nice of you! :P

I'm very new to these forums, despite the fact that I have used Textpad for years, so applogies in advance if this is the type of question that is asked all the time, but is the expectation of most users that Wildedit's functionailty will be rolled into a future version of Textpad?

Personally, I think it would make sense for Textpad to assume all of Wildedit's capabilities. People would still buy Wildedit who needed some Textpad goodness to complement other lesser editors they use. It could be an introduction into the world of Textpad. Wildedit would then be Textpad's little brother. As it is now you have two products that have crossover functionality and are each better at doing some things than the other. It's kind of a weird setup.
Post Reply