4 ms·
why do you need `while read` part at all? this is the main slow down here. shell is extremely time consuming compared to sed/awk/whatever tool.
by avaika 9y ago
why do you need `while read` part at all? this is the main slow down here. shell is extremely time consuming compared to sed/awk/whatever tool.
- aargh_aargh 9y agoThe script is currently a filter - the input comes from stdin.
- sebcat 9y agoYou can redirect stdin to sed directly, e.g., cat << EOF | sed -e 's/A/X/;s/B/Y/;s/C/Z/;' A Brave Cow Was Walking Crows Were Airborne All Was Well EOF or, cat << EOF > myfile A Brave Cow Was Walking Crows Were Airborne All Was Well EOF sed -e 's/A/X/;s/B/Y/;s/C/Z/;' < myfile or (copy-pasted from shell for clarity), $ cat foo.sh #!/bin/sh sed -e 's/A/X/;s/B/Y/;s/C/Z/;' $ cat << EOF | ./foo.sh A Brave Cow EOF X Yrave Zow
- aargh_aargh 9y agoThank you, that helped. I had to get rid of '^' anchors (because it's not processing individual lines anymore) and I put sed -e '' < /dev/stdin into the script, but the solution is now much simpler and faster.
- sebcat 9y agosed works on stdin by default (see third example) so there shouldn't be a need to pipe from /dev/stdin, and line anchors should still work. $ cat myfile foo foo baz baz $ cat foo.sh #!/bin/sh sed 's/^foo/bar/;s/^baz/foobar/' $ ./foo.sh < myfile bar foo foobar baz Depending on your expression, you may want to use -E to get POSIX ERE syntax.