Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 01-09-2009, 03:49 PM   #1
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:
String string = "one two three four five six seven";
string = string.replaceFirst("[^ ]* [^ ]* [^ ]* ","");
I've tried
echo ${STRING/[^ ]* [^ ]* [^ ]* /}
but that just gives me the very last word.
Old 01-09-2009, 05:30 PM   #2
Registered: Jun 2008
Location: Belgium
Distribution: Debian
Posts: 109

Rep: Reputation: 42
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 :
${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

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

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

$ 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 04:28 AM.
Old 01-10-2009, 08:30 AM   #4
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, 09:32 AM   #5
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Or just use cut
$ 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


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

All times are GMT -5. The time now is 11:22 PM.

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