Vim, search in document header [closed] - search

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need a macro/function that searches the current document, starting at the top, for a specific pattern. Stopping at the first line that isn't commented out.
For example in this vim script:
"This is the first line
"This is the third line
if exists("did_something")
It would stop at if line, return true if it found the pattern and false otherwise.
All help appreciated!

Let's say your pattern is the letter 'e'.
To search for a line that is not commented out, but contains an e:
/^\%(\s*"\)\#!.*e
This uses a negative lookahead (\#!) to confirm that the start of the line (^) isn't followed by a the beginning of a comment (whitespace and then a double quote \s*") but is followed by an 'e' (.*e). The \%( and \) makes the enclosed pattern an atom that other operators (like negative lookahead) can operate on as a unit.
To run a command on matching lines, use :g
:g/^\%(\s*"\)\#!.*e/echo "found one"
To see if the current line matches, use match()
:echo match( getline(line('.')), '^\%(\s*"\)\#!.*e' ) >= 0 ? 'true' : 'false'
The regex is pretty much always the same, the question is what do you want to do with matching lines?

Related

How to remove set of matching characters at the end of the string in a shell scripts [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 months ago.
Improve this question
I want to remove set of matching characters at the end of the string in a shell script. It should work in all the linux flavours, ideally with out using tools like sed,awk.
I found some examples on web but all of them are about removing a single character type.
Below is a set of examples which shows what I am trying to achieve.
Please help.
1. Input : test_-
Output: test
2. Input: test-_-
Output: test
3. Input: test1__-
Output: test1
I want to remove the all the "hyphen" and "underscore" characters from the end of the string.
Since you are tagging this zsh:
Assuming that your string is stored in a variable input, you can do a
if [[ $input =~ ^((.*[^-_])) ]]
then
output=$MATCH
fi
The .* does a greedy match, which guarantees that the last character is neither a dash nor a hyphen.
In bash, this works similar, only that you have to set
output=${BASH_REMATCH[1]}
Supposing your data is in a file, like
test_-
test-_-
test1__-
with grep
grep -oP '[a-z]*[0-9]*' data.txt

Remove Unorder Number From Sublime [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
i Have Data Like this
9372127603
9372130412
9372140
9372175041
937218
9372190908
9372191764
i need output like
9372127603
9372130412
9372175041
9372190908
9372191764
what i do to achieve this in sublime text ?
You can do this via regex Find and Replace. Open Find → Replace…, make sure the regex option is on, enter ^\d{1,9}\n into the Find: field, and make sure the Replace: field is empty:
Explanation:
^ beginning of line
\d any digit
{1,9} match preceding between 1 and 9 times, inclusive
\n newline character
test at Regex101
Once you've done that, hit Replace All and any numbers less than 10 digits long will be removed:

How to convert from A to B with a single command in VIM [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I would like to get from:
0010|C|||N55555555|E0222222220010
to the following:
0010|C|||N55555555|E022222222|1.0
the last four digits represent numbers to one decimal place for example it could be:
0010|C|||N55514200|E0222222220300
which I would like the command to convert it to:
0010|C|||N55555555|E022222222|30.0
I would like a command to type that will convert a number of those strings in bulk. Any help would be appreciated!
You can use a sub-replace-expression (see :help sub-replace-\=):
:%s#\d\{4}$#\=printf('|%.1f', str2nr(submatch(0)) / 10.0)#
That is:
:%s#: execute this substitute for every line in the file (using # as the delimiter here as I need to use / in the \= part)
\d\{4}$: match the last four digits of the line
\=: interprete the following:
printf('|%.1f: print a | followed by a 1-precision float (the second argument)
str2nr(submatch(0)): transform the match 4-digit group into a decimal number (removes the trailing 0 to avoid reading 0XYZ as octal values)
/ 10.0: divide the value by 10
This transforms
0010|C|||N55555555|E0222222220010
0010|C|||N55514200|E0222222220300
0010|C|||N55514200|E0222222220301
0010|C|||N55514200|E0222222229301
into
0010|C|||N55555555|E022222222|1.0
0010|C|||N55514200|E022222222|30.0
0010|C|||N55514200|E022222222|30.1
0010|C|||N55514200|E022222222|930.1

Remove entry based on the value of first column [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
If 3549,2152,4701 in first column then remove the entry:
sample data:
18106|1.0.4.0/22
3549|1.0.10.0/24
5413|1.0.0.0/16
2152|1.4.0.0/16
3549|1.0.8.0/22
4701|1.0.0.0/8
Expedted output:
18106|1.0.4.0/22
5413|1.0.0.0/16
How to achieve this?
For your pattern to match only on the first field you have to anchor the expression to the start of the line:
grep -v -E '^(3549|2152|4701)\|'
The ^ marks the beginning of the line (and $ would mark the end of the line)
The -E activates enhanced regular expressions so you don't have to \ escape pipes and parentheses, and the -v inverses the search (returning only lines that do not match).
The ^ matches the start of the line then parentheses with the pipe symbol marks alternatives (3549, 2152 or 4701), and \| stands for the pipe symbol itself which your first field ends with, and needs to be escaped by the backslash so it's not treated as another alternation.
Be careful to use single quotes around it because otherwise the shell itself will interpret some of the special characters.

Vim search and move the searched string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
There is a similar question on moving the searched string to the end of the file or to the beginning of file
:g /<pattern>/m0 -> beginning of the file.
:g /<pattern>/m$ -> beginning of the file.
Is there way to search and move to the beginning of a line. Any tutorial link to learn this ?
Thank you
You can do movement inside the current line with :substitute alone, no need for :g. You need to augment the <pattern> so that the text before / after is also captured (in \(...\) groups). This lets you "rearrange" the matches in the replacement part.
Move (first, for last use .* instead of .\{-}) match to beginning:
:%s/^\(.\{-}\)\(<pattern>\)/\2\1/
Move to end:
:%s/\(<pattern>\)\(.*\)$/\2\1/
The syntax of the :g command allows you to put any Ex command where your examples have m0 and m$. The :move command (short form :m) can take any number as an argument, and there are other possibilities.
If you want to move a line after Line 17, then use :m17. If you want to move it before Line 17, then use :m16 or :m17- (short for :m17-1). I often find it useful to mark a line (ma in Normal mode or :mark a) and then
:g/<pattern>/m'a-
:help :global
:help :move
:help {address}

Resources