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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With