0

My data is like below

sp|Q9H9K5|MER34_HUMAN(9-21)
sp|Q9H9K5|MER34_HUMAN(493-507)
sp|Q9H9K5|MER34_HUMAN(524-539)
sp|P31689|DNJA1_HUMAN(22-33)
sp|P31689|DNJA1_HUMAN(66-82)
sp|P31689|DNJA1_HUMAN(93-104)
sp|P08246|ELNE_HUMAN(7-27)
sp|P08246|ELNE_HUMAN(72-83)
sp|P10144|GRAB_HUMAN(5-13)

I am trying to extract the string between ||

sed -n " ||" file
grep "||" file 

did not work .

desire output is like this

Q9H9K5
Q9H9K5
Q9H9K5
P31689
P31689
P31689
P08246
P08246
P10144

Then I want to make them unique

 Q9H9K5
 P31689
 P08246
 P10144
  • It's easy to solve it when you use awk, just two steps – Emilio Galarraga Feb 18 at 17:46
2

You can try something like:

awk -F\| '{print $2}' input_file|sort -u
  • does not make them unique, just sort them – Learner Feb 18 at 16:53
  • @Learner, did you saw the key -u at the end of sort? This is to output only unique values (after sort) – Romeo Ninov Feb 18 at 16:56
  • 2
    ... or all in awk: awk -F'|' '!seen[$2]++ {print $2}' input_file – steeldriver Feb 18 at 17:01
  • 1
    @Romeo Ninov I liked it already – Learner Feb 18 at 17:30
3

You can use cut here to nice effect.

cut -d\| -f2 myfile.txt 

Produces the following output:

Q9H9K5
Q9H9K5
Q9H9K5
P31689
P31689
P31689
P08246
P08246
P10144

The -d tells cut to look out for the pipe character to delimit the columns of your output (in this case we must escape it). The -f specifies which column (or columns) of your input you want back. Columns are numbered starting with 1.

If you only want the unique values, you can pipe that output into sort and uniq as follows:

cut -d\| -f2 myfile.txt | sort | uniq

This produces:

P08246
P10144
P31689
Q9H9K5
  • 1
    Alternatively: cut -d '|' -f 2 file | sort -u or with just uniq instead of sort -u if the data is already sorted. – Kusalananda Feb 18 at 17:07
  • You can use sort -u instead of sort|uniq – Romeo Ninov Feb 18 at 17:09
  • @mttpgn I liked it already – Learner Feb 18 at 17:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.