Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 672
Matthew Warman Avatar asked Jan 23 '13 12:01

Matthew Warman


People also ask

Can I use regex with sed?

The sed command has longlist of supported operations that can be performed to ease the process of editing text files. It allows the users to apply the expressions that are usually used in programming languages; one of the core supported expressions is Regular Expression (regex).

Can you use sed in a bash script?

We can manipulate text in streams and files using sed from the command line in Bash and other command-line shells.

What is sed command in bash?

SED command in UNIX stands for stream editor and it can perform lots of functions on file like searching, find and replace, insertion or deletion. Though most common use of SED command in UNIX is for substitution or for find and replace.


2 Answers

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.

like image 155
Perleone Avatar answered Sep 20 '22 03:09

Perleone


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)

like image 27
SwiftMango Avatar answered Sep 20 '22 03:09

SwiftMango