Why is sed not recognizing \t as a tab?

RegexBashSed

Regex Problem Overview


sed "s/\(.*\)/\t\1/" $filename > $sedTmpFile && mv $sedTmpFile $filename

I am expecting this sed script to insert a tab in front of every line in $filename however it is not. For some reason it is inserting a t instead.

Regex Solutions


Solution 1 - Regex

Not all versions of sed understand \t. Just insert a literal tab instead (press Ctrl-V then Tab).

Solution 2 - Regex

Using Bash you may insert a TAB character programmatically like so:

TAB=$'\t' 
echo 'line' | sed "s/.*/${TAB}&/g" 
echo 'line' | sed 's/.*/'"${TAB}"'&/g'   # use of Bash string concatenation

Solution 3 - Regex

@sedit was on the right path, but it's a bit awkward to define a variable.

Solution (bash specific)

The way to do this in bash is to put a dollar sign in front of your single quoted string.

$ echo -e '1\n2\n3'
1
2
3

$ echo -e '1\n2\n3' | sed 's/.*/\t&/g'
t1
t2
t3

$ echo -e '1\n2\n3' | sed $'s/.*/\t&/g'
	1
	2
	3

If your string needs to include variable expansion, you can put quoted strings together like so:

$ timestamp=$(date +%s)
$ echo -e '1\n2\n3' | sed "s/.*/$timestamp"$'\t&/g'
1491237958	1
1491237958	2
1491237958	3

Explanation

In bash $'string' causes "ANSI-C expansion". And that is what most of us expect when we use things like \t, \r, \n, etc. From: https://www.gnu.org/software/bash/manual/html_node/ANSI_002dC-Quoting.html#ANSI_002dC-Quoting

> Words of the form $'string' are treated specially. The word expands > to string, with backslash-escaped characters replaced as specified by > the ANSI C standard. Backslash escape sequences, if present, are > decoded... > > The expanded result is single-quoted, as if the dollar sign had not > been present.

Solution (if you must avoid bash)

I personally think most efforts to avoid bash are silly because avoiding bashisms does NOT* make your code portable. (Your code will be less brittle if you shebang it to bash -eu than if you try to avoid bash and use sh [unless you are an absolute POSIX ninja].) But rather than have a religious argument about that, I'll just give you the BEST* answer.

$ echo -e '1\n2\n3' | sed "s/.*/$(printf '\t')&/g"
	1
	2
	3

* BEST answer? Yes, because one example of what most anti-bash shell scripters would do wrong in their code is use echo '\t' as in @robrecord's answer. That will work for GNU echo, but not BSD echo. That is explained by The Open Group at http://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html#tag_20_37_16 And this is an example of why trying to avoid bashisms usually fail.

Solution 4 - Regex

I've used something like this with a Bash shell on Ubuntu 12.04 (LTS):

To append a new line with tab,second when first is matched:

sed -i '/first/a \\t second' filename

To replace first with tab,second:

sed -i 's/first/\\t second/g' filename

Solution 5 - Regex

Use $(echo '\t'). You'll need quotes around the pattern.

Eg. To remove a tab:

sed "s/$(echo '\t')//"

Solution 6 - Regex

You don't need to use sed to do a substitution when in actual fact, you just want to insert a tab in front of the line. Substitution for this case is an expensive operation as compared to just printing it out, especially when you are working with big files. Its easier to read too as its not regex.

eg using awk

awk '{print "\t"$0}' $filename > temp && mv temp $filename

Solution 7 - Regex

I used this on Mac:

sed -i '' $'$i\\\n\\\thello\n' filename

Used this link for reference

Solution 8 - Regex

sed doesn't support \t, nor other escape sequences like \n for that matter. The only way I've found to do it was to actually insert the tab character in the script using sed.

That said, you may want to consider using Perl or Python. Here's a short Python script I wrote that I use for all stream regex'ing:

#!/usr/bin/env python
import sys
import re

def main(args):
  if len(args) < 2:
    print >> sys.stderr, 'Usage: <search-pattern> <replace-expr>'
    raise SystemExit

  p = re.compile(args[0], re.MULTILINE | re.DOTALL)
  s = sys.stdin.read()
  print p.sub(args[1], s),

if __name__ == '__main__':
  main(sys.argv[1:])

Solution 9 - Regex

Instead of BSD sed, i use perl:

ct@MBA45:~$ python -c "print('\t\t\thi')" |perl -0777pe "s/\t/ /g"
   hi

Solution 10 - Regex

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 https://perldoc.perl.org/perlre.html">Perl 5 documentation).

    perl -pe 's/(.*)/\t$1/' $filename > $sedTmpFile && mv $sedTmpFile $filename

  2. However, as pointed out by https://stackoverflow.com/users/131527/ghostdog74">ghostdog74</a>;, 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

Solution 11 - Regex

TAB=$(printf '\t')
sed "s/${TAB}//g" input_file

It works for me on Red Hat, which will remove tabs from the input file.

Solution 12 - Regex

If you know that certain characters are not used, you can translate "\t" into something else. cat my_file | tr "\t" "," | sed "s/(.*)/,\1/"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsixtyfootersdudeView Question on Stackoverflow
Solution 1 - RegexMark ByersView Answer on Stackoverflow
Solution 2 - RegexseditView Answer on Stackoverflow
Solution 3 - RegexBruno BronoskyView Answer on Stackoverflow
Solution 4 - RegexThomas BrattView Answer on Stackoverflow
Solution 5 - RegexrobrecordView Answer on Stackoverflow
Solution 6 - Regexghostdog74View Answer on Stackoverflow
Solution 7 - RegexRaj HassaniView Answer on Stackoverflow
Solution 8 - RegexRoman NurikView Answer on Stackoverflow
Solution 9 - RegexCees TimmermanView Answer on Stackoverflow
Solution 10 - RegexjustincbagleyView Answer on Stackoverflow
Solution 11 - RegexDayongView Answer on Stackoverflow
Solution 12 - RegexBill LutsView Answer on Stackoverflow