Angelo
Angelo

Reputation: 5059

tab separating the values

I have a set of values in a column stored in a txt file,

A13R
W234H
Q1H

What I want is to separate the first character and last character by a tab, something like this

A "\t" 13 "\t" R
W "\t" 234 "\t" H

What I tried and ofcourse it failed was

tr "" "\t" <test.txt>test2.txt

could you suggest something in bash.

Upvotes: 1

Views: 73

Answers (3)

Fritz G. Mehner
Fritz G. Mehner

Reputation: 17198

Pure Bash; using substring expansion:

while read line ; do
  echo -e "${line:0:1}\t${line:1:-1}\t${line: -1:1}"
done < data.txt

Upvotes: 2

Kent
Kent

Reputation: 195179

does this work?

sed 's/^./&\t/;s/.$/\t&/' file

Upvotes: 3

FatalError
FatalError

Reputation: 54591

Since it was tagged in your question, you could user perl like:

perl -pi -e 's/(.)(.*)(.)/\1\t\2\t\3/' < in.txt > out.txt

Be careful about using the same file for input and output like your tr example. Remember, your shell will open both the input and output files before executing the command. The output file is clobbered, and so when your command runs, it finds an empty file. There are tools to work around this, like sponge, but it's just as easy to use two different file names.

Upvotes: 2

Related Questions