Replace a pattern in all lines but only before another pattern - vim

I have files like this:
something something other text MARKER bla bla something
something else something MARKER foo bar etc pp something
I want to replace something with another thing, but only before the MARKER. So the replace should yield:
another thing another thing other text MARKER bla bla something
another thing else another thing MARKER foo bar etc pp something
How do I do it in vim?

For example,
:%s/something\ze.*MARKER/another thing/g
See :h /\ze

Related

Move Cursor Immediately Before a Character on a Line in Vim

Say I have the following:
text function(contents) text
and I wanted it to be
text function() text
Placing the cursor right after the opening parenthesis, I thought the following command would work df); however, what I ended up with was the following
text function( text
So I would need someway to specify that I want the character just before the closing parenthesis, but I'm not sure how to do that. There may also be a better way to do this.
What would be the best way to go about this?
You were close! You need dt) as in delete till )
The f motion places the cursor on the ) (remember it like find)
As for the 'best' way to do it, there is at least a more general way: if the
cursor were somewhere in the middle of the ( and ) (or on one of them), you
can do di) (or equivalently di() to delete inside )
If you do da) (or equivalently da() to delete around ), you would
delete the stuff in between and including the brackets.
The same goes for di[, di{, di<, di', di" etc. Using these so-called
text objects, as opposed to the d{motion} way, has the advantage that you can
repeat the edit on other pairs of brackets/quotes without the cursor needing to
be in precisely the same place - it just needs to be on or in between them.
In the following you could position the cursor on e.g. the 'i' of 'initial' in
the first line, do di) to delete the words 'some initial text', then move the
cursor to the 'e' in 'more' in the second line and just do . to also delete
the words 'some more text'):
(some initial text)
(some more text)
This way also works when the brackets (or quotes) are on different lines. For
example, with the cursor somewhere between the {}, doing di} will change
this:
function( args ) {
body of function
}
to this:
function( args ) {
}

Matching trouble in UIMA RUTA?

I have a lot of text like this:
(((((WORD1 Some text tokenA)))))
(((((WORD2 Some text tokenA)))))
(((((WORD3 Some text tokenB)))))
and etc.
I need match only "WORDâ„–" blocks. I try some code like this:
ANY[5,5]{REGEXP("(") -> MARK(Begin)};
ANY[5,5]{REGEXP(")") -> MARK(End)};
Begin ANY+? {-> MARK(WordB)} tokenB;
but it marks all text from first Begin to first tokenA. How I can mark only WORDB ?
===========
I have a lot of text like this:
)))))WORD tokenA. A lot of text.
(((((
)))))WORD tokenB. A lot of text.
(((((
)))))WORD tokenC. A lot of text.
(((((
)))))WORD tokenA. A lot of text.
(((((
and etc, with a lot of different WORDs and a lot of different tokens. What I need? I need mark every WORD by its tag.
My code:
DECLARE Begin, End, tokenA, wordA;
ANY[5,5]{REGEXP(">") -> MARK(Begin)};
ANY[5,5]{REGEXP("<") -> MARK(End)};
W{REGEXP("tokena") -> MARK(tokenA)};
Begin ANY+? {-> MARK(wordA)} tokenA;
My bug:
http://postimg.org/image/9rudzlz7j/
==========================
Thank you, "and the ANY+? by a wildcard "#"" working like a charm for me!

What is different about these two pairs of strings that makes this sed script with one and not the other?

This question is related to this other question I asked earlier today:
Find and replace text with all-inclusive wild card
I have a text file like this
I want= to keep this
This is some <text> I want to keep <and "something" in tags that I" want to keep> aff FOO1 WebServices and some more "text" that" should "</be> </deleted>
<this is stuff in tags I want=to begone> and other text I want gone too. </this is stuff in tags I want to begone>
A novice programmer walked into a "BAR2" descript keepthis
and this even more text, let's keep it
<I actually want this>
and this= too.`
when I use sed -f script.sed file.txt to run this script:
# Check for "aff"
/\baff\b/ {
# Define a label "a"
:a
# If the line does not contain "desc"
/\bdesc\b/!{
# Get the next line of input and append
# it to the pattern buffer
N
# Branch back to label "a"
ba
}
# Replace everything between aff and desc
s/\(\baff\)\b.*\b\(desc\b\)/\1TEST DATA\2/
}
I get this as my output:
I want= to keep this
This is some <text> I want to keep <and "something" in tags that I" want to keep> aff FOO1 WebServices and some more "text" that" should "</be> </deleted>
<this is stuff in tags I want=to begone> and other text I want gone too. </this is stuff in tags I want to begone>
A novice programmer walked into a "BAR2" descript keepthis
and this even more text, let's keep it
<I actually want this>
and this= too.
However, by simply changing the search strings from aff and desc to FOO1 and BAR2:
# Check for "FOO1"
/\bFOO1\b/ {
# Define a label "a"
:a
# If the line does not contain "BAR2"
/\bBAR2\b/!{
# Get the next line of input and append
# it to the pattern buffer
N
# Branch back to label "a"
ba
}
# Replace everything between FOO1 and BAR2
s/\(\bFOO1\)\b.*\b\(BAR2\b\)/\1TEST DATA\2/
}
gives the expected output:
I want= to keep this
This is some <text> I want to keep <and "something" in tags that I" want to keep> aff FOO1TEST DATABAR2" descript keepthis
and this even more text, let's keep it
<I actually want this>
and this= too.`
I am completely stumped about what is going on here. Why should searching between FOO1 and BAR2 work differently from the exact same script with aff and desc?
The end marker should be \bdesc instead of \bdesc\b.
Note the \b in the pattern, it matches a word boundary. Your above text contains the word description, but not desc.
Your previous question made me assume that you want that. If you don't care about word boundaries, remove the \b escape sequences completely.

How to find and replace the first line word with vim

I have a file like this:
foo
bar
And I'm trying to get something like:
foo: foo,
bar: bar,
I've tried :%s/^\w/\0: \0,/g where ^\w matches the first word in line but I'm getting
f: f,foo
b: b,bar
Someone can explain to me what I'm doing wrong?
You are missing two characters (\+) which extend the match to the entire word, because \w only matches one character of a word and not the whole word.
The following should do what you want.
%s/^\w\+/\0: \0,/g
Instead of replacing you could (globally) copy, paste and append:
:g/\v^.+$/normal! yEPa: ^[A,
(where ^[ is escape, entered as <c-v><esc>)
or use (lower-case) e if that is the kind of "word" you want

Indesign script to change text in a paragraph

There's a particular paragraph in my document and I want to replace its content completely with something else programatically. Is there any function to do this? I need something like this:
app.activeDocument.stories[7].paragraphs[0]. = "blah blah blah";
where is the function that would allow you to replace the content of the paragraph in question to "blah blah blah." I bet this is possible but am not aware of the functions available.
The property you need to use is "contents".
myParagraph.contents = "Bla bla bla";

Resources