LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-21-2013, 04:58 AM   #1
RaviTezu
Member
 
Registered: Nov 2012
Location: India
Distribution: Fedora, CentOs, RHEL
Posts: 164

Rep: Reputation: 24
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.

Last edited by RaviTezu; 02-21-2013 at 05:01 AM.
 
Old 02-21-2013, 05:01 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
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
 
Old 02-21-2013, 05:09 AM   #3
RaviTezu
Member
 
Registered: Nov 2012
Location: India
Distribution: Fedora, CentOs, RHEL
Posts: 164

Original Poster
Rep: Reputation: 24
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.
 
Old 02-21-2013, 05:11 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
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.
 
Old 02-21-2013, 05:13 AM   #5
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 592

Rep: Reputation: 140Reputation: 140
Could you post examples of input and required output.
 
Old 02-21-2013, 05:22 AM   #6
RaviTezu
Member
 
Registered: Nov 2012
Location: India
Distribution: Fedora, CentOs, RHEL
Posts: 164

Original Poster
Rep: Reputation: 24
Quote:
Originally Posted by chrism01 View Post
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.
 
Old 02-21-2013, 05:26 AM   #7
RaviTezu
Member
 
Registered: Nov 2012
Location: India
Distribution: Fedora, CentOs, RHEL
Posts: 164

Original Poster
Rep: Reputation: 24
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
 
Old 02-21-2013, 05:29 AM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
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...

Last edited by chrism01; 02-21-2013 at 05:33 AM.
 
1 members found this post helpful.
Old 02-21-2013, 06:48 AM   #9
RaviTezu
Member
 
Registered: Nov 2012
Location: India
Distribution: Fedora, CentOs, RHEL
Posts: 164

Original Poster
Rep: Reputation: 24
thanks Chris.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Search and replace Pattern preceeding another pattern nbkisnz Programming 3 05-13-2012 02:50 PM
[SOLVED] /bin/bash if statement pattern search, end of pattern special character? headhunter_unit23 Programming 3 04-29-2010 09:05 AM
[SOLVED] Adding (not replacing) a pattern match with a similar pattern? b-bri Linux - Newbie 2 08-31-2009 01:36 AM
printing pattern match and not whole line that matches pattern Avatar33 Programming 13 05-06-2009 07:17 AM
Bash for organizing by alphabet IanGlenn Programming 8 12-08-2007 06:40 PM


All times are GMT -5. The time now is 08:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration