How to remove the rest of the line after a specific string - string

So I am trying to remove the rest of the line after specific string on a document but I am finding it hard.
The lines look similar to this
abc:abc|bca:111 222|ccc:01/01/17
abc:abc|bca:bca bca|ccc:02/02/17
I am trying to delete everything after abc:abc including | but I don't know how.
I tried it with this on notepad+ \|.bca* but when I click Replace all it selects whole document.

Ctrl+H
Find what: \|.*$
Replace with: LEAVE EMPTY
check Wrap around
check Regular expression
DO NOT CHECK . matches newline
Replace all
Explanation:
\| : pipe character
.* : 0 or more any character but newline
$ : end of line
Result for given example:
abc:abc
abc:abc

Related

how to remove specific characters in vi or vim editor

I have some txt in vi:
|NC_004718|29751nt|SARS
|NC_045512|29903nt|Severe
|NC_004718|29751nt|SARS
|NC_045512|29903nt|Severe
|NC_004718|29751nt|SARS
now I want to replace remove everything after NC_004718, my expected output is:
NC_004718
NC_045512
NC_004718
NC_045512
NC_004718
How to do it? Thanks.
I would recommend using a substitution with regular expression to match the entire string and to capture what you would like to keep in parentheses. That way you can then replace the entire string with just the match.
:%s/^|\([^|]\+\)|.\+/\1/
To break down what is happening:
% means that you want to apply the command to each line within the file.
s means that you are doing substitution command (on each line). The s command has a syntax of s/<regular expression pattern>/<replacement>/<flags>
The regular eression pattern in the above command is ^|\([^|]\+\)|.\+.
^ means match from the line start.
| matches the character |.
\([^|]\+\) matches all characters except for the character |. Note that the real regular expression is actually ([^|]+), the additional \ characters are there because Vim needs to know that they are intended to be special characters for processing and not exact characters it needs to match. Also note that the parentheses are there to capture the match into a group (see below).
| again matches the actual character |.
.\+ matches all characters until the end of the line. Note that the . is considered special character by default but + still needs a preceding \.
The replacement text is only \1. This denotes that Vim should replace the text with whatever was captured in the first group (i.e. the first set of parentheses).
There are no flags with this command so there is nothing after the last /.
For example,
:g/NC_\d\+/normal! ygnV]p
:g/regex/ to match lines
normal! to execute Normal mode commands
ygn to yank the text previously matched by :g
V to select the whole line
]p or p to replace the line with the match
If you have only lines like those you have shown try:
:%norm xf|D

Search and delete to end of line in vim

I am trying to clean up some code and I am trying to find a good way of achieving the following:
I am a #decent
guy
and I want:
I am a guy
I tried using
:g/#/d
but the whole line gets deleted and I only want to delete until the end of line. What is the best way to achieve this in vim?
Thank you.
That won't because the usage of that command:
:[range]g/pattern/cmd
defaults to range being the whole line, and you are not doing any substitution anyway.
Use:
:%s/#.\+\n//g
instead.
# Matches a literal #.
.\+\n Matches everything until the end of line, and a new line.
// Replaces the entire match with nothing.
With :global you would want something like
:global/#/normal! f#D | join
or
:global/#/substitute/#.*// | join
Try this instead:
:s/ # .*\n/ /
Explanation:
You were using the wrong command, as they may look similar to new users.
:[range]g/VRE/act Globally apply the "act"ion (one letter command) to all lines (in range, default all file) matching the VRE (pattern)
:[range]s/VRE/repl/f Substitute within lines (in range, default current line) the matching VRE (pattern) with the "repl"acement using optional "f"lags
Now about the pattern, I think this candidate cover most cases (all but comments at the beginning of a line and comments without space after pound sign)
# litteral space, then hash tag, then space again
.* dot for any character, star to mean the previous may occur many times or even be absent
$ dollar at end to stay at "end of line", but \n to catch en EOL here
press d + shift 4 or d + $, which means delete to end of the line
d means delete
shift 4 or $ means cursor to end of the line

How to split words in line by comma, each with new line and matching indentation level? [duplicate]

I have this long regex string
(\.#.+|__init__\.py.*|\.wav|\.mp3|\.mo|\.DS_Store|\.\.svn|\.png|\.PNG|\.jpe?g|\.gif|\.elc|\.rbc|\.pyc|\.swp|\.psd|\.ai|\.pdf|\.mov|\.aep|\.dmg|\.zip|\.gz|\.so|\.shx|\.shp|\.wmf|\.JPG|\.jpg.mno|\.bmp|\.ico|\.exe|\.avi|\.docx?|\.xlsx?|\.pptx?|\.upart)$
and I would like to split it by | and have each component on a new line.
So something like this in the final form
(\.#.+|
__init__\.py.*|
\.wav|
\.mp3|
\.mo|
\.DS_Store|
... etc
I know I can probably do this as a macro, but I figured someone smarter can find a faster/easier way.
Any tips and helps are appreciated. Thanks!
Give this a try:
:s/|/|\r/g
The above will work on the current line.
To perform the substitution on the entire file, add a % before the s:
:%s/|/|\r/g
Breakdown:
: - enter command-line mode
% - operate on entire file
s - substitute
/ - separator used for substitute commands (doesn't have to be a /)
| - the pattern you want to replace
/ - another separator (has to be the same as the first one)
|\r - what we want to replace the substitution pattern with
/ - another separator
g - perform the substitution multiple times per line
Replace each instance of | with itself and a newline (\r):
:s/|/|\r/g
(ensure your cursor is on the line in question before executing)
actually you dont have to add |before the patter, try this s/,/,\r/g it will replace comma with comma following a line break.

searching for a pattern and placing it within another in vim

I have about 256 lines in a text file that look like /*0*/L"", I want to remove the last , and then put the remaining as a function argument code.append(/*0*/L""); I tried doing it with vim but I don't have much experience in it. how can we place something within something else in vi or vim?
:%s#\v(/\*0\*/L""),#code.append(\1);#
:%s : substitute all lines
# : alternative separator
\v : use very magic mode (see :h magic)
(/\*0\*/L""), : capture the regex, excluding the trailing comma
\1 : insert first captured group
this line would do the substitution on all lines in your buffer, only if the line ending with comma. No matter you had /*0*/L"", or /*123*/L"",
%s/\v(.*),$/code.append(\1)/
if you want to shrink the sub on certain pattern, change the .* part in the above cmd to fit your needs.

Simple way to replace all occurences of a string in a file in nodejs

Currently i'm using sed from shelljs. However, it only replaces the first occurrence, and has to be rerun to replace every occurrence in the file.
This is the line i'm using:
shell.sed '-i', '{mountPoint}', mountPoint, /tmp/somefile
Is there a way to get this working with shelljs, or some other simple way to perform search and replace?
Thanks
In your current SED command, you have the input and output in two different strings. Another way to arrange this allows you to put the match and replace patterns in the same string as seen below
s/{(.*)}/$1/g
It is broken down as follows
s/ #this is a search and replace
\{ #bracket is escaped since it means something special in REGEX language
( #keep what is inside the parenthesis for later (kept in $1)
.* #match anything
)
\} #other bracket you don't want to keep
/ #indicates you are now working on the replace pattern
$1 # what was captured in previous parenthesis
/ #end replace pattern
g #Global replace
have you tried using g in your replace pattern?
http://www.grymoire.com/Unix/Sed.html#uh-6

Resources