I have a source code file where I want to comment out (using single-line comments) certain blocks that have a specific opening and ending pattern. For example:
1
2
BEGIN
3
4
END
5
6
I want the output to be of the form:
1
2
//BEGIN
//3
//4
//END
5
6
I have tried to use sed, and I can match the whole block, but what I can't figure out is how to get each line of that match and perform another sed operation to replace the start of the line with the two forward slashes.
Edit: the BEGIN and END can also be on the same line.
Answers using any other tool are also welcome. Also, kindly provide an explanation for your code for beginners like me!
Related
I am attempting to remove .nc1 at the end of a line. I receive .nc1 in batches as a steel fabricator. We run into issues with our files where, line 5 in the example below, has an unnecessary .nc1 extension at the end. Problem I have, is that I cannot simply replace the value as it appears in line 2 as well.
In the example photo I have attached, I am looking to remove line 5 .nc1 extension and keep line 2 as is, .nc1 extension removal will be applied in a batch editing to all of my .nc1 files via find/replace.
ST
** BB233.nc1
F88
BB233
BB233.nc1
1000
A992
1
W21X201
Change to this
ST
** BB233.nc1
F88
BB233
BB233
1000
A992
1
W21X201
I was looking into Positive and/or Negative lookahead/lookbehind but didnt have much luck in making it work. I am a novice/lack thereof when it comes to using RegEx.
Match .nc1 only at the end of lines starting with whitespace, capturing the part you want to keep and putting it back, effectively deleting .nc1
Search: ^(\s+.*)\.nc1$
Replace: $1
I'd like to add a text of line after a specific line in Notepad++ I have found several similar answers through the web but I couldn't manage to adapt the code without recurring.
This is how my text file displayed in Notepad++
This is sample 1
This is sample 2
This is sample 3
This is sample 4
This is how I want it displayed after applying regex (Find third row, Insert the text without deleting any other line):
This is sample 1
This is sample 2
I insert the text here
This is sample 3
This is sample 4
The code should not search for any word to get a reference point except counting rows until find the specified one. In addition, it shouldn't repeat in the same text file like "every X row". I'd really wonder how it's done inside a batch file but a regex preferable for me.
Thanks in advance.
Just change {3} to {2}to match 2 lines.
Find what: (?:.+\R){2}\K
Replace with: Insert this sample\n
In order to merge the odd number line and the even number line in two methods.
One use command :s, the other use command :g and :s.
It's our homework and I could not get appropriate answer from the google.
And I had worked out the first one, which means I can solve it with command :s:
:%s/\(^.*$\)\n\(^.*$\)/\1 \2
And how could I use command :d and :s to solve it?
BEFORE:
1 aa
2 bb
3 abc
4 abc
5 an apple
6 is a bug
7 mazic
8 homework!
9 try a time
10 dodo
AFTER:
1 aa bb
2 abc abc
3 an apple is a bug
4 mazic homework!
5 try a time dodo
thanks to everyone and I have leant about how to solve it before the lesson.hah
:g/\(^.*$\)\n\(^.*$\)/s//\1 \2
What you can do here is :
Move the cursor to the line number to which you want to append the next line and then type below command in normal mode.
:s/\n/ /
Another way is go to the particular line and press SHIFT+V and then type below command:
:'<,'>s/\n/, /
Note that when you are in visual mode and press : then :'<,'> will automatically get typed. You just need to type regex ahead of that.
In both the above commands, g is not needed as it will not do any impact because only one \n will be there for each line.
You don't need to use :substitute here, there's a special command :join.
You can use the Ex command with :global, using ^ as the pattern to match all lines:
:global/^/join
Or use the shorter normal mode variant J:
:%normal! J
I'm trying to do a search within a visual block, (from a vimscript).
This is my code:
aaaaaaaaaaaa
a26 text tea
atext text a
atext 27 12a
a11 text 25a
aaaaaaaaaaaa
Let say my block selection is within the 'a' border and I want to search all numbers with 2 characters:
This is my search:
/\%V\d\{2}\%V
The problem with the 2nd \%V is that it shortened a visual block selection with 1 characters, it doesn't find the number 12 and 25 in my above example.
How can I extend the visual block selection (in a vimscript) with 1 character to the right, do the search and return to previous visual block selection?
OK, then I just write it as an answer, well this is an answer and question...:)
remove the 2nd \%V from your pattern would do this.
I posted first as comment, because.....
I personally have been using only single \%V in my work, and it worked fine. I saw this question, and checked the :h \%V, the help suggests using both... and it is zero-width. I don't really get why 2 \%Vs won't work for OP's question.
so, that is to say, I know how to fix the problem, but not 100% clear, what causes the problem, I hope others could explain a bit.
I've only just started playing with Regex and seem to be a little stuck! I have written a bulk find and replace using multiline in TextSoap. It is for cleaning up recipes that I have OCR'd and because there is Ingredients and Directions I cannot change a "1 " to become "1. " as this could rewrite "1 Tbsp" as "1. Tbsp".
I therefore did a check to see if the following two lines (possibly with extra rows) was the next sequential numbers using this code as the find:
^(1) (.*)\n?((\n))(^2 (.*)\n?(\n)^3 (.*)\n?(\n))
^(2) (.*)\n?((\n))(^3 (.*)\n?(\n)^4 (.*)\n?(\n))
^(3) (.*)\n?((\n))(^4 (.*)\n?(\n)^5 (.*)\n?(\n))
^(4) (.*)\n?((\n))(^5 (.*)\n?(\n)^6 (.*)\n?(\n))
^(5) (.*)\n?((\n))(^6 (.*)\n?(\n)^7 (.*)\n?(\n))
and the following as the replace for each of the above:
$1. $2 $3 $4$5
My Problem is that although it works as I wanted it to, it will never perform the task for the last three numbers...
An example of the text I want to clean up:
1 This is the first step in the list
2 Second lot if instructions to run through
3 Doing more of the recipe instruction
4 Half way through cooking up a storm
5 almost finished the recipe
6 Serve and eat
And what I want it to look like:
1. This is the first step in the list
2. Second lot if instructions to run through
3. Doing more of the recipe instruction
4. Half way through cooking up a storm
5. almost finished the recipe
6. Serve and eat
Is there a way to check the previous line or two above to run this backwards? I have looked at lookahead and lookbehind and I am somewhat confused at that point. Does anybody have a method to clean up my numbered list or help me with the regex I desire please?
dan1111 is right. You may run into trouble with similar looking data. But given the sample you provided, this should work:
^(\d+)\s+([^\r\n]+)(?:[\r\n]*) // search
$1. $2\r\n\r\n // replace
If you're not using Windows, remove the \rs from the replace string.
Explanation:
^ // beginning of the line
(\d+) // capture group 1. one or more digits
\s+ // any spaces after the digit. don't capture
([^\r\n]+) // capture group 2. all characters up to any EOL
(?:[\r\n]*) // consume additional EOL, but do not capture
Replace:
$1. // group 1 (the digit), then period and a space
$2 // group 2
\r\n\r\n // two EOLs, to create a blank line
// (remove both \r for Linux)
What about this?
1 Tbsp salt
2 Tsp sugar
3 Eggs
You have run into a major limitation of regexes: they don't work well when your data can't be strictly defined. You may intuitively know what are ingredients and what are steps, but it isn't easy to go from that to a reliable set of rules for an algorithm.
I suggest you instead think about an approach that is based on position within the file. A given cookbook usually formats all the recipes the same: such as, the ingredients come first, followed by the list of steps. This would probably be an easier way to tell the difference.