Take the 2-minute tour ×

I am on Unix using gvim and grep.

Problem 1:

I need to search an xml file for the following pattern:

< sample1>
< /sample1>

So the problem is the pattern crosses a line. I am new to gvim and grep and could'nt figure this out using my regex/special characters knowledge.

Problem 2:

One additional problem is, there is white space before the less than sign (<) in the second line. i.e.

< sample1>
 < /sample1>

Could anyone please suggest how I can search for these patterns?

share|improve this question
add comment

4 Answers

up vote 3 down vote accepted

To search for this pattern in Vim use the / command in normal mode:

/< sample>\n< \/sample1>

Notice that the / character on the second line has to be escaped using the \ character.

share|improve this answer
Thank you. This works. –  Romonov Aug 8 '12 at 15:13
add comment

One way using sed:

sed -n '/< sample1>/,/< \/sample1>/p' file.txt


If you wish to exclude the separators:

sed -n '/< sample1>/,/< \/sample1>/ {//!p}' file.txt
share|improve this answer
add comment

You can search for the < /sample1> pattern just typing in command mode:

/< \/sample1>

I don't understand the second problem, since does not matter if there is a white space. Just put a white space after the search slash:

/ < \/sample1>

I've just tested this in Vim and it worked. Hope to be useful for you.


share|improve this answer
add comment


You can use \n in ViM inside the search pattern.

Problem 1:

/<\ sample1>\n<\ \/sample1>

Problem 2:

/<\ sample1>\n\ <\ \/sample1>

If you want to be more precise and ignore cases like this:

blah blah < sample1>
 < /sample1> blah blah

You can explicitly ask to match the beginning and end of lines:

Problem 2:

/^<\ sample1>\n\ <\ \/sample1>$

If the amount of white space before the tags are not important, you can use \s*

/^\s*<\ sample1>\n\s*<\ \/sample1>$


If you are interested in using grep, I have bad news for you. grep doesn't do multiple lines. You can however look at this question and learn that pcregrep -M can solve your problem.

share|improve this answer
add comment

Your Answer


