LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-05-2022, 04:28 PM   #1
dave-vinyard5
LQ Newbie
 
Registered: Sep 2022
Posts: 4

Rep: Reputation: 0
How can I substitute grep -P with perl equivalent in Freebsd"


Hi

The grep command in FreeBSD doesn't support the -P perl compatible switch.

In linux, grep -P is supported.

Code:
grep -oP '<span class="resultBall vMid ball">\K\d+(?=</span>)' t5.html
14
19
28
35
39
...etc
How can I do this in freebsd via perl? Thx

Last edited by dave-vinyard5; 10-05-2022 at 04:30 PM.
 
Old 10-05-2022, 05:29 PM   #2
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,616

Rep: Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555

What search engine terms did you use that didn't answer that question?

Putting "grep perl equivalent" into a search engine gives a number of results, including highlighting the built-in grep function.

 
Old 10-05-2022, 07:21 PM   #3
BudiKusasi
Member
 
Registered: Apr 2017
Distribution: Artix
Posts: 345

Rep: Reputation: 15
try find, install from FreeBSD repo pcregrep or its newer, pcre2grep
 
1 members found this post helpful.
Old 10-06-2022, 03:14 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,333
Blog Entries: 3

Rep: Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730
You can use regular Perl as a direct replacement for grep. Just follow perl's expressive syntax to do the same thing:
Code:
perl -n -e 'print if m|<span class="resultBall vMid ball">\K\d+(?=</span>)|;' t5.html
See "man perlre" and "man perlrun". That's just one example with Perl, there's more than one way to to it. However, if you are processing HTML or even well-formed XML, then regex is the wrong tool for the job, regardless of language.

You'll survive much better with a proper XML or HTML parser. There are several for Python and Perl or standalone ones like xmlstarlet:

Code:
xmstarlet sel -N xhtml="http://www.w3.org/1999/xhtml" -t -v '//xhtml:span[@class="resultBall"]' 2>/dev/null t5.html
It might seem a little complex at first, but it uses XPath like you would with some of the Python and Perl based XML / HTML parsers, and which are similar to what you have used in CSS already.
 
Old 10-06-2022, 03:44 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,961

Rep: Reputation: 7330Reputation: 7330Reputation: 7330Reputation: 7330Reputation: 7330Reputation: 7330Reputation: 7330Reputation: 7330Reputation: 7330Reputation: 7330Reputation: 7330
grep -P means: Interpret PATTERNS as Perl-compatible regular expressions (PCREs).
That means perl by default will know and recognize those patterns. So I don't really understand this question.
Additionally perl has a function named grep.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Grep and substitute? kristo5747 Linux - Newbie 1 07-19-2014 06:56 AM
Creating an alias in ksh that uses grep and includes 'grep -v grep' doug248 Linux - Newbie 2 08-05-2012 02:07 PM
Substitute for grep -o? MikeyCarter Linux - Software 4 03-08-2012 01:17 PM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM
Perl script: how to substitute through entire document? mister_math Programming 2 03-07-2003 11:38 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration