I think others have clarified this adequately for other approaches (sed
, AWK
, etc.). However, my bash
-specific answers (tested on macOS High Sierra and CentOS 6/7) follow.
1) If OP wanted to use a search-and-replace method similar to what they originally proposed, then I would suggest using perl
for this, as follows. Notes: backslashes before parentheses for regex shouldn't be necessary, and this code line reflects how $1
is better to use than \1
with perl
substitution operator (e.g. per Perl 5 documentation).
perl -pe 's/(.*)/\t$1/' $filename > $sedTmpFile && mv $sedTmpFile $filename
2) However, as pointed out by ghostdog74, since the desired operation is actually to simply add a tab at the start of each line before changing the tmp file to the input/target file ($filename
), I would recommend perl
again but with the following modification(s):
perl -pe 's/^/\t/' $filename > $sedTmpFile && mv $sedTmpFile $filename
## OR
perl -pe $'s/^/\t/' $filename > $sedTmpFile && mv $sedTmpFile $filename
3) Of course, the tmp file is superfluous, so it's better to just do everything 'in place' (adding -i
flag) and simplify things to a more elegant one-liner with
perl -i -pe $'s/^/\t/' $filename