Skip to main content
corrected explanation
Source Link
bleistift2
  • 195
  • 1
  • 7

Consider this sample file (line numbers are for reference only):

1 Reference duiarneutdigane uditraenturida enudtiar.
2
3 Reference uiae uiaetrtdnsu iatdne uiatrdenu diaren uidtae
4 on line 23.
5
6 uiae
7
8 uaiernd Reference uriadne udtiraeb unledut iaeru uilaedr
9 uiarnde line 234.

I was hoping to match every string beginning with “Reference” and ending with a period (i.e. ll. 1, 3–4, and 8–9) using this grep command (tst is the sample file):

grep -P '(?s)Reference.*?\.' tst

However, it only matches the first line. What I was thinking:

  • (?s), so . matches all characters, including newlines
  • .*? should make the star non-greedy, so it doesn’t match the whole file if it ends with a period.
  • .*? to match all characters, including newlines
  • The expression should end with a literal period \..

I’ve also tried awk and grep’s -z flag, but with both I get either every line or not all lines match my expressions.

Consider this sample file (line numbers are for reference only):

1 Reference duiarneutdigane uditraenturida enudtiar.
2
3 Reference uiae uiaetrtdnsu iatdne uiatrdenu diaren uidtae
4 on line 23.
5
6 uiae
7
8 uaiernd Reference uriadne udtiraeb unledut iaeru uilaedr
9 uiarnde line 234.

I was hoping to match every string beginning with “Reference” and ending with a period (i.e. ll. 1, 3–4, and 8–9) using this grep command (tst is the sample file):

grep -P '(?s)Reference.*?\.' tst

However, it only matches the first line. What I was thinking:

  • (?s) should make the star non-greedy, so it doesn’t match the whole file if it ends with a period.
  • .*? to match all characters, including newlines
  • The expression should end with a literal period \..

I’ve also tried awk and grep’s -z flag, but with both I get either every line or not all lines match my expressions.

Consider this sample file (line numbers are for reference only):

1 Reference duiarneutdigane uditraenturida enudtiar.
2
3 Reference uiae uiaetrtdnsu iatdne uiatrdenu diaren uidtae
4 on line 23.
5
6 uiae
7
8 uaiernd Reference uriadne udtiraeb unledut iaeru uilaedr
9 uiarnde line 234.

I was hoping to match every string beginning with “Reference” and ending with a period (i.e. ll. 1, 3–4, and 8–9) using this grep command (tst is the sample file):

grep -P '(?s)Reference.*?\.' tst

However, it only matches the first line. What I was thinking:

  • (?s), so . matches all characters, including newlines
  • .*? should make the star non-greedy, so it doesn’t match the whole file if it ends with a period.
  • The expression should end with a literal period \..

I’ve also tried awk and grep’s -z flag, but with both I get either every line or not all lines match my expressions.

Source Link
bleistift2
  • 195
  • 1
  • 7

Multiline grepping – What’s wrong with this expression?

Consider this sample file (line numbers are for reference only):

1 Reference duiarneutdigane uditraenturida enudtiar.
2
3 Reference uiae uiaetrtdnsu iatdne uiatrdenu diaren uidtae
4 on line 23.
5
6 uiae
7
8 uaiernd Reference uriadne udtiraeb unledut iaeru uilaedr
9 uiarnde line 234.

I was hoping to match every string beginning with “Reference” and ending with a period (i.e. ll. 1, 3–4, and 8–9) using this grep command (tst is the sample file):

grep -P '(?s)Reference.*?\.' tst

However, it only matches the first line. What I was thinking:

  • (?s) should make the star non-greedy, so it doesn’t match the whole file if it ends with a period.
  • .*? to match all characters, including newlines
  • The expression should end with a literal period \..

I’ve also tried awk and grep’s -z flag, but with both I get either every line or not all lines match my expressions.