LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-29-2008, 07:46 PM   #1
pvpnguyen
Member
 
Registered: Mar 2006
Posts: 38

Rep: Reputation: 15
Script question


Hello All,

I have a string AAA_BBB-CCC_DDD-1000. Using bash script, how can I extract the number 1000 from the string? Your help is very much appreciated, thank you.
 
Old 02-29-2008, 08:31 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
This is pretty vague. Do you always have all letters and then just one group of numbers? To write a good script you have to anticipate all possible combinations.

If you just want to extract a group of 4 numbers anywhere on the line:

sed -r 's/.*([0-9]{4}).*/\1/' filename > newfilename
 
Old 02-29-2008, 09:28 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by pvpnguyen View Post
Hello All,

I have a string AAA_BBB-CCC_DDD-1000. Using bash script, how can I extract the number 1000 from the string? Your help is very much appreciated, thank you.
please go to this page, look under "Substring Removal", especially
"${string##substring}". Try the example. you will get what you need.
 
Old 03-02-2008, 05:37 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
A simple soln, assuming input us exactly formatted as per OP

echo AAA_BBB-CCC_DDD-1000|cut -d'-' -f3
 
Old 03-03-2008, 09:20 AM   #5
pvpnguyen
Member
 
Registered: Mar 2006
Posts: 38

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by chrism01 View Post
A simple soln, assuming input us exactly formatted as per OP

echo AAA_BBB-CCC_DDD-1000|cut -d'-' -f3
Hello Chrism01,

Thank you very much for your help. It works with your solution. I can't match the string since it is an input and changes all the time. But the 2 '-'s are always there, so yours works.
Again, thank you all for your answers and supports.
 
  


Reply



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
One last script question kinetik Linux - General 2 04-02-2006 05:47 PM
script question therealbxp Programming 5 08-07-2005 04:28 AM
Script Question easyE Linux - General 2 04-03-2005 11:34 AM
Script Question blas Linux - General 5 02-14-2004 12:48 AM
some script-question G.P.P. Linux - Newbie 3 01-11-2003 05:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:09 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
Open Source Consulting | Domain Registration