Matching dashes and newlines with Lua pattern - string

I am trying to split this file with the delimiter of 2 line breaks, 4 dashes in a row (----) and another 2 line breaks. Basically \n\n----\n\n.
The pattern I'm tossing into gmatch is "([^\n\n----\n\n]+)" but for some reason it's also matching instances of ---- that don't have the line breaks around it.

You need to escape - with %-.

Related

How to Delete lines that contain more than three dots in a email?

How do we delete the lines which have more than three dots in an email address?
How do we do this with sed command?
Eg:
fgtc21_pk#yahoo.com
bhchemitex#chemitex.com
hjcindysun48#hotmail.com
hhconsult_sa.jan.2020#yahoo.com
s.ing.song.lan.g.ju.n.55.l#gmail.com
VB.t.o.t.all.y.f.it.s.19.99#gmail.com
a.lice.i.n.wonde.r.lnd.2.1.f#gmail.com
I want the following lines with multiple Dots (3 or more dots )deleted from the file.
s.ing.song.lan.g.ju.n.55.l#gmail.com
VB.t.o.t.all.y.f.it.s.19.99#gmail.com
a.lice.i.n.wonde.r.lnd.2.1.f#gmail.com
sed '/^\(.*\..*\)\{3,\}#/d'
Match from the beginning of the line zero or more any characters, then a dot, then zero or more any characters, 3 or more times, then match a # sign. Delete matching lines.

How to change a specific colum content strings using bash/shell?

I'm having a .txt file looking like this (along about 400 rows):
lettuceFMnode_1240 J_C7R5_99354_KNKSR3_Oligomycin 81.52
lettuceFMnode_3755 H_C1R3_99940_KNKSF2_Tubulysin 70
lettuceFMnode_17813 G_C4R5_80184_KNKS113774F_Tetronasin 79.57
lettuceFMnode_69469 J_C11R7_99276_KNKSF2_Nystatin 87.27
I want to edit the names in the entire 2nd column so that only the last part will stay (meaning delete anything before that, so in fact leaving what comes after the last _).
I looked into different solutions using a combination of cut and sed, but couldn't understand how the code should be built.
Would appreciate any tips and help!
Thank you!
Here's one way:
perl -pe 's/^\S+\s+\K\S+_//'
For every line of input (-p) we execute some code (-e ...).
The code performs a subtitution (s/PATTERN/REPLACEMENT/).
The pattern matches as follows:
^ beginning of string
\S+ 1 or more non-whitespace characters (the first column)
\s+ 1 or more whitespace characters (the space after the first column)
\K do not treat the text matched so far as part of the final match
\S+ 1 or more non-whitespace characters (the second column)
_ an underscore
Because + is greedy (it matches as many characters as possible), \S+_ will match everything up to the last _ in the second column.
Because we used \K, only the rest of the pattern (i.e. the part of the match that lies in the second column) gets replaced.
The replacement string is empty, so the match is effectively removed.
With sed:
sed 's/ [^ ]*_/ /' file
Replace first space followed by non-space characters ([^ ]*) followed by _ widh one space.

How to replace the white blanks with four in vim?

There are 5 white blanks in line 3,6 white blanks in line 4,7 white blanks in line 5.
I want to replace them with 4 white blanks,how to write the command?
It is not %3,7s/^\s*/ /g ,how to fix it?
it is a test
it is a test
it is a test
I want change it into:
it is a test
it is a test
it is a test
Based on your example, it sounds like you want to replace all sequences of whitespace at the beginning of lines with 4 spaces.
Please comment if I have misinterpreted your question. You can do this by looking for an arbitrary amount of whitespace at the beginning of line and replacing with four spaces.
%s/^\s\+/ /g
You typed :%3,7s/^\s*/ /g, but % is a range and 3,7 is another range, so it's wrong : you have to choose between them:
:%s ...
will replace in the whole file, or
:3,7s ...
will only replace in lines from 3 to 7.

How to remove all the empty new lines in a document with one command?

My document looks something like this:
Line number one
Line number two
Line number three
I want the whole document to look like this:
Line number one
Line number two
Line number three
In other words, to remove all the empty lines. How to accomplish this?
Try :g/^$/d, which will remove all blank lines. The g indicates global, the ^$ is a regular expression that basically means 'match lines that start and end with nothing in between', and the d means delete. You can mix and match as much as you need :)
Another space-related command that may come in handy if you have random whitespace is :%s/\s\+$//, which trims any trailing whitespace (as #Bernhard points out, the $ operator means that you have a max of one occurrence per line, so the g is unnecessary).
Per the update, possible that the lines already contain whitespace, in which case :g/^\s*$/d should work.
The command I use is
:v/./d
The v command matches the lines that do not match the given pattern.
It was inherited from ed.

Vim Multi-line search pattern

I have a substitute command that captures and displays submatch() values in the replacement string. But I have another line of information that I want to parse below this line. That line is always the first line after an empty line, though the number of lines TO that empty line varies. For example:
The first important line I want to capture is here
Stuff I don't want.
A few more lines of stuff I don't want...
Second line I want to capture.
This pattern repeats a hundred or so times in a document. I can substitute "The First Important Line" fine, but shouldn't that search pattern include a way to jump down to the first empty line and then pick up the next "Second line I want to capture." ?? I could then place the contents of that second line into submatch parenthesis and substitute them where needed (right?).
If so, I cannot discover the way to extend the first search pattern to capture the "Second line" Suggestions or correcting my approach would be greatly appreciated.
Someone has already dealt with a similar problem. Below I provide their solution and the detailed description.
/^\nF\d\_.\{-}\_^\n\zs.*/+
It means "Find a block of lines that start with F and a digit,
then scan forward to the next blank line and select the line after that."
Part of regex
Meaning
^\n
Matches the start of a line, followed by a newline - i.e a blank line
F\d
The next line starts with an F followed by a digit
\_.\{-}
\_. is like ., but also matches newline. \{-} matches the minimum number of the preceeding \_.. (If I were to use * instead of \{-}, it would match to near the end-of file.)
\_^\n
Matches a blank line. \_^ is like ^, but ^ only works at the start of a regular expression.
\zs
When the match is finished, set the start of match to this point. I use this because I don't want the preceding text to be highlighted.
.*
Matches the whole line.
The + after the regular expression tells Vim to put the cursor on the line after the selection.
I think I read about offsets, but I can't find the bit in the help that is relevant right now. As such, my other solution would be to record a macro to do what you want:
qa/[Your pattern]<CR>jddq
You could then execute this macro with #a and repeat with ##; or run it a lot of times (e.g., 999#a).

Resources