regex to move entire line in a specific line position in multiples file
-
@Alan-Kilborn
Thanx for reply.
where I wrote XXXX it means that there are different words. I tried the formula but it didn’t give any results, the files remained as they were before. -
@Rockberto-Manenti said in regex to move entire line in a specific line position in multiples file:
I tried the formula but it didn’t give any results, the files remained as they were before.
Sorry, I forgot to mention that you should checkmark Wrap around and press the Replace All button. Then it works, assuming that your double-quote characters are the ‘simpler’ type (
"
) and not the fancy Unicode types shown in your original posting. -
@Alan-Kilborn
Thanx again. I’m definitely doing something wrong and it doesn’t work.
I simplify the example and remove " in the fileslet’s say I have some text files composed like this
DATE 1980
GENRE HEAVY METAL
PERFORMER SAXON
TITLE WHEELS OF STEELand another like this
GENRE HARD ROCK
PERFORMER LED ZEPPELIN
TITLE PRESENCE
DATE 1976I would like to have these results
PERFORMER SAXON
TITLE WHEELS OF STEEL
DATE 1980
GENRE HEAVY METALPERFORMER LED ZEPPELIN
TITLE PRESENCE
DATE 1976
GENRE HARD ROCKthanks again for your patience
-
With the original data
REM DATE XXXX REM GENRE "XXXX" PERFORMER "XXXXXXXX" TITLE "XXXXXXXX"
using your regex, I got 0 matches when
REM DATE XXXX
is the first line in the file.If I put any lines before that, like:
any line before REM DATE XXXX REM GENRE "XXXX" PERFORMER "XXXXXXXX" TITLE "XXXXXXXX"
… then your regex matches and gives the expected results:
PERFORMER "XXXXXXXX" TITLE "XXXXXXXX" REM DATE XXXX REM GENRE "XXXX" any line before
If I change from
.+?
to.*?
, so my full regex is(?s-i)\A(.*?)(REM DATE \w+\RREM GENRE "\w+"\R)(PERFORMER "\w+"\RTITLE "\w+"\R)
then I getPERFORMER "XXXXXXXX" TITLE "XXXXXXXX" REM DATE XXXX REM GENRE "XXXX"
As my output.
So that’s what I’d recommend to @Rockberto-Manenti for the original problem statement.
@Rockberto-Manenti used a different set of data the second time:
DATE 1980 GENRE HEAVY METAL PERFORMER SAXON TITLE WHEELS OF STEEL
With those longer pieces of text, it also comes to light that even with prefix lines, Alan’s wouldn’t have matched, because he assumed exactly one word (no spaces) inside the quotes. But since you show spaces are possible in the XXXX sequence, and I am guessing that somewhere you also have punctuation (since performers and song titles sometimes do), I would recommend
(?s-i)\A(.*?)(?-s)(REM DATE .*?\RREM GENRE ".*?"\R)(PERFORMER ".*?"\RTITLE ".*?"\R)
if you have quotes and(?s-i)\A(.*?)(?-s)(DATE .*?\RGENRE .*?\R)(PERFORMER .*?\RTITLE .*?\R)
if you have neither quote marks nor REM prefix.Please note: changing your spec (like having
REM
before it originally and then changing it to not, or having quotes around the words or not) will change the regex. Showing us real data originally, instead of XXXX data, would have prevented some of the confusion.And I’m not convinced that you’ve actually shown things with enough variety yet. I am guessing that you might really have those four lines in any of the 4!=24 possible orders, and you always want them to output in the order of PERFORMER, TITLE, DATE, GENRE, regardless of order. This would completely change the regex again (pretty drastically) to be able to handle all possible orders.
For example, assuming that the first four lines are always one of the DATE, GENRE, PERFORMER, and TITLE lines, with anything after on the same line, then I would use
(?=(?s:.*)(?-s:(^PERFORMER\b.*\R)))(?=(?s:.*)(?-s:(^TITLE\b.*\R)))(?=(?s:.*)(?-s:(^DATE\b.*\R)))(?=(?s:.*)(?-s:(^GENRE\b.*\R)))(?-s:^.+?\R){4,}
replaced with${1}${2}${3}${4}
. Though mine won’t work if you have theany other data
lines before those four lines, or in between those lines.This is why our Template for Search/Replace Questions specifically says that you should make the data match as close to your real-world problem as you can. If you had used that template, you would have saved yourself and Alan lots of time.
I can understand using XXXXX when you have secret data (real names, addresses, business info, etc). But honestly, for a list of songs and their performers, there’s nothing secret, and all your XXXing did was waste a bunch of time and effort.
----
Useful References
-
@PeterJones said in regex to move entire line in a specific line position in multiples file:
using your regex, I got 0 matches when REM DATE XXXX is the first line in the file.
So when people don’t ask their questions correctly,
(a)
I should move on to reading the next post – which I should have done in this case, or(b)
sometimes I copy their whole original post into Notepad++ and start working with that as their data – which I did in this case.Often, as people respond and the back and forth grows lengthy, I mainly lose interest, but sometimes their need is clarified, making the original post read differently.
So…in this case, if you copy the OP’s first post contents into Notepad++, and fixup the quotes to be normal quotes, and then run my regex replacement on it, you’ll see that it does what was originally requested (or at least how I interpreted the need the first time I read it).
Really, I should choose my option
(a)
more often.
and all your XXXing did was waste a bunch of time and effort.
So true, users think they know how to generalize a question; they’d be better off providing a real example the first time around.
-
I deeply apologize for the confusion. My fault especially for my incorrect English.
In the first example I had put XXXX only to indicate generic words.
There is no hidden data, they are all .cue text files (for flac music) in which however the order of the four fields (PERFORMER, TITLE, DATE, GENRE) is not always the same, they can often be in different positions.
On notepad++ I created a macro that eliminates everything superfluous and leaves me with only the four lines in question, but for cataloging purposes, I would like all the .cue files to have the same order, therefore the entire line that begins with PERFORMER be the first, the line starting with TITLE is the second, the line starting with DATE is the third and the line starting with GENRE is the last.
Thanks again and I apologize again -
@Rockberto-Manenti said in regex to move entire line in a specific line position in multiples file:
I created a macro that eliminates everything superfluous and leaves me with only the four lines in question
Then after runnnig that macro, add the regex I supplied, because what I supplied will take those four lines in any order and replace the with the four lines in the order you requested.
-
sorry once again for the inconvenience, but I tried to use the regex but I get no results.
the message says nothing has changed, What am I doing wrong? -
@Rockberto-Manenti said in regex to move entire line in a specific line position in multiples file:
What am I doing wrong?
Hard to say without more information from you.
If I take one of your original blocks of text and do the Replace on it with Peter’s expression:
Then I obtain the ordering that I think you want:
-
I don’t know the reason but this is the result
-
@Rockberto-Manenti said in regex to move entire line in a specific line position in multiples file:
I don’t know the reason but this is the result
It’s because your file doesn’t end in a newline.
The easiest way to fix that is to change your macro that you use before this to make sure there’s a newline (
\r\n
) at the end of the last lineThe other option would be every time you see a
\R
in the regex that I supplied, use(?:\R|\Z)
instead. -
@PeterJones said in regex to move entire line in a specific line position in multiples file:
It’s because your file doesn’t end in a newline.
Genius!!! Works great!!!
many many many thanx!!! you rulez!