sed search and replace with spaces

46 views Asked by At

I am trying to convert the following string

California_Zephyr_5, on tm to California_Zephyr_5, ON TIME

when I use the following statement this way

 # Search and replace
 webData=$(echo "$webData" | sed 's/on tm/ON TIME/')

I get California_Zephyr_5, on tm

If I try it with the individual abbreviations, it seems to work for those:

 webData=$(echo "$webData" | sed 's/on tm/ON TIME/')

results in California_Zephyr_5, on ON TIME

 webData=$(echo "$webData" | sed 's/on tm/ON TIME/')

results in California_Zephyr_5, ON TIME tm

so I assume the issue is with the space.

Thanks in advance for your help.

3

There are 3 answers

2
Cyrus On

There is one non-breaking space after on and before tm. See with GNU cat:

echo "$webData" | cat -A

Output:

California_Zephyr_5, onM-BM- tm$

or

echo "$webData" | hexdump -C

Output:

00000000  43 61 6c 69 66 6f 72 6e  69 61 5f 5a 65 70 68 79  |California_Zephy|
00000010  72 5f 35 2c 20 6f 6e c2  a0 74 6d 0a              |r_5, on..tm.|
0000001c

I suggest to use:

webData=$(echo "$webData" | sed 's/on\xc2\xa0tm/ON TIME/')
echo "$webData"

Output:

California_Zephyr_5, ON TIME
0
Ivan On

BTW you can do this even without sed, using just vars:

1.Variable substitution

$ webData='California_Zephyr_5, on tm'
$ echo ${webData/on*tm/ON TIME}
California_Zephyr_5, ON TIME

2.Using an array

$ arr=($webData)
$ echo ${arr[0]} ON TIME
California_Zephyr_5, ON TIME
0
Ricotto On

There is one non-breaking space after on and before tm as suggested above.

echo "$webData" | cat -A and echo "$webData" | hexdump -C demonstrated that.

I used:

webData=$(echo "$webData" | sed 's/on\xc2\xa0tm/ON TIME/')

Thanks for everyone's help.