views:

71

answers:

4

I have a string like this

BRADI5G20430|BRADI5G20430.1||1

How can I replace the bar (single and multiple) with tab ("\t")? I tried this but dont' work

sed 's/\|+/\t/g'

I also want to include this line in bash script.

+1  A: 

Not sure off the top of my head why + doesn't work, but this will:

$ echo  "BRADI5G20430|BRADI5G20430.1||1" | sed -e 's/\|\{1,\}/\t/g'
BRADI5G20430\tBRADI5G20430.1\t1
David Wolever
+1  A: 

How didn't it work?

Hint: if you see how it didn't work, you should be able to see what you need to do to make it work -- try removing parts and see how the behaviour changes.

Hint #2: it's the + part that sed doesn't recognize.

sed 's/\|\|*/\t/g'

Ether
+1  A: 

I believe that the command you are looking for is actually

sed -e 's/[|]\{1,\}/\t/g'

Sed does not use the + syntax for one-or-more, but allows you to specify an open ended number of repititions. See here for more information.

a_m0d
sed does use `+`, you just need to escape it if not use `-r`
ghostdog74
+2  A: 

you need to escape the +

$ sed 's/|\+/\t/g' file
BRADI5G20430    BRADI5G20430.1  1

or you can use -r option of sed, but this time, the + will be "one or more" and you need to escape the "|" since in regex, it means alternation.

$ sed -r 's/\|+/\t/g' file
BRADI5G20430    BRADI5G20430.1  1

or use awk, set field separator to "|" and then set output field separator OFS to tab \t

eg

$ awk -F"|" '{$1=$1}1' OFS="\t" file
BRADI5G20430    BRADI5G20430.1          1

this replaces each tab with "|". If you want to replace all "|" with one tab

$ awk '{gsub(/\|+/,"\t")}1' file
BRADI5G20430    BRADI5G20430.1  1
ghostdog74