Kryo
Kryo

Reputation: 933

manipulate text using shell script?

How can i manipulate the text file using shell script?

input

chr2:98602862-98725768
chr11:3100287-3228869
chr10:3588083-3693494
chr2:44976980-45108665

expected output

2 98602862 98725768
11 3100287 3228869
10 3588083 3693494
2 44976980 45108665

Upvotes: 0

Views: 41

Answers (2)

Ed Morton
Ed Morton

Reputation: 203645

If you don't care about a leading blank char:

$ tr -s -c '[0-9\n]' ' ' < file
 2 98602862 98725768
 11 3100287 3228869
 10 3588083 3693494
 2 44976980 45108665

Upvotes: 1

nu11p01n73R
nu11p01n73R

Reputation: 26667

Using sed you can write

$ sed 's/chr//; s/[:-]/ /g' file
2 98602862 98725768
11 3100287 3228869
10 3588083 3693494
2 44976980 45108665

Or maybe you could use awk

awk -F "chr|[-:]" '{print $2,$3, $4}' file
2 98602862 98725768
11 3100287 3228869
10 3588083 3693494
2 44976980 45108665

What it does

  • -F "chr|[-:]" sets the field separators to chr or : or -. Now you could print the different fields or columns.

  • You can also use another field separator as -F [^0-9]+ which will makes anything other than digits as separators.

Upvotes: 1

Related Questions