how to extract strings

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












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









share|improve this question
























  • It's easy to solve it when you use awk, just two steps

    – Emilio Galarraga
    Feb 18 at 17:46















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









share|improve this question
























  • It's easy to solve it when you use awk, just two steps

    – Emilio Galarraga
    Feb 18 at 17:46













0












0








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









share|improve this question
















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






text-processing awk sed grep






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 18 at 17:13









Jeff Schaller

43.4k1160140




43.4k1160140










asked Feb 18 at 16:48









LearnerLearner

1356




1356












  • It's easy to solve it when you use awk, just two steps

    – Emilio Galarraga
    Feb 18 at 17:46

















  • It's easy to solve it when you use awk, just two steps

    – Emilio Galarraga
    Feb 18 at 17:46
















It's easy to solve it when you use awk, just two steps

– Emilio Galarraga
Feb 18 at 17:46





It's easy to solve it when you use awk, just two steps

– Emilio Galarraga
Feb 18 at 17:46










2 Answers
2






active

oldest

votes


















2














You can try something like:



awk -F| 'print $2' input_file|sort -u





share|improve this answer























  • 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





share|improve this answer




















  • 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








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f501401%2fhow-to-extract-strings%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You can try something like:



awk -F| 'print $2' input_file|sort -u





share|improve this answer























  • 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















2














You can try something like:



awk -F| 'print $2' input_file|sort -u





share|improve this answer























  • 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













2












2








2







You can try something like:



awk -F| 'print $2' input_file|sort -u





share|improve this answer













You can try something like:



awk -F| 'print $2' input_file|sort -u






share|improve this answer












share|improve this answer



share|improve this answer










answered Feb 18 at 16:50









Romeo NinovRomeo Ninov

6,69632029




6,69632029












  • 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

















  • 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
















does not make them unique, just sort them

– Learner
Feb 18 at 16:53





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





@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




2





... or all in awk: awk -F'|' '!seen[$2]++ print $2' input_file

– steeldriver
Feb 18 at 17:01





... or all in awk: awk -F'|' '!seen[$2]++ print $2' input_file

– steeldriver
Feb 18 at 17:01




1




1





@Romeo Ninov I liked it already

– Learner
Feb 18 at 17:30





@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





share|improve this answer




















  • 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















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





share|improve this answer




















  • 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













3












3








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





share|improve this answer















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






share|improve this answer














share|improve this answer



share|improve this answer








edited Feb 18 at 17:10

























answered Feb 18 at 17:04









mttpgnmttpgn

16317




16317







  • 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












  • 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







1




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






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





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





@mttpgn I liked it already

– Learner
Feb 18 at 17:31

















draft saved

draft discarded
















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f501401%2fhow-to-extract-strings%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown






Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay