append data in a file by using tab delimiter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting append data in a file by using tab delimiter
# 1  
Old 07-30-2008
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
# 2  
Old 07-30-2008
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.

Code:
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:

Code:
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.)

Last edited by era; 07-30-2008 at 07:58 AM.. Reason: Note about echo -n
# 3  
Old 07-30-2008
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?
# 4  
Old 07-30-2008
How about this?

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

# 5  
Old 07-30-2008
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
# 6  
Old 07-30-2008
Quote:
Originally Posted by harim
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:

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

Regards
# 7  
Old 07-31-2008
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.)

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to loop file data based on delimiter

My file has data that looks like below: more data.txt I wish to display each string seperated by a delimiter : Expected output: I tried the below but I m not getting every split string on a new line. #!/bin/bash for i in `sed 's/:/\\n/g' data.txt`; do echo -n... (2 Replies)
Discussion started by: mohtashims
2 Replies

2. Shell Programming and Scripting

[Solved] Append an header to a tab delimited file

Dear All, I would like to find an automatic way to add a given code which belong to a class at the end of the column , for example this is my input file: 0610009O20Rik V$VMYB_01 310 (+) 1 0.971 v-Myb V$EVI1_04 782 (-) 0.763 0.834 Evi-1 V$ELK1_02 1966 (-) 1 0.984 Elk-1... (4 Replies)
Discussion started by: paolo.kunder
4 Replies

3. Shell Programming and Scripting

Compare two tab-delimiter files

Hi, I have two files like: file1 chr1 40 chr1 50 chr2 10 chr2 60 file2 chr1 30 chr1 50 chr2 15 chr2 20 and want to get the difference of column 2 when column 1 is the same in both files. (4 Replies)
Discussion started by: linseyr
4 Replies

4. UNIX for Dummies Questions & Answers

Making a Tab delimiter file to Comma

How can i make a tab delimiter file to a comma delimiter??? (13 Replies)
Discussion started by: saggiboy10
13 Replies

5. UNIX for Dummies Questions & Answers

add (append) a column in a tab delimited file

I have a file having the following entries: test1 test2 test3 11 22 33 22 44 66 99 99 44 --- I want to add a column so that the above file becomes: test1 test2 test3 notest 11 22 33 * 22 44 66 * 99 99 44 * --- Thanks (6 Replies)
Discussion started by: mary271
6 Replies

6. Shell Programming and Scripting

creating delimiter file & append with cron

I have the following script working fine, and need to generate a file delimiter (with tab or special character) for Excel data import. The script will run every hour in crontab to append the new rows to the delimiter, so that I can collect the data for i.e. a week, which will give me a lot of... (0 Replies)
Discussion started by: Daniel Gate
0 Replies

7. UNIX for Dummies Questions & Answers

Delimiter: Tab or Space?

Hello, Is there a direct command to check if the delimiter in your file is a tab or a space? And how can they be converted from one to another. Thanks, G (4 Replies)
Discussion started by: Gussifinknottle
4 Replies

8. UNIX for Advanced & Expert Users

how to search delimiter tab in a line and replace it

hi every one plz help me i want to search for a line contains tabspace This is a line The should be changed see the above line is seperated with tab space i want to replace that tab space in to # as This is a line#The should be changed i have tried with... (4 Replies)
Discussion started by: kkraja
4 Replies

9. Shell Programming and Scripting

Cutting a tab delimiter file

I have a 30 column tab delimited record file. I need to extract the first 10column. The following command to cut was not working cut -f 1-10 -d "\t" filename. Could any one keep on this . Thanks in Advance (4 Replies)
Discussion started by: vinod.thayil
4 Replies

10. Shell Programming and Scripting

replace delimiter : with '\001' in unix data file

HI can any one tell me how to replace a delimiter : with another delimiter '\001' it is a non printable octal character. thanks in adv spandu (4 Replies)
Discussion started by: spandu
4 Replies
Login or Register to Ask a Question