1

I want to grab all occurrences in configuration file where first line starts from 'object' and immediately second line starts with 'nat'

object network obj_any

 nat (inside,outside) dynamic interface

object network obj-test

 nat (DMZ1,outside) static 10.206.49.180

object network obj-192.168.236.200

 nat (DMZ1,outside) static 10.206.74.60

object network obj-192.168.236.8

 nat (DMZ1,outside) static 10.206.49.183 tcp 8080 80

object network obj-192.168.236.9

 nat (DMZ1,outside) static 10.206.49.178 tcp 1002 22

object network obj-192.168.236.10

 nat (DMZ1,outside) static 10.206.49.178 tcp 8080 80

object network obj-192.168.236.13

 nat (DMZ1,outside) static 10.206.74.58 dns

I tried below but seems not working

object network .+? nat .+? static .+?

and selected 'match new line" but seems not matching

LukStorms
  • 28,916
  • 5
  • 31
  • 45
ghostrider
  • 11
  • 3
  • Try `^object.*\R\s*nat.*? static .*` – revo Jun 24 '18 at 21:15
  • It matches things for me with your current text, regex and settings. – Aaron Jun 24 '18 at 21:17
  • @revo thanks ! It is matching except the first statement. Also why you put '*' after \R\s (I mean to match what?) Also instead of new line matching, you are matching with \R (return carriage) – ghostrider Jun 24 '18 at 21:39
  • 1) First pair of lines doesn't have `static` in its second line. So it doesn't match. 2) To match leading spaces. You have some spaces at lines beginning with `nat` and a blank line in middle. 3) Carriage return is `\r`, not `\R`. – revo Jun 24 '18 at 21:41
  • Sorry for my ignorance. 1- What is \R doing? 2- How can I match dynamic line as well? 3- How to copy all matching lines into a new text file? Thanks for your help and time – ghostrider Jun 24 '18 at 21:47
  • 1) `\R` matches any kind of newline characters. 2) Remove `.*? static` 3) By copy / pasting? – revo Jun 24 '18 at 21:52
  • @revo how can I copy all matched in single go. I cannot find and copy. In original file, I have more than 100 matched and I need to copy – ghostrider Jun 26 '18 at 20:01
  • Please see this topic https://stackoverflow.com/questions/2298962/how-to-copy-marked-text-in-notepad – revo Jun 26 '18 at 20:04
  • @revo I used below method from your link, but it seems its only copying the first line (that is bookmarked not all search result). Use Mark under Search and enter the regex in Find What. Select Bookmark Line and click Mark All. Click Search -> Bookmark -> Copy Bookmarked Lines. Paste into a new document. Any help? – ghostrider Jun 26 '18 at 20:14

1 Answers1

0

I believe that this cannot be done in one step with Notepad++. A multi-step process to copy the lines is as follows.

(1) Find the wanted pairs of lines and merge them into single lines with a marker string. (2) Bookmark all lines with the marker and copy them. (3) Paste the wanted lines into a new buffer and convert the marker strings back to newlines.


In more detail.

(Setup) Choose a marker string, something that does not occur anywhere in the buffer being searched or in the destination buffer. For this example I choose !!!.

(1) Do a regular expression replace of ^(object.*)\R+( nat.*)$ with `\1!!!\2'. This converts the wanted lines so the first pair shown in the question become:

object network obj_any!!! nat (inside,outside) dynamic interface

(2) Open the search window and select the Mark tab. Click on Clear all marks, tick Bookmark line, enter the marker string (i.e. !!!) into the Find what field and click on Mark all. Select menu => Search => Bookmark => Copy bookmarked lines.

(3) Select the place where the copied lines should be written and Paste in the copied lines. Do a regular expression search and replace of !!! with \r\n\r\n. (May need to alter the replacement string if you preferred line endings are not Windows.)

Notes

The above does not preserve the exact sequence of CRs and LFs between the two lines. The first replace uses \R+ to find any combination of CRs and LFs between the two lines. The final replace inserts a fixed CR and LF sequence.

Rather than using the Copy bookmarked lines it may be suitable to use Remove unmarked lines which then leaves only the wanted lines in the buffer. The Copy and Paste command are then not needed and the final search and replace can be done in the initial buffer.

AdrianHHH
  • 13,492
  • 16
  • 50
  • 87