Subham Tripathi
Subham Tripathi

Reputation: 2733

Replacing a string having double slash using sed or awk in linux

I wish to replace this

<property name="sourceUrl" value="file://D:/Uploaded Files?move=../../backup&amp;include=.*.ok"/>

with this

<property name="sourceUrl" value="file:///tmp/ETL-Files?move=../../backup&amp;include=.*.ok"/>

I am following an approach to first find the line number and then replace that line with the desired text, but that is not happening because of the < present at the start of the line.

 grep -n  ^<property name="sourceUrl" value= etl-job-context.xml

How can I solve this, or is there any better approach?

Upvotes: 0

Views: 208

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174706

You may use sed.

sed 's~\(<property *name="sourceUrl" *value="\)[^?]*~\1file:///tmp/ETL-Files~g' file

Basic sed uses the Basic Regular Expression engine for matching. In BRE \(..\) is called a capturing group. So this would capture all chars which exist from the property tag up to the value parameter. And the remaining chars up to ? were matched by [^?]*, which means to match any char but ?, zero or more times.

Sed uses some special chars as delimiters. Here I used ~ because the replacement string contains / slashes. \1 in the replacement refers to the chars which captured by group index 1. And the following chars will be appednded next to the captured chars. Note that the matched chars are automatically removed during replacement.

Upvotes: 1

Related Questions