Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

What I want to do is:

  • Add <title> to the beginning of lines 1, 3, 5, etc...(hundreds of lines in a notepad++ file)
  • Add </title> to the end of lines 1, 3, 5, etc...

Then:

  • Add <link> to the beginning of even number lines (e.g. lines 2, 4, 6, 8, etc...)
  • Add </link> to the end of even number lines (e.g. lines 2, 4, 6, 8, etc...)
share|improve this question
    
Are you sure it's not <link> instead of with parentheses? You need to surround those tags with backticks. –  slhck Nov 13 '12 at 8:33
    
Yes you're correct, except when O tried to post "< >" in my original post, they wouldn't show, so I've changed them to "( )" for the sake of giving an example. Thanks –  Paul M Nov 13 '12 at 15:07
    
Hopefully I havn't messed up the question in my edit. Sorry if I have ! –  Sirex Nov 13 '12 at 21:00
add comment

6 Answers

Perhaps you can approach this in a different way than "alternate lines" (which would really be best served using a scripting language).

Assuming that the lines you want to be enclosed with "link" contain something that identifies them as a link, you could replace these lines first, then assume any left must be titles. So lets say that your link lines contain "http":

  1. Control-H to bring up replace dialog
  2. Enter (http.*) as the search criteria (find any line with http in it, and store in variable \1
  3. Enter (link)\1(/link) as the replace criteria - replace what was found in the previous step with itself contained in link tags
  4. Replace all - now all your link lines are enclosed, any remaining lines must be titles

  1. Enter ^([^(\(link\))]*)$ as the search criteria (find any line that does not contain (link)
  2. Enter (title)\1(/title) as the replace criteria
  3. Replace all

This will then enclose the remaining lines with title tags.

share|improve this answer
    
Thank You Very Much! –  Paul M Nov 13 '12 at 20:24
add comment

Personally, I think it would be a lot easier to accomplish this by writing a simple perl/python script, but here is a way you can do it in Notepad++. I tried it with some sample data and it worked as you wanted.

  • Open the file you want to edit
  • Go to the start of the first line in the file
  • Click on the Macro --> Start Recording option in the menu
  • Insert (title), press end button to go to the end of the line, insert (/title)
  • press home button go to the beginning of the line
  • press down button to go to line 2
  • Insert (link), press end button to go to the end of the line, insert (/link)
  • press home button go to the beginning of the line
  • press down button to go to line 3
  • Click on the Macro --> Stop Recording option in the menu

Now you can replay the macro how many ever times you want and it will repeat/rinse the actions.

share|improve this answer
    
Playing back macros is typically fairly slow. Quite acceptable to modify 5 or 10 pairs of lines or so. But the OP said they have hundreds of lines in the file. It would probably be quite inconvenient to have to replay the macro and wait for it to complete, then repeat it hundreds of times. –  Kevin Fegan Nov 13 '12 at 9:22
    
Thank You Very Much! Worked like a charm! –  Paul M Nov 13 '12 at 20:23
1  
@kevin-fegan: Notepad++ offers the option to run a macro multiple times (or until end-of-file). Hundreds of lines are dealt with rather quickly. –  Derethus Nov 14 '12 at 14:18
    
@Derethus - Thanks for that (+1) I don't use Notepad++ that often and I didn't know that. –  Kevin Fegan Nov 14 '12 at 22:08
add comment

Edit: This answer was written and tested while using Notepad++ version 5.9.8 and it will work for the current version of Notepad++ (v 6.2), and most previous versions. After I wrote this answer, I updated Notepad++ to the current version of 6.2 and found that the solution is now much easier. So, if you are using Notepad++ version 5.9.8 (or earlier versions), and you don't want to update to the current version of Notepad++, then this answer will work for you. If you have upgraded to Notepad++ version 6 or later, then see my other answer which is much simpler.

Using this as the original file:

This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
This is line 6
This is line 7

Step 1. Place a Unique tag with "<title>" and "</title>" at the beginning and end of all lines:

In "Regular Expression" Mode (F: is Find expression, R: is Replace expression):  
    F:^(.*)$
    R:<##@@Begin@@##><title>\1</title><##@@End@@##>

Results in this:

<##@@Begin@@##><title>This is line 1</title><##@@End@@##>
<##@@Begin@@##><title>This is line 2</title><##@@End@@##>
<##@@Begin@@##><title>This is line 3</title><##@@End@@##>
<##@@Begin@@##><title>This is line 4</title><##@@End@@##>
<##@@Begin@@##><title>This is line 5</title><##@@End@@##>
<##@@Begin@@##><title>This is line 6</title><##@@End@@##>
<##@@Begin@@##><title>This is line 7</title><##@@End@@##>

Step 2. Remove all the "Newlines" (\r\n):

In "Extended" mode:
    F:\r\n
    R:

Results in this (entire file is on a single line):

<##@@Begin@@##><title>This is line 1</title><##@@End@@##><##@@Begin@@##><title>This is line 2</title><##@@End@@##><##@@Begin@@##><title>This is line 3</title><##@@End@@##><##@@Begin@@##><title>This is line 4</title><##@@End@@##><##@@Begin@@##><title>This is line 5</title><##@@End@@##><##@@Begin@@##><title>This is line 6</title><##@@End@@##><##@@Begin@@##><title>This is line 7</title><##@@End@@##>

Step 3. Change "<title>" and "</title>" to "<link>" "</link>" in the second line of every two lines ("even" lines):

In "Regular Expression" Mode:
    F:<##@@Begin@@##>(<title>.*?</title><##@@End@@##>)<##@@Begin@@##><title>(.*?)</title>(<##@@End@@##>)
    R:\1<link>\2</link>\3

Results in this (entire file is still on a single line):

<title>This is line 1</title><##@@End@@##><link>This is line 2</link><##@@End@@##><title>This is line 3</title><##@@End@@##><link>This is line 4</link><##@@End@@##><title>This is line 5</title><##@@End@@##><link>This is line 6</link><##@@End@@##><title>This is line 7</title><##@@End@@##>

Step 4. Replace the "end" tag with a "Newline" (\r\n):

In "Extended" mode:
    F:<##@@End@@##>
    R:\r\n

Results in this:

<title>This is line 1</title>
<link>This is line 2</link>
<title>This is line 3</title>
<link>This is line 4</link>
<title>This is line 5</title>
<link>This is line 6</link>
<title>This is line 7</title>

So, 4 steps. Not bad if you need to do this only once, or only very occasionally.

The big limitation with Notepad++, is that you cannot combine "Extended" and "Regex" search/replacements. So, in "Extended" mode, you can search for "Newlines" but you cannot search using "wildcards" (*.?), and in "Regex" mode, you can search using "wildcards", but you cannot search for "Newlines".

If you need to do this on a regular basis, it would be much easier to use Textpad (http://textpad.com), you can do it in two steps (if there is a possibility that the file has an "odd" number of lines"):

Step 1. add "<title>" "</title>" to all lines:

In "Regular Expression" Mode (F: is Find expression, R: is Replace expression):  
    F:^(.*)$
    R:<title>\1</title>

Step 2. Change "<title>" and "</title>" to "<link>" and "</link>" in the second line of every two lines ("even" lines):

Still in "Regular Expression" Mode:  
    F:^(<title>.*</title>)\n<title>(.*)</title>
    R:\1\n<link>\2</link>

With Textpad, if you know the file always has an "even" number if lines, you can do the whole thing in one shot:

In "Regular Expression" Mode:  
    F:^(.*)\n(.*)\n
    R:<title>\1</title>\n<link>\2</link>\n

You could also make this replacement easily with awk.

Edit: here is some information and an example of doing it with awk...

Awk is a scripted programming language that was created for unix, but is now available for Windows and I believe also for Mac.

The primary use for awk is to process (manipulate and filter) text output and text files. For example, to process a "log" file to print particular fields and lines.

Awk does not have a user-interface. It is a program that is typically run from command window prompt, or a "shell script" (unix) or "batch file" (Windows).

You tell awk what to do with the text that is being processed by specifying awk commands on the command line when you run awk, or by creating an awk command script file.

On unix, it is very common to specify all the awk commands on the command line and not in a awk script file. On Windows it is just the opposite... because of the way awk uses quote-marks, parenthesis, and other punctuation in its commands, awk scripts generally (maybe always) confuse the Windows command prompt. So on Windows it is nearly always better to use awk by putting the awk commands into an awk script file.

Here is an example using awk as a solution to the question.

First, let's say the text you want manipulate is in a file named:

example.txt

Next, create a file with the following code. You could save it as:
oddeven.awk (or whatever you wish).

{
    if (NR % 2 == 1)
    {
        print "<title>"$0"</title>"
    }
    else
    {
        print "<link>"$0"</link>"
    }
}

Alternately, you could specify this awk script on a single line like this:

{ if (NR % 2 == 1) {print "<title>" $0 "</title>"} else {print "<link>" $0 "</link>"} }

The awk command line parameters we will use are:

-f "oddeven.awk"   This is the name of the awk command script file.
"example.txt"      This is the name of the text file you are reading (and manipulating).
"output.txt"       This is the name of the file you are creating that will contain the modified text.

Then, the command line you would use to call awk is like this:

awk -f "oddeven.awk" "example.txt" > "output.txt"

If this process of modifying the file is a "one-time" thing, then using Notepad++ or another text editor is probably easier than learning a scripting language.

But, if you will be doing this same process over and over, or if you want to automate the process so it can be done from a shell script (unix) or batch file (Windows), then awk is the way to go.

share|improve this answer
    
Wow! I'm overwhelmed with abundant knowledge and expertise! Thank You! Btw, what do you mean by "awk" in your last comment? Cheers! –  Paul M Nov 13 '12 at 20:25
    
@PaulM I added an explaination about awk, and an example of how to use it. –  Kevin Fegan Nov 14 '12 at 13:14
    
I edited both of my answers to use "<>" instead of "()" to reflect the same changes made to the question. –  Kevin Fegan Nov 14 '12 at 14:11
add comment

This answer is for those using Notepad++ version 6 and later. If you are using a version of Notepad++ prior to version 6 then see my other answer for the working solution.

Previous versions of Notepad++ had a big search/replace limitation that you couldn't combine "Extended" and "Regex" search/replacements. So, in "Extended" mode, you could search for "Newlines" but you couldn't search using "wildcards" (*.?), and in "Regex" mode, you could search using "wildcards", but you couldn't search for "Newlines". This meant that you couldn't do multi-line Regex searches (multi-line yes, but not if the search included Regex patterns).

Starting with Notepad++ version 6 and later, they began using a different Regex engine that now allows the Regex search/replace to search for (and replace) "Newlines" and at the same time it can search using "wildcards" (*.?), so with the newer versions of Notepad++ this replacement can be made in 1 or 2 steps, depending on the (even or odd) number of lines in the file.

Using the "1-Step" method here will work if the file will always have an "even" number of lines. This is because the first method takes lines in pairs and makes the changes to even and odd lines at the same time.

If there is an "odd" number of lines in the file, then using the "1-Step" method will leave the last line unmodified because it does not belong to a "pair" of lines. In that case, use the "2-Step" method which will work for all files regardless of the (even or odd) number of lines it contains.

The "2-Step" method (if there is a possibility that the file has an "odd" number of lines"):

Step 1. Add "<title>" and "</title>" to all lines:

In "Regular Expression" Mode (F: is Find expression, R: is Replace expression):  
    F:^(.*)$
    R:<title>\1</title>

Step 2. Change "<title>" and "</title>" to "<link>" and "</link>" in the second line ("even" lines) of every pair of two lines:

Still in "Regular Expression" Mode:  
    F:^(<title>.*</title>)\r\n<title>(.*)</title>
    R:\1\r\n<link>\2</link>

If you know the file always has an "even" number if lines, you can do the entire replacement in one shot:

In "Regular Expression" Mode:  
    F:^(.*)\r\n(.*)\r\n
    R:<title>\1</title>\r\n<link>\2</link>\r\n
share|improve this answer
    
Thanks to all of you guys/gals. I really appreciate your help! This is truly an AMAZING site! Cheers! –  Paul M Nov 13 '12 at 23:59
    
I edited both of my answers to use "<>" instead of "()" to reflect the same changes made to the question. –  Kevin Fegan Nov 14 '12 at 14:09
add comment

I know this isn't a definite answer but it can lead you in the right direction. If you take a look at this site it shows you to use the replace function in regex mode to insert things at the beginning and end of lines.

E.G. Find what: ^(.+)$ Replace with: title\1/title

Now that will edit every line, but that is half way there.

share|improve this answer
    
Thanks for your swift response, I have already used that in other problems, but it doesn't help with this one though! –  Paul M Nov 13 '12 at 3:40
    
You're welcome. I know it's not exactly what you wanted but I was sharing some info to maybe get you thinking. I hope you figure it out –  adamk33n3r Nov 13 '12 at 3:42
    
If someone has a solution, please let me know...I don't know how to do this...Thanks –  Paul M Nov 13 '12 at 4:20
add comment

I know this was last year, but I'm new to regex and took this as a personal challenge...

See Stackoverflow for adding leading line numbers (Alt+Select for column mode select, Alt+C for Column Editor).

Use it to achieve this look:

1  line
2  line
3  line
4  line
5  line
6  line
7  line
8  line
9  line
10 line
11 line
12 line
13 line
14 line
15 line
16 line
17 line
18 line
19 line
20 line
21 line
22 line
23 line
24 line

Then use regex for adding your text (Strg+H, activate Regular expression Search Mode).
Replacing ^[0-9]+[13579]+[ ]([^\n]$) with <title>\1</title> will take care of every uneven line greater than 10.
Replacing ^[0-9]+[02468]+[ ]([^\n]$) with <link>\1</link> will take care of every even line greater than 10.

Either alter the first 10 lines per hand, or copy them into a new file and use ^[13579]+[ ]([^\n]$) on them.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.