sed with vertical bar?

Viewed 301

I have a list

>ANARCI-HMM_human_167.7|pdb|7EPU|A
>ANARCI-HMM_alpaca_173.7|pdb|7EVY|E
>ANARCI-HMM_alpaca_172.8|pdb|7F2O|S
>ANARCI-HMM_alpaca_171.8|pdb|7F4F|S
>ANARCI-HMM_alpaca_173.6|pdb|7F8W|D

I want to remove from ANARCI to the first vertical bar |.

expecting

>pdb|7EPU|A
>pdb|7EVY|E
>pdb|7F2O|S
>pdb|7F4F|S
>pdb|7F8W|D

I tried

sed 's/ANARCI.*\|//g'

but didn't work.

Do you have any idea how to sed in this case?

4 Answers

Using sed

$ sed 's/[A-Z][^|]*|//' input_file
>pdb|7EPU|A
>pdb|7EVY|E
>pdb|7F2O|S
>pdb|7F4F|S
>pdb|7F8W|D

1st solution: With your shown samples, please try following sed code.

sed -E 's/(.*)ANARCI[^|]*\|(.*)/\1\2/' Input_file

Explanation: Adding detailed explanation for above sed code.

  • Using -E option of sed to enable ERE(extended regular expression) for program.
  • Then using sed's capability of storing matched patterns into temporary buffer memory(called capturing groups), by which we can make use of caught values while substitution.
  • Creating 2 capturing groups here, 1st which has everything before ANARCI string and 2nd capturing group which has everything after first pipe(matching from ANARCI to till first pipe) to get rest of part after first pipe.
  • While performing substitution substituting line with 1st and 2nd capturing group.


2nd solution: You could use awk for this task also, use match function of awk. Simple explanation would be, using match function of awk and matching only part which you don't required in output, while printing the values printing everything else apart from matched part(which is not required).

awk 'match($0,/ANARCI[^|]*/){print substr($0,1,RSTART-1) substr($0,RSTART+RLENGTH+1)}' Input_file


3rd solution: Adding 1 more solution in awk, where setting field separators to: from string ANARCI to till first occurrence of pipe. Then in main awk program printing 1st and last field, required values as per shown samples.

awk -v FS="ANARCI[^\\\\|]*\\\\|" '{print $1 $NF}' Input_file

Try:

sed 's/ANARCI[^|]*\|//g'

to not match the |

If you want to remove from ANARCIat the first vertical bar |, try this:

sed 's/ANARCI[^|]*\|//g'

or

sed 's/ANARCI[^|]*\|(.*)/\1\2/'
Related