I have a file with seq likes this


It has 3 lines. I want for each line each letter gets separated by tab and then want to store it in another file. How can i do it.

  • 2
    With such questions you should always give an example for the expected output. – Hauke Laging Dec 17 '14 at 22:41
  • It's a DNA sequence... How long is the strand? ie you should split tabs at the end of the chain. Add more info as Hauke states. – eyoung100 Dec 17 '14 at 23:05

If I have understood your intention correctly then this does it:

sed -e 's/./&\t/g' -e $'s/\t$//' file

The second replacement deletes the tab at the end of the line.

|improve this answer|||||

Try doing this using :

perl -ne 'print join "\t", split //' file > new_file
|improve this answer|||||
  • If you like golf: perl -ne '$,="\t";print split//' file > new_file – Joseph R. Dec 18 '14 at 5:00

In awk:

awk -F '' -vOFS='\t' '{$1=$1}1' file > new_file

Borrowed the idiom {$1=$1}1 from an answer to one of your other questions.

This sets the field separator to nothing (-F ''), which means that each record is read character-by-character. The output field separator is set to a TAB character (-vOFS='\t') and the idiom {$1=$1}1 is (as far as I can tell) a no-op that has the side effect of causing awk to insert the OFS between every two fields before printing them all.

|improve this answer|||||
  • pretty good. not everybody breaks it down by arguments. someday I'll learn how to use awk probably - and I'll need it broken down that way I expect. thanks. – mikeserv Dec 18 '14 at 4:52
  • 1
    @mikeserv Thanks. I have promised myself to learn proper awk one day, too :) – Joseph R. Dec 18 '14 at 4:53
  • I wonder if that {$1=$1}1 thing is anything like sed's s/.\{0,1\}/ /g? You know - matching the null field between the strings? That's pretty cool, anyway. – mikeserv Dec 18 '14 at 4:56
  • 1
    @mikeserv Probably not. As far as I can tell, $1=$1 doesn't do anything but tricks awk into thinking the first field has been re-assigned so that it knows to insert the OFS between adjacent fields. The 1 at the end is to simply return a "true" value and cause awk to print the current record by default. – Joseph R. Dec 18 '14 at 4:58

I think you should give fold a go:

tr \\n \\r <infile | fold -w1 | tr '\r\n'  '\n\t' >outfile

I preprocess fold's input w/ tr by replacing the instream \newline characters w/ \returns. fold is printing a \newline character for every column in input - each of your capital letters - but it resets its counter on \returns and so when tr does the final post-processing and converts fold's \newlines to \tabs there are no extra \tab characters in output. The output is three lines with a \tab character following each character but the last on each line.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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