User
User

Reputation: 398

How to use sed to split a string?

I am new to bash scripting. I want to split the following string,

chr14:81370042-81371098(+)

into

chr14 81370042 81371098 +

or

chr14:81370042-81371098(-)

into

chr14 81370042 81371098 -

Please tell me how do I do this with one command that would work for both the cases.

I tried, cat a.tsv | tr -s ':' '\t' | sed "s/\t[0-9]+.*[0-9]+/[0-9]+\t[0-9]/g", it does not work.

Thanks.

Upvotes: 4

Views: 1713

Answers (3)

Ed Morton
Ed Morton

Reputation: 203149

$ sed 's/[():]/ /g; s/-/ /' file
chr14 81370042 81371098 +
chr14 81370042 81371098 -

or if you don't like the trailing blank char that leaves then:

$ sed 's/[(:]/ /g; s/)//; s/-/ /' file
chr14 81370042 81371098 +
chr14 81370042 81371098 -

Upvotes: 3

anubhava
anubhava

Reputation: 784898

This sed may work:

sed -E 's/[^-+_[:alnum:]]+/ /g; s/ +$//; s/-(.)/ \1/g' <<< 'chr14:81370042-81371098(+)'

chr14 81370042 81371098 +

Or else:

sed -E 's/[^-+_[:alnum:]]+/ /g; s/ +$//; s/-(.)/ \1/g' <<< 'chr14:81370042-81371098(-)'

chr14 81370042 81371098 -

[^-+_[:alnum:]]+ matches 1 or more of any character that is not -, +, _ and alphanumeric.

Upvotes: 4

RavinderSingh13
RavinderSingh13

Reputation: 133428

Written and tested with shown samples, could you please try following.

echo "chr14:81370042-81371098(+)" | awk '{gsub(/:|-|\(|\)/,OFS)} 1'

2nd solution: Using field separator in awk.

echo "chr14:81370042-81371098(+)"  | 
awk -v FS=':|-|\(|\)' '{
  $1=$1
  sub(/ +$/,"")
}
1'

Upvotes: 3

Related Questions