atul.mishra
atul.mishra

Reputation: 35

sed replace : delete a character between two strings while preserving a regex

I have a csv file which is delimited by #~#.

there is a field which contains 0 and then n(more than 1) number of '.'(dot).

I need to remove the zero and preserve the later dots. I have to also take care that floating numbers are not affected.

So effectively replace #~#0.....#~# to #~#.....#~# (dots can be from 1 to any)

Upvotes: 1

Views: 113

Answers (4)

anubhava
anubhava

Reputation: 785146

Using sed you can do:

s='#~#0.....#~#'
sed -r 's/(^|#~#)0(\.+($|#~#))/\1\2/g' <<< "$s"
#~#.....#~#

sed -r 's/(^|#~#)0(\.+($|#~#))/\1\2/g' <<< "#~#0.00#~#"
#~#0.00#~#

Upvotes: 1

karakfa
karakfa

Reputation: 67497

To limit the replacement with fields matching the pattern use this

$ echo "#~#0.12#~#0.....#~#0.1#~#0.#~#" | sed -r 's/#~#0(\.+)#~#/#~#\1#~#/g'

will preserve 0.12 and 0.1 but replace 0..... and 0.

#~#0.12#~#.....#~#0.1#~#.#~#

+ in regex means one or more. Anchoring with the field delimiters will make sure nothing else will be replaced.

Upvotes: 1

Walter A
Walter A

Reputation: 20002

Escape the dots ans include all characters that should match:

echo "#~#0.1234#~#0.....#~#" | sed 's/#~#0\.\./#~#../g'

Using var's will not improve much:

delim="#~#"
echo "#~#0.1234#~#0.....#~#" | sed "s/${delim}0\.\./${delim}../g"

Upvotes: 0

iamauser
iamauser

Reputation: 11479

]$ echo "#~#0.....#~#" | sed 's/#0/#/g'
#~#.....#~#

Upvotes: 0

Related Questions