Home > Software engineering >  print text between pipes
print text between pipes

Time:10-02

I have a text file with ids as below

ref|XP_029641976.1|
ref|XP_014779594.1|
gb|KOF78315.1|

I wish to print only the text between pipes and I've tried this sed -n '/|/,/|/p and also tried substituting but they don't work. The string in front of the first pipe varies. Any ideas? Thank you

CodePudding user response:

You can get text between pipes with sed regexp like this

#!/bin/bash

echo "ref|XP_029641976.1|
ref|XP_014779594.1|
gb|KOF78315.1|" | sed -E "s:.*\|(.*?)\|:\1:g"

CodePudding user response:

As others have suggested, awk is a good candidate for this:

awk -F'|' '{ print $2 }' file

Specify the field delimiter with -F and then print the second pipe delimited field with $2

  •  Tags:  
  • bash
  • Related