append data in a file by using tab delimiter

Hi,

I need to append the data in to a file by using tab delimiter.
eg:
echo "Data1" >> filename.txt
echo "\t" >> filename.txt (its not working)
echo "Data2" >> filename.txt.

the result sould be like this.
Data1 Data2

Different shells and different versions of echo work slightly differently, but a literal tab character should always work. At the prompt, you might need to type <ctrl-v> <tab> to get a literal tab.

Note that echo will add a newline (in some versions you can suppress that with the -n option, or with an escape code \c) so it might be better to simply put everything on one line.

echo "Data1	Data2" >>filename.txt

Or you could pick a unique punctuation character and change it to tab with tr -- again, different shells and different versions of tr might work slightly differently, but this works for me:

echo "Data1:Data2" | tr : '\010' >>filename.txt

(010 is the octal ASCII code for the tab character. Maybe your tr doesn't understand octal. Maybe your tr does understand '\t' instead. Experiment, and Google.)

its not working.
Is there any other way? or is there any other command to write the value into a file with the tab delimiter?

How about this?

printf "%s\t%s\n" "Data1" "Data2" >>filename.txt

Hi Sharmila,

Try the below command & let me know

echo "data1" >> filename.txt
echo -e "\t" >> filename.txt
echo "data2" >> filename.txt

In the echo command whatever u specify within quotes it treats that one to be literal
Use the -e option in case Escape Identifiers

This gives 3 lines instead of 1 line as the OP expected.

A possibility with echo:

echo -n "data1" >> filename.txt
echo -e -n "\t" >> filename.txt
echo "data2" >> filename.txt

Regards

But as noted earlier, there are dialects of echo and not all of them work like that. printf is at least somewhat more portable. Or to paraphrase the immortal words of the creator of Perl (Larry Wall: "It is easier to write a portable shell than to write a portable shell script"), use a portable language instead. (Hint: Perl.)

perl -le 'print "Data1", "\t", "Data2"' >>filename.txt