Issue
I want to remove ID attribute from a XML file as below. Tries some sed command but it not yielding correct result. need help in correcting my sed command
<State>
<Resident Id="100">
<Name>Sample Name</Name>
<PhoneNumber>1234567891</PhoneNumber>
<EmailAddress>[email protected]</EmailAddress>
<Address>
<StreetLine1>Street Line1</StreetLine1>
<City>City Name</City>
<StateCode>AE</StateCode>
<PostalCode>12345</PostalCode>
</Address>
</Resident>
<Resident Id="101">
<Name>Sample Name1</Name>
<PhoneNumber>1234567891</PhoneNumber>
<EmailAddress>[email protected]</EmailAddress>
<Address>
<StreetLine1>Current Address</StreetLine1>
<City>Los Angeles</City>
<StateCode>CA</StateCode>
<PostalCode>56666</PostalCode>
</Address>
</Resident>
<Resident Id="" />
</State>
Tried below sed commands but it not giving me correct result.
sed -i 's/^(ID=".*)".*/\1>/' Resident.xml
sed '/ ID.*/d' Resident.xml
Want xml like below. All id attributes removed from xml.
<State>
<Resident>
<Name>Sample Name</Name>
<PhoneNumber>1234567891</PhoneNumber>
<EmailAddress>[email protected]</EmailAddress>
<Address>
<StreetLine1>Street Line1</StreetLine1>
<City>City Name</City>
<StateCode>AE</StateCode>
<PostalCode>12345</PostalCode>
</Address>
</Resident>
<Resident>
<Name>Sample Name1</Name>
<PhoneNumber>1234567891</PhoneNumber>
<EmailAddress>[email protected]</EmailAddress>
<Address>
<StreetLine1>Current Address</StreetLine1>
<City>Los Angeles</City>
<StateCode>CA</StateCode>
<PostalCode>56666</PostalCode>
</Address>
</Resident>
<Resident />
</State>
Solution
As other writes, you should not use sed
, but if you have no other option.
sed 's/ Id=".*"//' file
<State>
<Resident>
<Name>Sample Name</Name>
<PhoneNumber>1234567891</PhoneNumber>
<EmailAddress>[email protected]</EmailAddress>
<Address>
<StreetLine1>Street Line1</StreetLine1>
<City>City Name</City>
<StateCode>AE</StateCode>
<PostalCode>12345</PostalCode>
</Address>
</Resident>
<Resident>
<Name>Sample Name1</Name>
<PhoneNumber>1234567891</PhoneNumber>
<EmailAddress>[email protected]</EmailAddress>
<Address>
<StreetLine1>Current Address</StreetLine1>
<City>Los Angeles</City>
<StateCode>CA</StateCode>
<PostalCode>56666</PostalCode>
</Address>
</Resident>
<Resident />
</State>
Answered By - Jotne