I have a list (in a .txt file) which I'd like to quickly convert to JavaScript Syntax, so I want to take the following:
AliceBlue
AntiqueWhite
Aqua
Aquamarine
Azure
Beige
Bisque
Black
BlanchedAlmond
and convert it to an array literal...
var myArray = ["AliceBlue", "AntiqueWhite", ... ]
I have the list in notepad++ and I need a reg expression to add the " at the start of the line and ", at the end and remove the line break... does anyone have a quick fix to do this? I'm terrible with RegEx.
I often have to perform such tasks so to know how to do this would be a great benefit to me. Many thanks
You won't be able to do it in a single replacement; you'll have to perform a few steps. Here's how I'd do it:
Find (in regular expression mode):
(.+)
Replace with:
"\1"
This adds the quotes:
"AliceBlue"
"AntiqueWhite"
"Aqua"
"Aquamarine"
"Azure"
"Beige"
"Bisque"
"Black"
"BlanchedAlmond"
Find (in extended mode):
\r\n
Replace with (with a space after the comma, not shown):
,
This converts the lines into a comma-separated list:
"AliceBlue", "AntiqueWhite", "Aqua", "Aquamarine", "Azure", "Beige", "Bisque", "Black", "BlanchedAlmond"
Add the var myArray = assignment and braces manually:
var myArray = ["AliceBlue", "AntiqueWhite", "Aqua", "Aquamarine", "Azure", "Beige", "Bisque", "Black", "BlanchedAlmond"];
One simple way is replace \n(newline) with ","(double-quote comma double-quote) after this append double-quote in the start and end of file.
example:
AliceBlue
AntiqueWhite
Aqua
Aquamarine
Beige
Replcae \n with ","
AliceBlue","AntiqueWhite","Aqua","Aquamarine","Beige
Now append "(double-quote) at the start and end
"AliceBlue","AntiqueWhite","Aqua","Aquamarine","Beige"
If your text contains blank lines in between you can use regular expression \n+ instead of \n
example:
AliceBlue
AntiqueWhite
Aqua
Aquamarine
Beige
Replcae \n+ with "," (in regex mode)
AliceBlue","AntiqueWhite","Aqua","Aquamarine","Beige
Now append "(double-quote) at the start and end
"AliceBlue","AntiqueWhite","Aqua","Aquamarine","Beige"
Put your cursor at the begining of line 1.
click Edit>ColumnEditor. Put " in the text and hit enter.
Repeat 2 but put the cursor at the end of line1 and put ", and hit enter.
In notepad++, for placing any thing before value
Press CTRL+H
Replace ^ with ' (sign you want append at the start)
Select search mode as Regular Expression
Click Replace All
In notepad++, for placing any thing After value
Press CTRL+H
Replace $ with ' (sign you want to append at the end)
Select search mode as Regular Expression
Click Replace All
Ex: After performing above steps
AHV01 replaced with 'AHV01'
Happy Learning!!
Thanks.
Place your cursor at the end of the text.
Press SHIFT and ->. The cursor will move to the next line.
Press CTRL-F and type , in "Replace with:" and press ENTER.
You will need to put a quote at the beginning of your first text and the end of your last.
I am using Notepad 8.1.9.2 64bit on Windows10, the replacement procedures can be finished in one step, try this:
Find what: (.+)\r\n
Replace with: "\1",
Note: Wrap around and regular express option is selected.
And then you still need to add bracket manually in your code
Thanks!
Related
I have multiple strings containing sql queries that outputs with \n and \t symbols in my logs.
Here is one for example
SELECT id, codcom, action, dc, tc FROM\n\t\t\t\t(SELECT\n\t\t\t\t\tid, codcom, action, dc, tc\n\t\t\t\tFROM\n\t\t\t\t\tattendance.inout\n\t\t\t\tWHERE\n\t\t\t\t\tdc between '2021-10-25' and '2021-10-31' and cod_ditta = 1 and codcom = 6293\n\t\t\t\tUNION\n\t\t\t\tSELECT\n\t\t\t\t\tid, codcom, action, dc, tc\n\t\t\t\tFROM\n\t\t\t\t\tattendance.inout_timb_ext\n\t\t\t\tWHERE\n\t\t\t\t\tdc between '2021-10-25' and '2021-10-31' and cod_ditta = 1 and codcom = 6293) timbs\n\t\t\t\tORDER BY dc , tc
is it possible to use a single regex to replace all the symbols that starts with the backslash symbol with a new line, tab, etcetera?
I know I could achieve the same result with multiple replaces. This question is more out of curiosity.
What I tried so far
So far I was able to select all the symbols that I want to replace with the regex (in the find section)
\\(\w)
But when it comes to replace I don't know how to replace it with the actual new line, tab, etc.
I tried this so far (in the replace section), without achieving the desired result:
\\$1
\\1
Not possible with \1 because the group will capture the \\t which is treated literally and is not \t (tab).
In sublime text, do it in two steps, use below expressions:
Step 1:
Find All (regex mode): (\\t)
Replace All: (space)
Step 2:
Find All (regex mode): (\\t)
Replace All: \n (newline)
Hope here is the right place to write ask this question.
I am preparing a script to import to a database using notepad++.
I have a huge file that has rows like that:
(10496, '69055-230', 'Rua', '5', 'Manaus', 'Parque 10 de Novembro',
'AM'),
INSERT INTO dne id, cep, tp_logradouro, logradouro, cidade,
bairro, uf VALUES
Is there a way using FIND/REPLACE to replace the ',' to ';' on every line before the INSERT statement?
I am not sure how to match the end of the line before a specific word.
The result would be
(10496, '69055-230', 'Rua', '5', 'Manaus', 'Parque 10 de Novembro',
'AM');
INSERT INTO dne id, cep, tp_logradouro, logradouro, cidade,
bairro, uf VALUES
Find what: ,(?=\s*INSERT)
Replace with: ;
Description
, matches a literal comma
(?=\s*INSERT) is a lookeahead that will assert for (but won't consume)
\s* any number of white spaces (including newlines)
INSERT as literal
If you also want to replace any commas before the end of the file, use
,(?=\h*\R\h*INSERT|\s*\z)
Note both expressions would fail if you have another instance of a comma followed by INSERT that shouldn't be replaced, but in that case you should specify it in the question.
You don't even need a regular expression for that.
Select Extended in Search Mode
Replace ,\nINSERT INTO with ;\nINSERT INTO
This matches , at the end of a line just before INSERT INTO at the beginning of the next line. Keep in mind that \n will match only in a Linux/Unix/Mac OS X file. For Windows use \r\n, for Mac OS Classic \r (reference).
Using sublim text or notepad++, click CTRL+h and replace all ")INSERT," by ");INSERT"
I expect that the INSERT statements will all have the form:
INSERT INTO table col1, col2, col3, ...
VALUES (val1, val2, val3, ...),
^^ what you want to replace
Assuming that the only place that ), will be observed is the end of the VALUES line, then you can just can just do the following replacement:
Find: ),$
Replace: );$
You can do this replacement with the regex option enabled.
I have a question using Notepad++ or UltraEdit to copy the first or two first columns of my file and add them to the end. The problem would be easy if my file had regular columns, but it doesn't. Here is what it looks like:
18,-8 22 30.82,70 2 34.25,
19,-8 23 10,70 1 42.97,
20,-8 23 40.42,700 51.85,
21,-8 24 10.1,70 0 0.89,
22,-8 24 40.05,69 59 10.09,
...
1318,-7 27 26.82,78 3 16.1,
I'd like my id numbers to be copied at the end of each line. I have tried the replace tools, but didn't find the correct expression in order to catch the beginning of the line.
One possible solution using Notepad++
Assuming that the columns are separated by commas ,:
You can record a macro that will execute the following steps:
Press the Home / Pos1 key to set the caret to the first position in the current line
Search for , two times (or how many columns should be copied to the end of the line
Press Shift + Home to select the text from the beginning of the line to the possition of the caret
Copy the selected text by pressing Ctrl + C
Press End to set the caret to the end of the current line
Paste the copied text to the end of the line by pressing Ctrl + V
Move the caret to the next line by pressing ↓ (Arrow Down)
Run the macro till the end of the file is reached.
PS: Always backup your data before running the macro!
Try the following in Regular Expression search and replace mode:
Find:
^([0-9]*)(.*)$
Replace:
\1\2\1
Explanation
^ and $ are anchors for the beginning and end of a line, respectively.
^([0-9]*) matches from the start of the line until a non-digit is met (in your case, a comma). The
( and ) make the matched expression available for usage in the Replace box via \1.
(.*)$ matches everything else until the end of the line. Again, the brackets make the matched expression accessible, this time via \2.
So, since you want a copy of the first column at the end of the line, you can just do:
Replace: \1\2\1
If, instead, you wanted to move the first column to the end, you might want to do
Find: ^([0-9]*),(.*)$
Replace:
\2\1
Note the added comma in the find expression. Without it, the comma after the first column of data would get matched as part of the (.*) expression and would thus remain at the beginning of your lines when your lines gets replaced with \2\1.
edit Oops, others have beaten me to (basically) the same answer, but I hope the explanation is helpful nevertheless.
Find what: ^([0-9]*)(.*)
Replace with: \2\1
Hope this serves you.
In Notepad++:
Open the Replace dialog: Search -> Replace...
To copy the first field to the end:
Find what: ^([0-9]+,)(.*)$
Replace with: \1\2\1
To move the first field to the end:
Find what: ^([0-9]+,)(.*)$
Replace with: \2\1
I've got a CSV file with some 600 records where I need to replace some [CRLF] with a [space] but only when the [CRLF] is positioned between two ["] (quotation marks). When the second ["] is encountered then it should skip the rest of the line and go to the next line in the text.
I don't really have a starting point. Hope someone comes up with a suggestion.
Example:
John und Carol,,Smith,,,J.S.,,,,,,,,,,,,,+11 22 333 4444,,,,,"streetx 21[CRLF]
New York City[CRLF]
USA",streetx 21,,,,New York City,,,USA,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,Normal,,My Contacts,[CRLF]
In this case the two [CRLF] after the first ["] need to be replaced with a space [ ]. When the second ["] is encountered, skip the end of the line and go to next line.
Then again, now on the next line, after the first ["] is encountered replace all [CRLF] until the second ["] is encountered. The [CRLF]s vary in numbers.
In the CSV-file the amount of commas [,] before (23) and after (65) the 2 quotation marks ["] is constant.
So maybe a comma counter could be used. I don't know.
Thanks for feedback.
This will work using one regex only (tested in Notepad++):
Enter this regex in the Find what field:
((?:^|\r\n)[^"]*+"[^\r\n"]*+)\r\n([^"]*+")
Enter this string in the Replace with field:
$1 $2
Make sure the Wrap around check box (and Regular expression radio button) are selected.
Do a Replace All as many times as required (until the "0 occurrences were replaced" dialog pops up).
Explanation:
(
(?:^|\r\n) Begin at start of file or before the CRLF before the start of a record
[^"]*+ Consume all chars up to the opening "
" Consume the opening "
[^\r\n"]*+ Consume all chars up to either the first CRLF or the closing "
) Save as capturing group 1 (= everything in record before the target CRLF)
\r\n Consume the target CRLF without capturing it
(
[^"]*+ Consume all chars up to the closing "
" Consume the closing "
) Save as capturing group 2 (= the rest of the string after the target CRLF)
Note: The *+ is a possessive quantifier. Use them appropriately to speed up execution.
Update:
This more general version of the regex will work with any line break sequence (\r\n, \r or \n):
((?:^|[\r\n]+)[^"]*+"[^\r\n"]*+)[\r\n]+([^"]*+")
Maybe do it in three steps (assuming you have 88 fields in the CSV, because you said there are 23 commas before, and 65 after each second ")
Step 1: replace all CR/LF with some character not anywhere in the file, like ~
Search: \r\n Replace: ~
Step 2: replace all ~ after every 88th 'comma group' (or however many fields in CSV) with \r\n -- to reinsert the required CSV linebreaks:
Search: ((?:[^,]*?,){88})~ Replace: $1\r\n
Step 3: replace all remaining ~ with space
Search ~ Replace: <space>
In this case the source data is generated by the export function in GMail for your contacts.
After the modification outlined below (without RegEx) the result can be used to tidy up your contacts database and re-import it to GMail or to MS Outlook.
Yes, I am standing on the shoulders of #alan and #robinCTS. Thank you both.
Instructions in 5 steps:
use Notepad++ / find replace / extended search mode / wrap around = on
-1- replace all [CRLF] with a unique set characters or a string (I used [~~])
find: \r\n and replace with: ~~
The file contents are now on one line only.
-2- Now we need to separate the header line. For this move to where the first record starts exactly before the 88th. comma (including the word after the 87th. comma [,]) and enter the [CRLF] manually by hitting the return key. There are two lines now: header and records.
-3- now find all [,~~] and replace with [,\r\n] The result is one record per line.
-4- remove the remaining [~~] find: ~~ and replace with: [ ] a space.
The file is now clean of unwanted [CRLF]s.
-5- Save the file and use it as intended.
This question already has answers here:
Removing empty lines in Notepad++
(22 answers)
Closed 9 years ago.
I have a text file with a thousand lines of numbers like so:
402
115
90
...
As you can see there is a blank line in between each number that I want to remove so that I have
402
115
90
...
How can I do this?
Press Ctrl+H (Replace)
Select Extended from SearchMode
Put \r\n\r\n in Find What
Put \r\n in ReplaceWith
Click on Replace All
As of NP++ V6.2.3 (nor sure about older versions) simply:
Go menu -> Edit -> Line operations
Choose "Remove Empty Lines" or "Remove Empty Lines (Containing white spaces)" according to your needs.
By the way, in Notepad++ there's built-in plugin that can handle this:
TextFX -> TextFX Edit -> Delete Blank Lines (first press CTRL+A to select all).
This will remove any number of blank lines
CTRL + H to replace
Select Extended search mode
replace all \r\n with (space)
then switch to regular expression and replace all \s+ with \n
You can record a macro that removes the first blank line, and positions the cursor correctly for the second line. Then you can repeat executing that macro.
This should get your sorted:
Highlight from the end of the first line, to the very beginning of the third line.
Use the Ctrl + H to bring up the 'Find and Replace' window.
The highlighed region will already be plased in the 'Find' textbox.
Replace with: \r\n
'Replace All' will then remove all the additional line spaces not required.
Here's how it should look: