简体   繁体   中英

Print RegEx matches using SED in bash

I have an XML file, the file is made up of one line.

What I am trying to do is extract the " finalNumber " attribute value from the file via Putty. Rather than having to download a copy and search using notepad++.

I've built up a regular expression that I've tested on an On-line Tool , and tried using it within a sed command to duplicate grep functionality . The command runs but doesn't return anything.

RegEx:

(?<=finalNumber=")(.*?)(?=")

sed Command (returns nothing, expected 28, see file extract):

sed -n '/(?<=finalNumber=")(.*?)(?=")/p' file.xml

File Extract:

...argo:finalizedDate="2012-02-09T00:00:00.000Z" argo:finalNumber="28" argo:revenueMonth=""...

I feel like I am close (i could be wrong), am I on the right lines or is there better way to achieve the output?

Nothing wrong with good old grep here.

grep -E -o 'finalNumber="[0-9]+"' file.xml | grep -E -o '[0-9]+'

Use -E for extended regular expressions, and -o to print only the matching part.

Though you already select an answer, here is a way you can do in pure sed:

sed -n 's/^.*finalNumber="\([[:digit:]]\+\)".*$/\1/p' <test

Output:

28

This replaces the entire line by the match number and print (because p will print the entire line so you have to replace the entire line)

这可能适合你(GNU sed):

sed -r 's/.*finalNumber="([^"]*)".*/\1/' file

sed does not support look-ahead assertions. Perl does, though:

perl -ne 'print $1 if /(?<=finalNumber=")(.*?)(?=")/'

As I understand, there is no need to use look-aheads here. Try this one

sed -n '/finalNumber="[[:digit:]]\+"/p'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM