Use sed (or awk) to replace first two slashes on every line
I have a text file, save from an Asterisk db, that looks like so:
/general/astbin : /asterisk/bin
/general/astlang : /var/lib/asterisk/sounds/za
/general/cdrdays 开发者_StackOverflow : 7
/general/cellc1 : _084[02-9]XXXXXX
/general/cellc2 : _0841.
I want to only strip the first two forward slashes of each line and replace it with spaces, but can figure it out.
Doing sed -i 's/\// /g'
will remove ALL forward slashes, which I don't want.
Anyone have some thoughts?
Quick trick:
sed -i -e 's|/| |' -e 's|/| |' inputfile
Below you have the solution doing it in one go, as opposed to other solutions (*):
sed -i 's,/\([^/]*\)/, \1 ,' file
(*) except for Thaddee Tyl's one, which was sent just before mine, but unfortunately not working initially.
Why it is better to do it in one go?
Because it is more sane and just more efficient.
Here is some flawed analogy. If you look for two books by author Z in a library you haven't been before, you do not find first one, go out from the library, come in again and start looking for second one forgetting about previous attempt.
"Proof"
# use tmpfs to conduct test in memory, avoiding disk I/O overhead
$ mkdir stupid-sed-speed-test
$ sudo mount -t tmpfs tmpfs stupid-sed-speed-test/ -o size=1050M
$ cd stupid-sed-speed-test/
# create 512 MB + "//\n" at the end
$ awk 'BEGIN{printf"%*s",512*1024**2,"";print"//";exit}' >file
# "forgetting" solution
$ time sed -e 's|/| |' -e 's|/| |' file >/dev/null
real 0m4.817s
user 0m3.388s
sys 0m1.424s
$ time sed -i -e 's|/| |' -e 's|/| |' file
real 0m5.450s
user 0m3.360s
sys 0m2.076s
# recreate 512 MB + "//\n" at the end
$ awk 'BEGIN{printf"%*s",512*1024**2,"";print"//";exit}' >file
# "one go" solution
$ time sed 's,/\([^/]*\)/, \1 ,' file >/dev/null
real 0m3.548s
user 0m2.080s
sys 0m1.464s
$ time sed -i 's,/\([^/]*\)/, \1 ,' file
real 0m4.155s
user 0m2.068s
sys 0m2.080s
(my hardware specs for those curious about them: Desktop)
Difference is surely not a mind-blowing one, but 25% is sometimes a lot.
This should work:
sed -e 's/\// /' -e 's/\// /'
This ought to work:
sed -i 's/^\/\([^\/]*\)\/ /\1 /'
精彩评论