Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.
0:root@SERVER:/root # echo "something" | sed -e 's/^/\t/g'
tsomething
0:root@SERVER:/root # 

AIX/ksh .. why doesn't it works? I just want to put a "tab" before every line!

share|improve this question
    
What happens if instead of typing "\t" you press Ctrl-V then press Tab? –  manatwork Dec 11 '11 at 16:02

2 Answers 2

up vote 5 down vote accepted

\t on the right hand side of a sed expression is not portable. Here are some possible solutions:

POSIX shell

Bear in mind that since many shells store their strings internally as cstrings, if the input contains the null character (\0), it may cause the line to end prematurely.

echo "something" | while IFS= read -r line; do printf '\t%s\n' "$line"; done

awk

echo "something" | awk '{ print "\t" $0 }'

Perl

echo "something" | perl -pe 'print "\t"'
share|improve this answer
    
Is pointless to specify perl's -n switch here. -p implies the -n's effect. –  manatwork Dec 19 '11 at 10:19
    
@manatwork - Thanks, removed. –  Chris Down Dec 19 '11 at 10:26

If your shell is bash, ksh93 or zsh, you can use shell escaping to put a literal tab in the sed command. The quoting syntax $'…' treats every character inside the quotes literally except for backslashes and single quotes. You can use the usual backslash escapes inside these quotes.

sed -e $'s/^/\t/g'
share|improve this answer
    
pastebin.com/raw.php?i=aezVyqif –  LanceBaynes Dec 12 '11 at 17:27
    
@LanceBaynes Then I presume you have ksh88. –  Gilles Dec 12 '11 at 18:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.