Say I have a document as follows:
abc
def
ghi
If I want the document to be:
abc,
def,
ghi,
Is there a way I can do it using Find and Replace. If not, is there any better way?
for example in notepad++ use replace with extended flag and replace '\r' by ',\r'
also if all lines has same length - you can select vertical area of end of lines by mouse and pressing Alt+Shift and type ','
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.
There I have a tex file which contains serval paragraphs like:
\paragraph{name1}
...
\paragraph{name2}
...
Now I want to substitute all the "paragraph" with item, just like:
\item
...
\item
...
to reach that I have tried many commands and finally i used this:
(note that I used "a:" to "z:" as paragraph names)
**:% s/\\paragraph[{][a-z]:[}]/\\item/g**
and I think that is nether pretty nor efficient. I have tried to match the line contains "paragraph" but somehow only this word is replaced. Now that I can delete all such lines with
**:% g/_*paragraph_*/d**
are there anyway better to perform a substitute in the same way?(or to say to substitute all the line contains a specific word)
Your first attempt was almost correct. Rather than this
:% s/\paragraph[{][a-z]:[}]/\item/g
Use this
:% s/^\\paragraph{[a-z|0-9]\+}$/\\item/g
Let's break it down piece by piece:
The ^ character matches the start of the line, so that you don't match something like this:
Some text \paragraph{abc}
The reason why we use \\ instead of \ is because \ is an escape character, so to match it, we escape the escape character.
Doing [a-z|0-9]\+ will match one or more a-z or 0-9 characters, which is what I assume your paragraph names are composed of. If you need capital letters, you could do something like [a-zA-Z|0-9]\+.
Finally, we anchor the expression to the end of the line with $, so that it does not match lines that don't fit this pattern exactly.
Easy way to do with macro!
First, search the pattern using / like /\paragraph
Let's start the macro. Clear register a by pressing qaq.
Press qa to start recording in register a.
Press n to go its occurence. Then, press c$ to delete till end of line and to insert the text. Then, type the text and then press escape key.
Press #a to repeat the process. End macro by pressing q.
Now, macro is recorded and you can press #a once to make changes in all such lines.
You can do this:
:%s/\\paragraph{[^{}]*}/\\item/g
This finds all occurrences of \paragraph{, followed by 0 or more non-{} characters, followed by } (i.e. something like \paragraph{stuff here}), and replaces them by \item.
Or if you want to replace all lines containing paragraph:
:%s/^.*paragraph.*$/\\item/
I would like to match any character and any whitespace except comma with regex. Only matching any character except comma gives me:
[^,]*
but I also want to match any whitespace characters, tabs, space, newline, etc. anywhere in the string.
EDIT:
This is using sed in vim via :%s/foo/bar/gc.
I want to find starting from func up until the comma, in the following example:
func("bla bla bla"
"asdfasdfasdfasdfasdfasdf"
"asdfasdfasdf", "more strings")
I
To work with multiline in SED using RegEx, you should look at here.
EDIT:
In SED command, working with NewLine is a bit different. SED command support three patterns to manage multiline operations N, P and D. To see how it works see this(Working with Multiple Lines) explaination. Here these three operations discussed.
My guess is that N operator is the area of consideration that is missing from here. Addition of N operator will allows to sense \n in string.
An example from here:
Occasionally one wishes to use a new line character in a sed script.
Well, this has some subtle issues here. If one wants to search for a
new line, one has to use "\n." Here is an example where you search for
a phrase, and delete the new line character after that phrase -
joining two lines together.
(echo a;echo x;echo y) | sed '/x$/ { N s:x\n:x: }'
which generates
a xy
However, if you are inserting a new line, don't use "\n" - instead
insert a literal new line character:
(echo a;echo x;echo y) | sed 's:x:X\ :'
generates
a X
y
So basically you're trying to match a pattern over multiple lines.
Here's one way to do it in sed (pretty sure these are not useable within vim though, and I don't know how to replicate this within vim)
sed '
/func/{
:loop
/,/! {N; b loop}
s/[^,]*/func("ok"/
}
' inputfile
Let's say inputfile contains these lines
func("bla bla bla"
"asdfasdfasdfasdfasdfasdf"
"asdfasdfasdf", "more strings")
The output is
func("ok", "more strings")
Details:
If a line contains func, enter the braces.
:loop is a label named loop
If the line does not contain , (that's what /,/! means)
append the next line to pattern space (N)
branch to / go to loop label (b loop)
So it will keep on appending lines and looping until , is found, upon which the s command is run which matches all characters before the first comma against the (multi-line) pattern space, and performs a replacement.
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!