2

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?

Shahbaz
  • 46,337
  • 19
  • 116
  • 182
Romonov
  • 8,145
  • 14
  • 43
  • 55

4 Answers4

3

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.

Prince Goulash
  • 15,295
  • 2
  • 46
  • 47
2

One way using sed:

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

EDIT:

If you wish to exclude the separators:

sed -n '/< sample1>/,/< \/sample1>/ {//!p}' file.txt
Steve
  • 51,466
  • 13
  • 89
  • 103
0

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.

Cheers.

José L. Patiño
  • 3,683
  • 2
  • 29
  • 28
0

ViM

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>$

grep

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.

Community
  • 1
  • 1
Shahbaz
  • 46,337
  • 19
  • 116
  • 182