LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to get only the first alphabet pattern from a input? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-get-only-the-first-alphabet-pattern-from-a-input-4175451079/)

RaviTezu 02-21-2013 03:58 AM

how to get only the first alphabet pattern from a input?
 
I have some files with below names:

xen-scripts-org-4.999.9-0.3.beta.20021004git.blahblah.x86_64

xx-yyyy-zzzz-4.999.9-0.3.beta.20021007git.8.amzn1.x86_64

I'd like use some command or a small script to get the first letters(not numbers) from those file names.

output should look like: xen-scripts-org


Please help.

chrism01 02-21-2013 04:01 AM

Is this what you want
Code:

echo xx-yyyy-zzzz-4.999.9-0.3.beta.20021007git.8.amzn1.x86_64|cut -d'-' -f1,2,3
xx-yyyy-zzzz


RaviTezu 02-21-2013 04:09 AM

hi chrism01,

but if the input is something like this "xx-yyyy-4.999.9-0.3.beta.20021007git.8.amzn1.x86_64"

i won't be getting the desired output ..right?

I need to ignore the numbers from the input and get the first name.

Thanks for your reply.

chrism01 02-21-2013 04:11 AM

That's what it does; you get the leading xx-yyyy-zzzz; just try it.
If unless you wanted something else, please show a before and after example to clarify.

whizje 02-21-2013 04:13 AM

Could you post examples of input and required output.

RaviTezu 02-21-2013 04:22 AM

Quote:

Originally Posted by chrism01 (Post 4896549)
That's what it does; you get the leading xx-yyyy-zzzz; just try it.
If unless you wanted something else, please show a before and after example to clarify.

If you see my second post particulalry.. i changed the input. it doesn't contain zzzz.
If i use your command.. it gives the following out put...
[user@mc ~]$ echo xx-yyyy-4.999.9-0.3.beta.20021007git.8.amzn1.x86_64|cut -d'-' -f1,2,3
xx-yyyy-4.999.9

Which is wrong.

RaviTezu 02-21-2013 04:26 AM

If input is xx-4.999.9-0.3.beta.20021007git.8.amzn1.x86_64 should get xx
& xx-4.99 should get xx
& abcd-2.555.6666.8885 should get abcd
& xx-yyyyy-zzzz-wwww-20021007git.8 should get xx-yyyyy-zzzz-wwww

chrism01 02-21-2013 04:29 AM

Ok, so a variable num of 'fields' before the 4.999 etc.
IF the 4.99 is a constant ie where you want to grab the prefix
Code:

echo xx-yyyy-zzzz-4.999.9-0.3.beta.20021007git.8.amzn1.x86_64|sed -e s/-4.*//
xx-yyyy-zzzz

 echo xx-yyyy-4.999.9-0.3.beta.20021007git.8.amzn1.x86_64|sed -e s/-4.*//
xx-yyyy

You need to delineate the rules exactly; what can change, what can vary and which part you want extracted.


Edit: so had that open while you posted above. Here's the amended sed
Code:

sed -e s/-[0-9].*//
Basically remove pattern that starts with '-', followed by a digit, followed by anything...

RaviTezu 02-21-2013 05:48 AM

thanks Chris.


All times are GMT -5. The time now is 01:43 AM.