1 |
Sebastian Werner wrote: [Tue Oct 09 2001, 01:27:53AM EST] |
2 |
> i need a solution in a script to replace all backslashes (\). I know that i |
3 |
> how i replace slashes (/) (sed s/"\/"/"someelse"/g) but how i replace |
4 |
> backslashes. |
5 |
|
6 |
sed 's/\\/replacement/' |
7 |
|
8 |
Note that if you're replacing in a shell variable, you might consider |
9 |
using the bash replacement syntax instead of calling out to sed. |
10 |
|
11 |
Usage: |
12 |
|
13 |
VAR="${VAR#patt}" # snip shortest match from front |
14 |
|
15 |
VAR="${VAR##path}" # snip longest match from front |
16 |
|
17 |
VAR="${VAR%patt}" # snip shortest match from end |
18 |
|
19 |
VAR="${VAR%%path}" # snip longest match from end |
20 |
|
21 |
VAR="${VAR/patt/repl}" # replace first occurance |
22 |
|
23 |
VAR="${VAR//patt/repl}" # replace all occurances |
24 |
|
25 |
VAR="${VAR/#patt/repl}" # replace, rooted to beginning (^ in regex) |
26 |
|
27 |
VAR="${VAR/%patt/repl}" # replace, rooted to end ($ in regex) |
28 |
|
29 |
Examples (not necessarily practical): |
30 |
|
31 |
progname="${0##*/}" # snip entire path from program name |
32 |
|
33 |
newvar="${oldvar//\\\\/backslash}" # note that \\\\ matches one backslash! |
34 |
|
35 |
set -- "${@/#/$D/}" # prepend $D/ to all args |
36 |
|
37 |
newarray=("${@/#/$D}") # same as last example, but put in newarray |
38 |
|
39 |
Aron |