delete delimiter

Hi All,

I wanted to replace | to space from 3rd occurence onwards:
I Used below:

echo "a|b|c|d|e" |sed 's/\|/ /3/g'
Got output:
a|b|c d|e
expecting Output:
a|b|c d e
echo "a|b|c|d|e" |sed 's/|/ /3g'

--ahamed

Ahamed,

getting output as below
a b c d e

expecting Output:
a|b|c d e

I am getting it correctly

root@bt:/tmp# echo "a|b|c|d|e" |sed 's/|/ /3g'
a|b|c d e

Please check if you missed the 3

--ahamed

echo "a|b|c|d|e" |sed 's/|/ /3g'
a b c d e

this is in HP-UX

Using awk... ugly though!

echo "a|b|c|d|e" | awk -F"|" '{ for(i=1;i<=NF;i++){_1=(i<3)?"|":OFS; printf $i _1} }'

--ahamed

---------- Post updated at 12:09 PM ---------- Previous update was at 12:03 PM ----------

thats weird! :eek:

--ahamed

Thanks Ahamed.
Awk command is working.

It's not weird; that's what happens when you depend on implementation-specific behavior. Three different implementations yield three different results:

# The OP's HP-UX sed:
echo "a|b|c|d|e" |sed 's/|/ /3g'
a b c d e

# An old OSX/FreeBSD sed
echo "a|b|c|d|e" |sed 's/|/ /3g'
a|b|c d|e

# An old GNU sed
echo "a|b|c|d|e" |sed 's/|/ /3g'
a|b|c d e

The POSIX standard states that combining the global flag with a numeric flag yields undefined behavior. If you want your sed script to be portable to other sed implementations, do not do that. Otherwise, you're probably restricted to running it on your specific sed (although it's possible that there may be other implementations which behave similarly).

Regards,
Alister

1 Like