What is the proper way to insert tab in sed?
You can simply use the sed
i
command correctly:
some_command | sed '1i\
text text2'
where, as I hope it is obvious, there is a tab between 'text' and 'text2'. On MacOS X (10.7.2), and therefore probably on other BSD-based platforms, I was able to use:
some_command | sed '1i\
text\ttext2'
and sed
translated the \t
into a tab.
If sed
won't interpret \t
and inserting tabs at the command line is a problem, create a shell script with an editor and run that script.
Using sed to insert TABs
I believe the problem is with competition between the way that the shell and sed are expanding the meta-characters. I've tried tripling the first backslash character and that seems to work for me:
sed -i "i \\\t\t\ttime.sleep(0.1) " tmp.tmp
How can I insert a tab character with sed on OS X?
Try: Ctrl+V and then press Tab.
Why is sed not recognizing \t as a tab?
Not all versions of sed
understand \t
. Just insert a literal tab instead (press Ctrl-V then Tab).
sed - insert line after match with a double tab for the beginning of inserted line
sed is for simple substitutions on individual lines, that is all. For anything even slightly more interesting just use awk instead for clarity, portability, efficiency and most other desirable attributes of software:
$ awk '{print} /loop:/{print "\t\"add %%g1, 1, %%g1\\n\\t\""}' file
"loop:\n\t"
"add %%g1, 1, %%g1\n\t"
"add %%g1, 1, %%g1\n\t"
The above just implements what you were trying to do with sed but there's probably a better approach depending on your real requirements, e.g. any of these has benefits over the above but produce the same output under different conditions:
$ awk '1;NR==2' file
"loop:\n\t"
"add %%g1, 1, %%g1\n\t"
"add %%g1, 1, %%g1\n\t"
$ awk '{print} f{print;f=0} /loop:/{f=1}' file
"loop:\n\t"
"add %%g1, 1, %%g1\n\t"
"add %%g1, 1, %%g1\n\t"
$ awk '{print} /loop:/{sub(/".*/,""); print $0 "\"add %%g1, 1, %%g1\\n\\t\""}' file
"loop:\n\t"
"add %%g1, 1, %%g1\n\t"
"add %%g1, 1, %%g1\n\t"
Based on the comment you left under a previous answer of mine it sounds like you do want one of those bottom so you don't have to specify the indentation.
Can't insert a new line and tab with sed
As some sed versions don't support \n
insertion, you can try with literal $'\n'
and $'\t'
character :
cat /etc/login.conf | sed -e 's|:umask=022:|:umask=022:\'$'\n\t\t:charset=UTF-8:|g'
Using Sed to insert line of text with tab delimiter
Your second try is almost correct. Just delete '
separating \t
(GNU sed 4.2.1)
sed -i '1s/^/x\ty\tz\n/' INPUTFILE.txt
However, answer suggested by glenn jackman seems to be more elegant.
Insert tab creating issues: Sed treats first tab as literal character and then as whitespace thereafter
Please try instead:
sed -i "/DocumentRoot\s\/data\/iga\/wwwroot/a\\\t\t\tServerName itWorked" /etc/apache2/sites-available/default-ssl.conf
or
sed -i '/DocumentRoot\s\/data\/iga\/wwwroot/a\\t\t\tServerName itWorked' /etc/apache2/sites-available/default-ssl.conf
# enclosed with single quotes
The a
command expects a backslash in front of the replacement text,
then you need to add extra backslashes.
Note that the \t
kind of notation in the replacement is supported byGNU sed
only. A standard sed
does not.
BASH - SED insert new line before last, containing tab and variable
You may use this sed
:
sed "\$i \\\\tnewline = $newvalue" file
Difference is escaping first $
when we use double quotes and use of \\\\t
instead of \\t
due to expansion of double quotes in shell.
Why won't the tab be inserted on the first added line?
With GNU sed:
sed '/^foo$/a \\tinsert1\n\tinsert2' file
<---- single quotes! --->
Produces:
foo
insert1
insert2
From the manual:
a \
text Append text, which has each embedded newline preceded by a backslash.
Since the text to be append itself has to to be preceded by a backslash, it needs to be \\t
at the beginning.
PS: If you need to use double quotes around the sed command because you want to inject shell variables, you need to escape the \ which precedes the text to be appended:
ins1="foo"
ins2="bar"
sed "/^foo\$/a \\\t${ins1}\n\t${ins2}" file
Related Topics
Search for a Cronjob with Crontab -L
Stripping Single and Double Quotes in a String Using Bash/Standard Linux Commands Only
How to Get the Contents of a Webpage in a Shell Variable
Convert All File Extensions to Lower-Case
How to Check the Version of Openmp on Linux
Adding a Directory to Path in Ubuntu
How to Add Timestamp While Redirecting Stdout to File in Bash
Whiptail: How to Redirect Output to Environment Variable
Read a Single Sector from a Disk
Vm/Min_Free_Kbytes - Why Keep Minimum Reserved Memory
Amazon Ec2 - Apache Server Restart Issue
Is It Possible That Linux File Descriptor 0 1 2 Not for Stdin, Stdout and Stderr
Exclude .Svn Directories from Grep
Shellscript to Monitor a Log File If Keyword Triggers Then Execute a Command
Fuzzy File Search in Linux Console