Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find information inside a xml tag using grep?

Tags:

I am working on a bash script to extract some information from a xml file. I'm using grep for this.

To find the information I need, I run:

grep -oP "<title>(.*)</title>" temp.xml

I get a list of matches and this includes the <title> tag.

How can I get a list containing only the text inside the title tag but without the title tag using grep?

like image 721
filype Avatar asked May 28 '12 08:05

filype


People also ask

How do I read an XML file in Linux?

xml file you want to view. If you're using Gnome, you can open GNOME Files and double-click the folder where the file is stored. If you're using KDE, the built-in file manager is Dolphin. Right-click the file and select Open With Other Application.

What is Flag in grep?

grep Flags. The four most commonly used flags to grep are -i (case-insensitive search), -l (list only the names of matching files), -w (which matches whole words only), and -v (invert; this lists only the lines that do not match the pattern). Another less well-known flag that is rather useful is -e.

What is grep in shell script?

Grep is a pattern matching command that we can use to search inside files and directories for specific text. Grep is commonly used with the output of one command, piped to be the input of the grep command.


1 Answers

Since you already use grep -P, why don't you use its features?

grep -oP '(?<=<title>).*?(?=</title>)'

In the general case, XPath is the correct solution, but for toy scenarios, yes Virginia, it can be done.

like image 120
tripleee Avatar answered Sep 20 '22 15:09

tripleee