5heikki
5heikki

Reputation: 152

Add a column of tabs into a tab-separated file

Input is a tab-separated file (here spaces but whatever):

Sample1 ACGGGGCGCAGCAGGCGCGA text other122
something ABCDSDDADASDASDSAD hjas 23823sss
ahsdksads AHJHJDHSJHJDALKDLK hello world

What I want to do is to add a column of tabs after the first column. So basically:

Sample1<TAB><TAB>ACGGGGCGCAGCAGGCGCGA<TAB>text<TAB>other122
something<TAB><TAB>ABCDSDDADASDASDSAD<TAB>hjas<TAB>23823sss
ahsdksads<TAB><TAB>AHJHJDHSJHJDALKDLK<TAB>hello<TAB>world

sed/awk preferred but whatever goes..

Upvotes: 0

Views: 1678

Answers (6)

Aleks-Daniel Jakimenko-A.
Aleks-Daniel Jakimenko-A.

Reputation: 10683

Pure bash solution:

filename='file.txt'
tempFile="/tmp/my_script_$RANDOM"
while read -r line; do
    printf "%s\n" "${line/  /       }" >> "$tempFile" # should be ${line/<TAB>/<TAB><TAB>}
done < "$filename"
mv -f -- "$tempFile" "$filename"

Upvotes: 0

lurker
lurker

Reputation: 58324

Since you want two tabs after the first column:

 sed 's/\([^ \t]*\) \([^ \t]*\)/\1\t\2/g' foo.txt | sed 's/\t/\t\t/'

Upvotes: 0

potong
potong

Reputation: 58578

This might work for you (GNU sed):

sed 's/\t/&&/' file

or:

awk '{sub(/\t/,"&&")}1' file

Upvotes: 3

fedorqui
fedorqui

Reputation: 290525

What about this?

$ awk 'BEGIN{OFS=FS="\t"} $2="\t"$2' file
Sample1         ACGGGGCGCAGCAGGCGCGA text other122
something       ABCDSDDADASDASDSAD hjas 23823sss
ahsdksads       AHJHJDHSJHJDALKDLK hello world

Which is exactly the same as awk 'BEGIN{OFS=FS="\t"} $1=$1"\t"'. That is, it either appends one tab to the end of 1st field or to the beginning of the 2nd one.

Also

$ awk 'BEGIN{OFS=FS="\t"} $2=FS$2' file

Upvotes: 1

Bentoy13
Bentoy13

Reputation: 4974

You change the output field seperator to a tab, and and an extra tab after the first element:

awk -v 'OFS=\t' '$1=$1"\t"'

Upvotes: 0

Ed Morton
Ed Morton

Reputation: 204731

sed 's/<TAB>/<TAB><TAB>/' file

Upvotes: 1

Related Questions