Reputation: 97
I have a file "fruit.xml" that looks like the below:
FRUIT="Apples"
FRUIT="Bananas"
FRUIT="Peaches"
I want to use a single SED line command to find all occurrences of NAME="
and I want strip the value between the ""
from all the matches found.
So the result should look like:
Apples
Bananas
Peaches
This is the command I am using:
sed 's/.*FRUIT="//' fruit.xml
The problem is that it leaves the last "
at the end of the value I need. eg: Apples"
.
Upvotes: 0
Views: 512
Reputation: 10039
assuming 1 occurence per value and with this format
sed 's/.*="//;s/".*$//' fruit.xml
Upvotes: 0
Reputation: 6712
Use a simple cut
command
cut -d '"' -f2 fruits.xml
Output:
Apples
Bananas
Peaches
Upvotes: 0
Reputation: 727
Try this, this replaces the line with the founded fruit in the quotes:
sed 's/.*FRUIT="\(.*\)"/\1/' test.xml
Upvotes: 0
Reputation: 174806
This would be enough if you want to keep the leading spaces,
sed 's/\bFRUIT="\([^"]*\)"/\1/' fruit.xml
OR
sed 's/\bFRUIT="\|"//g' fruit.xml
Upvotes: 1
Reputation: 290165
Just catch the group and print it back: catch everything from "
until another "
is found with the ()
(or \(...\)
if you don't use the -r
option). Then, print it back with \1
:
$ sed -r 's/.*FRUIT="([^"]*)"/\1/' file
Apples
Bananas
Peaches
You can also use field separators with awk
: tell awk
that your field separators are either FRUIT="
or "
. This way, the desired content becomes the 2nd field.
$ awk -FS='FRUIT="|"' '{print $2}' file
Apples
Bananas
Peaches
To make your command work, just strip the "
at the end of the line:
$ sed -e 's/.*FRUIT="//' -e 's/"$//' file
^^ ^^^^^^^^^^^
| replace " in the end of line with nothing
-e to allow you use multiple commands
Upvotes: 5