4 ms·
And if you have more processing in the loop, you can pipe that into one sed call: for f in *.txt; do thing $f | sed; done becomes for f in *.txt; do
by faho 4y ago
And if you have more processing in the loop, you can pipe that into one sed call:
for f in *.txt; do thing $f | sed; done
becomes
for f in *.txt; do thing $f; done | sed
which will start `sed` once instead of once per file in the loop.
However, depending on your shell and the exact loop you use this might cause it to be run in a subshell, which means you no longer get to influence the state outside in form of variables and e.g. jobs to wait for (ran into that once, had a `thing &` in the loop and a `wait` outside and was super confused that it couldn't wait). See https://mywiki.wooledge.org/BashFAQ/024 https://mywiki.wooledge.org/BashFAQ/024.
And if `thing` takes a list of files, you can do `thing *.txt | sed` to start `thing` only once.
Also you can often use the shell's string replacement - `echo ${foo/bar/baz}` instead of `echo $foo | sed s/bar/baz`.