user7823016
user7823016

Reputation: 167

sed unintentionally wipes remainder of line

Attempting to locate a variety of similar incidents within a file.

<address DOMAIN='0x000' bus='PCOIP_USB0' slot='0x00' function '0x0' />
<address DOMAIN='0x000' bus='PCOIP_USB1' slot='0x00' function '0x0' />
<address DOMAIN='0x000' bus='PCOIP_USB7' slot='0x00' function '0x0' />
<address type='bmu' domain='0x000' bus='PCOIP_USB9' slot='0x00' function '0x0' />
<address DOMAIN='0x000' bus='PCOIP_USB4' slot='0x00' function '0x0' />

I'm attempting to simply locate any instance beginning with PCOIP_ and replace that string with 0x86

The result will look like:

<address DOMAIN='0x000' bus='0x86' slot='0x00' function '0x0' />
<address DOMAIN='0x000' bus='0x86' slot='0x00' function '0x0' />
<address DOMAIN='0x000' bus='0x86' slot='0x00' function '0x0' />
<address type='bmu' domain='0x000' bus='0x86' slot='0x00' function '0x0' />
<address DOMAIN='0x000' bus='0x86' slot='0x00' function '0x0' />

The command I'm trying currently which handles replacing the necessary string, except it also wipes the remainder of that entire line:

sed s/PCOIP_\(.*\)/0x86/g' file

The command as is makes the file look like:

<address DOMAIN='0x000' bus='0x86
<address DOMAIN='0x000' bus='0x86
<address DOMAIN='0x000' bus='0x86
<address type='bmu' domain='0x000' bus='0x86
<address DOMAIN='0x000' bus='0x86

Upvotes: 1

Views: 43

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627607

You can use

sed "s/PCOIP_[^']*/0x86/g" file

Here, PCOIP_[^']* matches PCOIP_ and then any zero or more chars other than a ' char. The whole match is replaced with 0x86 substring.

See an online demo:

#!/bin/bash
s="<address DOMAIN='0x000' bus='PCOIP_USB0' slot='0x00' function '0x0' />
<address DOMAIN='0x000' bus='PCOIP_USB1' slot='0x00' function '0x0' />
<address DOMAIN='0x000' bus='PCOIP_USB7' slot='0x00' function '0x0' />
<address type='bmu' domain='0x000' bus='PCOIP_USB9' slot='0x00' function '0x0' />
<address DOMAIN='0x000' bus='PCOIP_USB4' slot='0x00' function '0x0' />"
sed "s/PCOIP_[^']*/0x86/g" <<< "$s"

Output:

<address DOMAIN='0x000' bus='0x86' slot='0x00' function '0x0' />
<address DOMAIN='0x000' bus='0x86' slot='0x00' function '0x0' />
<address DOMAIN='0x000' bus='0x86' slot='0x00' function '0x0' />
<address type='bmu' domain='0x000' bus='0x86' slot='0x00' function '0x0' />
<address DOMAIN='0x000' bus='0x86' slot='0x00' function '0x0' />

Upvotes: 3

Related Questions