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.
Are you sure it's not
<link>
instead of with parentheses? You need to surround those tags with backticks. – slhck – 2012-11-13T08:33:44.233Yes 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 – 2012-11-13T15:07:52.133
Hopefully I havn't messed up the question in my edit. Sorry if I have ! – Sirex – 2012-11-13T21:00:52.933