Extract a group of lines from a text file on Mac

53 views
Skip to first unread message

Ross Madia

unread,
Oct 25, 2023, 1:41:40 PM10/25/23
to BBEdit Talk

See picture attached.
I have a log file (text format) that has thousands of lines.
I'm looking to extract 4 lines where the word "ERROR" appears.
HELP???ExtractLines.jpg

Patrick Woolsey

unread,
Oct 25, 2023, 1:57:53 PM10/25/23
to bbe...@googlegroups.com
I would start with the Text -> Process Lines Containing... command. :-)


Regards,

Patrick Woolsey
==
Bare Bones Software, Inc. <https://www.barebones.com/>



> On Oct 25, 2023, at 13:40, Ross Madia <ro...@sienviro.com> wrote:
>
> See picture attached.
> I have a log file (text format) that has thousands of lines.
> I'm looking to extract 4 lines where the word "ERROR" appears.
> HELP???<ExtractLines.jpg>
>

Kaveh Bazargan

unread,
Oct 25, 2023, 3:02:07 PM10/25/23
to bbe...@googlegroups.com
Will this work?

Search:
(.+\"ERROR".+)\r(.+)\r(.+)\r(.+)
Replace:
\1\2\3\4

Then press extract. Should give you a new file with the result. I see you have commas on all the lines anyway, so just removing the return

--
This is the BBEdit Talk public discussion group. If you have a feature request or need technical support, please email "sup...@barebones.com" rather than posting here. Follow @bbedit on Twitter: <https://twitter.com/bbedit>
---
You received this message because you are subscribed to the Google Groups "BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bbedit+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bbedit/7f38d17e-35d2-41d9-b499-4ab28980e6e5n%40googlegroups.com.


--
Kaveh Bazargan PhD
Director
Accelerating the Communication of Research
  https://rivervalley.io/gigabyte-wins-the-alpsp-scholarly-publishing-innovation-award-using-river-valleys-publishing-technology/

Tim A

unread,
Oct 27, 2023, 11:09:49 PM10/27/23
to BBEdit Talk
Looking to learn something here. Kaveh suggests:
Search:  (.+\"ERROR".+)\r(.+)\r(.+)\r(.+)

Why is a single quote around ERROR escaped here? I find any of these search patterns work ...
\"ERROR"  as  above;
\"ERROR\"
"ERROR"

Kaveh Bazargan

unread,
Oct 28, 2023, 6:30:24 AM10/28/23
to bbe...@googlegroups.com
Hi Tim

You are right that in BBEdit both escaped and unescaped versions worked. I was sloppy and escaped just one. So nothing magical, just being inconsistent!!

But I believe that in other environments, e.g. other programming languages, you sometimes need to escape. I think sometime with \" and sometimes ""

Others will chime in as my knowledge is limited here... :-)

Regards
Kaveh

Neil Faiman

unread,
Oct 28, 2023, 10:05:57 AM10/28/23
to BBEdit Talk Mailing List
On Oct 28, 2023, at 6:29 AM, Kaveh Bazargan <ka...@rivervalley.io> wrote:

But I believe that in other environments, e.g. other programming languages, you sometimes need to escape. I think sometime with \" and sometimes ""


The problem is that 

  • Regular expressions themselves are a moderately complex language with a specific syntax, but
  • Regular expressions are often used in other languages, where they may also be subject to the syntax rules of those languages.

The only things that need to be “escaped” in the regular expression language are the regular expression special marker characters: parentheses, dots, plus signs, asterisks, question marks, brackets, backslashes, some letters …

When you use a regular expression in BBEdit, in a Find and Replace dialog or one of the special Text menu operations, you are just writing a regular expression, so only the regular expression language rules apply, and the only things that need to be escaped are the regular expression operator characters. (Single backslashes before other characters generally are just ignored, like the backslashes before the quote marks in this example; but best practice is probably to use only the backslashes that are required by the regular expression syntax.)

But, for example, when you use a regular expression in the Perl language, the regular expression (often) has slashes around it to show that it is a regular expression, so if there are slashes in the regular expression, they need to be escaped.

In some languages, a regular expression is just written as a a string literal, which means that it has to satisfy the language rules for a string literal. In particular, backslashes are special in string literals in the C-family languages, which means that any backslashes in a string literal have to be escaped, as well as any quote marks, which otherwise would mark the end of the string.

Thus, the regular expression (.+"ERROR".+)\r(.+)\r(.+)\r(.+) as a string literal in C would be "(.+\”ERROR\".+)\\r(.+)\\r(.+)\\r(.+)” where the red backslashes are needed so that the quotes around ERROR won’t look like the end of the string, and so that the character sequence \r is part of the regular expression (otherwise, the \r would be transformed into a carriage-return character by the C string literal parser, and the regular expression would contain return characters instead of  backslash-r sequences).

Regards,
Neil Faiman

Kaveh Bazargan

unread,
Oct 28, 2023, 10:16:50 AM10/28/23
to bbe...@googlegroups.com
Thank you for the detailed response Neil. I stand corrected on that detail. I normally use regex directly so no need to escape...

Of course regex is already a headache to read, and escaping backslashes etc makes it doubly so...

Regards
Kaveh

--
This is the BBEdit Talk public discussion group. If you have a feature request or need technical support, please email "sup...@barebones.com" rather than posting here. Follow @bbedit on Twitter: <https://twitter.com/bbedit>
---
You received this message because you are subscribed to the Google Groups "BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bbedit+un...@googlegroups.com.

Tim A

unread,
Oct 28, 2023, 4:43:07 PM10/28/23
to BBEdit Talk
There are often subtleties to patterns posted that baffle me. So nice to have a welcoming community that I can learn from.
So thanks indeed Neil and Kaveh.
Reply all
Reply to author
Forward
0 new messages