FireHawk2300
FireHawk2300

Reputation: 97

sed remove part of all match occurrences

I have a file "names.xml" that looks like the below:

    NAME="James"
    NAME="Jack_DONE"
    NAME="John_DONE"
    NAME="Jimmy"

I want to use a single SED line command to find all occurrences of 'NAME="J' That has '_DONE' within the "" and I want to remove only the '_DONE' from all the matches found.

So the result should look like:

    NAME="James"
    NAME="Jack"
    NAME="John"
    NAME="Jimmy"

This is the command I am using:

sed 's/^[^#]\(.*\)NAME="J\(.*\)_DONE"/\1"NAME=J\2"/' names.xml

I am sure I can do this in a way more efficient way! Your time and assistance is much appreciated.

Upvotes: 2

Views: 268

Answers (4)

Jotne
Jotne

Reputation: 41460

Here is an awk version:

awk '/NAME="J/ {sub(/_DONE/,"")}1' names.xml
    NAME="James"
    NAME="Jack"
    NAME="John"
    NAME="Jimmy"

To write it back yo the file:

awk '/NAME="J/ {sub(/_DONE/,"")}1' names.xml > tmp && mv tmp names.xml

or if you have gnu awk >= 4.1

awk -i '/NAME="J/ {sub(/_DONE/,"")}1' names.xml

Upvotes: 0

anubhava
anubhava

Reputation: 785721

You can use:

sed -i.bak -r '/^#/!s/(NAME="J[^"]*)_DONE"/\1"/' file
NAME="James"
NAME="Jack"
NAME="John"
NAME="Jimmy"

Upvotes: 3

Cyrus
Cyrus

Reputation: 88819

With GNU sed: Only in lines that contain NAME="J search for _DONE and replace by nothing.

sed '/NAME="J/{s/_DONE//}' names.xml

Output:

    NAME="James"
    NAME="Jack"
    NAME="John"
    NAME="Jimmy"

Upvotes: 5

John Zwinck
John Zwinck

Reputation: 249462

Only a minor tweak is required to fix the position of the quotes and indentation:

sed 's/^\([^#].*\)NAME="J\(.*\)_DONE"/\1NAME="J\2"/' names.xml

Upvotes: 2

Related Questions