I know how to add something to the end of every line, but how to add text at the end of the lines containing specific words.
Some line of text here
Tomatoes Oranges
Mili Deci Centi
Some line of text there
Fire Flame
Dog Cat
Tall Small
Some line of text with more text
Mother farher
-------
I want to add characters at the end of the lines containing "Some line", something like this:
Some line of text here EXTRATEXT
Tomatoes Oranges
Mili Deci Centi
Some line of text there EXTRATEXT
Fire Flame
Dog Cat
Tall Small
Some line of text with more text EXTRATEXT
Mother farher
-------
The lines end in different characters, so I need to search for a pattern that is inside the line, and add text at the end of those line.
Replace the following pattern:
Some line.*
With:
$0 EXTRATEXT
This matches from Some line up to the end of the line (.*, as . matches any character but a newline).
You can then replace the whole match ($0) with itself followed by the extra text you want.
[a-zA-Z]+\n or \w+\n or mutliple \n+ at the end if you want to clean empty lines too. Finally if it's important that the word is capital on the firs letter: [A-Z][a-zA-Z]+\n
Why don't you try delimiting the regex pattern with a line-break, or a carriage return.
I think it might be achieved with \r\n at the end of the regex, on Notepad++.
Related
I have the following text, and I need to extra parts out of it:
[Firstname LastName 21/06/2018 - 17:27]
Lorem Ipsum
[Foo Bar 25/01/2017 - 12:10]
Lorem Ipsum - First line
Lorem ipsum Second line
Lorem ipsum third line
Some other random text
I need to extract parts of this text, which I have almost managed to do using the following regex:
\[(?<name>\w+? \w+?) (?<date>\d{2}\/\d{2}\/\d{4}) - (?<time>\d{2}:\d{2})\]\n*(?<note>.+)
Everything works correctly, except for the group labelled <note>, it's only picking up the first line of the note. If there is a line break in the note, then anything after the line break is not picked up.
How can I get it to match all text in the note section, until the regex finds a double line break?
Instead of looking for . (which does not include newlines by default) you can look for [^[], or every character before the next square bracket, followed by two line breaks:
\[(?<name>\w+? \w+?) (?<date>\d{2}\/\d{2}\/\d{4}) - (?<time>\d{2}:\d{2})\]\n*(?<note>[^[]+\n\n)
https://regex101.com/r/12S3ZQ/3
I have modified your original regex to give you the expected output.
\[(?<name>\w+? \w+?) (?<date>\d{2}\/\d{2}\/\d{4}) - (?<time>\d{2}:\d{2})\]\n*(?<note>.+\n?\n?)+
It should match everything until the double line break, notice the only change is at the end.
Instead of...
(?<note>.+)
It is now...
(?<note>.+\n?\n?)+
Edit: Changed the regex so it will include lines separated by ONE line break, but not two.
You may use
\[(?<name>\w+? \w+?) (?<date>\d{2}\/\d{2}\/\d{4}) - (?<time>\d{2}:\d{2})\]\s*(?<note>[\s\S]+?)(?=\n{2}|$)
See the regex demo
The (?<note>[\s\S]+?)(?=\n{2}|$) will match 1+ chars, as few as possible, up to the first 2 newline chars or end of string.
If your regex engine supports \R construct to match any line break sequence, you can use (?=\R{2}|$).
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 have a huge file around half a gig and the file has records shown below:
44 ,1577,23GRE ,GREASE THE ENGINE
44 ,1577,23GRE ,GREASE THE ENGINE
44 ,1577,24GRE ,GREASE THE WHEELS
I want to remove white spaces between the the commas and whitespaces after content "GREASE THE ENGINE" and convert the file as shown below using vi:
"44","1577","23GRE","GREASE THE ENGINE"
"44","1577","23GRE","GREASE THE ENGINE"
"44","1577","24GRE","GREASE THE WHEELS"
I tried removing whitespaces by giving a command :1,$s/ //g This removes all the whitespace and renders the file as shown below which defeats the purpose. I want GREASE THE ENGINE with spaces.
44,1577,23GRE,GREASETHEENGINE
44,1577,23GRE,GREASETHEENGINE
44,1577,24GRE,GREASETHEWHEELS
Appreciate any or all help.
Thanks
You can use this substitute command in vi:
:1,$s/ *,/,/g
:1,$s/ *$//
:1,$s/,/","/g
First we replace trailing spaces then replace all spaces followed by , to a single ,. Finally we match each field that is not a comma and quote them.
[[:blank:]] will match a space or tab.
For your input it gives:
"44","1577","23GRE","GREASE THE ENGINE"
"44","1577","23GRE","GREASE THE ENGINE"
"44","1577","24GRE","GREASE THE WHEELS"
Another possibility:
%s/\s\+\([,$]\)/"\1"/g
%s/^/"/g
%s/$/"/g
Remove any sequence of white space characters (white space, tab, etc.) before a comma or the end of line;
Add " at the beginning of every line
Add " at the end of every line
If you had an empty line at the end of the file, you will end up with a line of "" which is easy to delete.
I have a csv file with semicolons separator and I need to remove all the line breaks after any character but ; and ".
I have succeeded in finding positions but removing line breaks doesn't seem to work.
What I have:
100138;"Some data";"AB";"My text goes here";
100139;"Some data 2";"CH";"My text goes here";
100140;"Some data 3";"CH";"My text goes here
And it has new line here
But it is still part of quoted data
and ends here";
100141;"Some data 4";"CH";"Another nice text without semicolon"enter
What I need:
100138;"Some data";"AB";"My text goes here";
100139;"Some data 2";"CH";"My text goes here";
100140;"Some data 3";"CH";"My text goes here And it has new line here But it is still part of quoted data and ends here";
100141;"Some data 4";"CH";"Another nice text without semicolon"enter
I used (?<=[^("|;)])$ to find it but \n doesn't seem to change anything.
I use notepad++ for that.
$(?<=[^;])(?<=[^"])\R
$ Find end of line
(?<=[^;]) Must not end with ;
(?<=[^"]) Must not end with "
\R Match linebreak character(s)
Have a try with:
Find what: (?<![;"])\R
Replace with: NOTHING
This will replace all linebreaks that aren't preceeded by ; nor ".
I have a document that has a range of numbers like this:
0300010000000394001001,27
0300010000000394001002,0
0300010000000394002001,182
0300010000000394002002,51
0300010000000394003001,156
0300010000000394003002,40
I need to find the new line character and replace with a number of spaces depending on the string length.
If it has 24 characters like this - 0300010000000394001002,0 then I need to replace the new line character at the end with 5 blank spaces.
If it has 25 characters like this - 0300010000000394002002,51 then I need to replace the new line character at the end with 4 blank spaces and so on.
In my text editor I can use find and replace. I search for the line length by ^(.|\s){24}$ for 24 characters - but this will obviously replace the whole line and I only need to replace the new line character at the end.
I want to specify a new line character AFTER ^(.|\s){24}$. Is this possible?
It sounds like you need two things.
Multi-line Mode (See "Using ^ and $ as Start of Line and...")
Backreferencing
Most editors that support regex support these naturally, but you'll have to let us know what editor you're using for us to be specific. Without knowing what editor you're using, all I can say is that you want to do some combination of the following:
regex subst
----- -----
^(.{24})\n $1 <-- there are spaces here
^(.{24})^M \1 <-- there are spaces here
^(.{24})\s ^^^^^