LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 01-09-2009, 02:49 PM   #1
BeholdMyGlory
LQ Newbie
 
Registered: Jan 2009
Posts: 1

Rep: Reputation: 0
Regex in bash?


Say I have a string conatining a few words:
STRING="one two three four five six seven";

how do I go about removing the first three words? I've search google but not yet found anything that seems to do what I want.

I would like to use the following regex:
"[^ ]* [^ ]* [^ ]* "
As demonstrated i Java:
Code:
String string = "one two three four five six seven";
string = string.replaceFirst("[^ ]* [^ ]* [^ ]* ","");
System.out.println(string)
I've tried
echo ${STRING/[^ ]* [^ ]* [^ ]* /}
but that just gives me the very last word.
 
Old 01-09-2009, 04:30 PM   #2
antegallya
Member
 
Registered: Jun 2008
Location: Belgium
Distribution: Debian
Posts: 109

Rep: Reputation: 42
Hello,
it is simply because the substring replacement you are using is expanding to the longest match. (Maybe what you do isn't what you mean, the '*' in bash doesn't mean the same as the '*' in java's regexp or others. Bash's '*' means expanding to any string of any character and it doesn't expand only the preceeding pattern)
Try using this instead :
Code:
${STRING#[^ ]* [^ ]* [^ ]* }
the '#' is for deleting the following match, starting from the front of $STRING and matching the shortest match.
In java '${string#PATTERN}' would be something like
Code:
string.replaceFirst("^PATTERN","")
Regards,
antegallya

Last edited by antegallya; 01-09-2009 at 04:31 PM.
 
Old 01-10-2009, 03:26 AM   #3
openSauce
Member
 
Registered: Oct 2007
Distribution: Fedora, openSUSE
Posts: 252

Rep: Reputation: 39
^^ the above doesn't work for me. However you can use sed:

Code:
$ echo $STRING | sed 's/[^ ]* [^ ]* [^ ]* //'
four five six seven
Type info sed to find out how it works.

Edit: actually antegallya's works too, just had a typo before

Last edited by openSauce; 01-10-2009 at 03:28 AM.
 
Old 01-10-2009, 07:30 AM   #4
archieval
Member
 
Registered: Apr 2007
Location: Philippines
Distribution: Kubuntu, Ubuntu, CentOS
Posts: 289

Rep: Reputation: 41
u can also use perl, its a practical extraction =)
 
Old 01-10-2009, 08:32 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Or just use cut
Code:
$ string="one two three four five six seven"
$ echo $string
one two three four five six seven
$ string=$(echo $string | cut -d" " -f4-)
$ echo $string
four five six seven
 
  


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
bash replace all matches of regex substring in string nickleus Linux - General 3 04-30-2011 11:08 AM
BASH get marked RegEx result: "foo s bar" | /foo(.*)bar/ 1 -> " s " hansschmucker Programming 6 10-23-2007 10:34 PM
bash: better way to delete files not matching a regex? pbhj Programming 8 10-15-2007 03:05 PM
regex with sed to process file, need help on regex dwynter Linux - Newbie 5 08-31-2007 05:10 AM
BASH RegEx file name parsing Hewson Linux - General 7 04-27-2007 05:37 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12: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