0

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
1
  • what is the logic here? try to describe it in a way that makes sense and also provide your attempts, since this seems quite basic. Commented Jun 25, 2015 at 14:57

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Alternatively using awk, the field separator could be [^0-9]+ (i.e. one or more non-digits).
@TomFenech Good point.Added to a answer. Thanks for the tip :)
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.