![]() The sed solution would be portable to all Unix-like systems. Note that if there are several instances of prefix on a line, the sed variation would return the string after the last one, while the GNU grep variation would return the string after the first one (which would include the other instances of prefix). The substitution will replace the whole line that matches the expression (which is a BRE), with the piece of it that occurs after the string prefix. How to grep string with regex in Perl Ask Question Asked 8 years, 4 months ago Modified 8 years, 4 months ago Viewed 6k times 2 I am new to Perl and I want write a simple script which will be getting the webpage content via LSW::Simple get () and then I want it to grep in the get () result for some regex match. ![]() With xmlstarlet it just becomes an XPath exercise: branch (xmlstarlet sel -t -v //blah1 name'andy'/branch file. AND the dot had to be escaped, AND case insensitive flag has to be placed as part of command params. 6 Answers Sorted by: 43 Use an XML parser for parsing XML data. What this does is to only print the lines that sed manages to apply the given substitution to. I see, the regex has to be set between quotation marks. If you are not using GNU grep, then you may use sed instead to get the bit between the string prefix and the end of the line: sed -n 's/.*prefix\(.*\)/\1/p' file Note that both -P and -o are non-standard extensions the POSIX specification of grep. grep understands three different versions of regular expression syntax: basic (BRE), extended (ERE), and Perl-compatible (PCRE). Again, with GNU grep (and some other grep implementations), you may use the -o option to get only the bit(s) that matches the given expression from each line. Regular expressions are constructed analogously to arithmetic expressions, by using various operators to combine smaller expressions. If you use GNU grep, you would be able to use Perl-like regular expressions if you used grep with the GNU grep-specific -P option.Īlso note that grep returns lines by default, not substrings from lines. See the manual for re_format or regex or whatever similar manual your grep manual refers to on your system, or the POSIX standard texts that I just linked to. ![]() These are basic regular expressions (BRE) and extended regular expressions (ERE, if grep is used with the -E option). What you are showing is a Perl-like regular expression (PCRE, "Perl Compatible Regular Expression"). Regular expressions come in many different flavours. This article uses the regular expression dialect that goes with the Linux grep command, with an extension to support more powerful features.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |