3 ms·
Some examples would be nice. E.g. instead of for f in *.txt; do sed 's/i/n/g' "$f" >"$f".tmp && cat "$f".tmp >"$f"; done you can sed -i.tmp 's/i/n/g'
by unhammer 4y ago
Some examples would be nice. E.g. instead of
for f in *.txt; do sed 's/i/n/g' "$f" >"$f".tmp && cat "$f".tmp >"$f"; done
you can
sed -i.tmp 's/i/n/g' *.txt
Instead of running awk multiple times, awk can output to files given in input (or from variables etc.), e.g. if the filename you want to print to is in the third column then `{print $1,$2 >>$3}` puts cols 1 and 2 in that file
- faho 4y agoAnd 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`.
- 6LLvveMx2koXfwn 4y ago> for f in *.txt; do sed 's/i/n/g' "$f" >"$f".tmp && cat "$f".tmp >"$f"; done $f = $f.tmp in this case.
- amelius 4y agoOr use python and do the entire thing in a single process.