LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 11-25-2009, 01:21 AM   #1
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 707

Rep: Reputation: 55
regex in shell scripts


Hi all..

Code:
[vinay@TEG test]$ echo 'text[2*?!]' | sed 's/text*[2-4]*/vinay/'
vinay[2*?!]
Code:
[vinay@TEG test]$ echo 'text[2*?!]' | sed 's/text*[2-4]/vinay/'
text[2*?!]
In second case text* should match text in text[2*?!] and the output should be vinay[2*?!] right ?
 
Old 11-25-2009, 01:33 AM   #2
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
wrong, although I guess you knew that already.

Quote:
sed 's/text*[2-4]/vinay/'
This would match:

tex2
tex3
tex4
text2
text3
text4
textt2
textt3
textt4

Basically, match tex then any number of the character t (including zero) followed by a number between 2 and 4
 
Old 11-25-2009, 01:48 AM   #3
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
Try:

Code:
echo 'text[2*?!]'|sed 's/\(text\)\(\[[2-4]\)/vinay\2/'
\(text\) searces for an exact match for text
\(\[[2-4]\) searches for a litteral [ character followed by a number between 2 and 4

Therefore \1 refers to text (but we are not using this part.

\2 refers (in this instance) to [2

We replace the search with vinay followed by the second reference [2 the rest of the string is kept as it wasn't part of the search.

HTH,

Dis

Last edited by Disillusionist; 11-25-2009 at 01:54 AM.
 
Old 11-25-2009, 03:45 AM   #4
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 707

Original Poster
Rep: Reputation: 55
Hi Disillusionist

Quote:
Originally Posted by Disillusionist View Post
Try:

Code:
echo 'text[2*?!]'|sed 's/\(text\)\(\[[2-4]\)/vinay\2/'
is giving output as
vinay[2*?!]

Actually output should be
vinay[2

Also you are escaping [ with \ to match [, I hope its not necessary..why its matching whole [2*?!] ??

Till now i have read lot of material on regex..still struggling to find answers..

Last edited by vinaytp; 11-25-2009 at 03:46 AM.
 
Old 11-25-2009, 03:58 AM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by vinaytp View Post
Hi Disillusionist



is giving output as
vinay[2*?!]

Actually output should be
vinay[2
Why do you think so? Please explain.

Quote:
Originally Posted by vinaytp View Post
Also you are escaping [ with \ to match [, I hope its not necessary..why its matching whole [2*?!] ??
It isn't. It's only replacing the matched text (which happens to include
[2-4) and putting it back in the replacement string with \2 ... '*?!]' wasn't
matched and won't be replaced.


Cheers,
Tink
 
  


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
Shell regex - add new field czezz Programming 3 02-28-2009 03:13 PM
How to ssh from a shell script ? For ppl who can write shell scripts. thefountainhead100 Programming 14 10-22-2008 06:24 AM
regex with sed to process file, need help on regex dwynter Linux - Newbie 5 08-31-2007 05:10 AM
performing regex on shell script variable? ocicat Programming 3 07-17-2007 02:10 PM
shell scripts nautilus_1987 Linux - General 3 08-30-2002 03:12 AM

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

All times are GMT -5. The time now is 09:02 AM.

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