TheGeoEngineer
TheGeoEngineer

Reputation: 97

Using Sed to insert line of text with tab delimiter

I am trying to add a line of text to the top of a text file with tab delimiters.

sed -i '1s/^/x,y,z\n/' INPUTFILE.txt

^^This of course yields "x,y,z" in the top row of the input file.

sed -i '1s/^/x'\t'y'\t'z\n/' INPUTFILE.txt

^^This yields "xtytz" in the top row of the input file.

What is the correct syntax to add a tab between the entries x,y, and z?

Thanks!


Update:
Two suggestions ended up working here:

  1. using an actual tab as a "character" between x,y,and z

    sed -i '1s/^/x y z\n/' INPUTFILE.txt

  2. Using \t, but without the ' surrounding it... Just drop it in!

    sed -i '1s/^/x\ty\tz\n/' INPUTFILE.txt

Big thanks to all for input.

Upvotes: 2

Views: 5124

Answers (2)

damgad
damgad

Reputation: 1446

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.

Upvotes: 2

glenn jackman
glenn jackman

Reputation: 246867

This works with my sed (GNU sed 4.2.1): there's a newline immediately after the backslash

seq 5 | sed '1i\
a\tb\tc'
a   b   c
1
2
3
4
5

Upvotes: 2

Related Questions