LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 09-18-2008, 07:09 AM   #1
bharatbsharma
Member
 
Registered: Oct 2007
Posts: 33

Rep: Reputation: 15
omit white spaces for pattern matching in shell script


Hello

In shell script i am searching for a pattern like this

----------------------------------
value={ip = 10.10.10.10 }

if [[ $value == {ip* ]]
-----------------------------------

but if i have n no. of white spaces between "{" and "ip"

for example

value={ ip = 10.10.10.10 }

my pattern matching does not work.

How do i have have a general pattern matching in which i can have n no. of white spaces or no white spaces between "{" and "ip"?

Can somebody help me?
 
Old 09-18-2008, 07:31 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
To match spaces, use quotes:
Code:
if [[ "aa" == " aa" ]]; then 
  echo match
else 
  echo no match
fi
Should output "no match" because of the leading space on the second operand. You can still use variables, e.g.
Code:
v=" aa"
if [[ "$v" == " aa" ]]; then 
  echo match
else 
  echo no match
fi
Should output "match".
 
Old 09-18-2008, 08:43 PM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
Do you really need to match on the '{' as well ?
Try a regex: http://www.tldp.org/LDP/abs/html/bas...#REGEXMATCHREF
 
Old 09-18-2008, 08:50 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
you can use simple case/esac
Code:
# s="value={ip = 10.10.10.10 }"
# case $s in
> value={*ip* ) echo $s;;
> esac
value={ip = 10.10.10.10 }
# s="value={ ip = 10.10.10.10 }"
# case $s in  value={*ip* ) echo $s;; esac
value={ ip = 10.10.10.10 }
 
Old 09-18-2008, 09:25 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
To match on particular characters separated by any number of "whitespace" bytes:

abc[[:space:]]*def

matches all of the following:
Code:
abcdef
abc def
abc  def
abc   def
The whitespace can be tabs or spaces

Last edited by pixellany; 09-19-2008 at 02:09 AM.
 
  


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
Linux/Unix script for file pattern matching varunnarang Programming 1 08-07-2006 02:14 PM
bourne shell pattern matching or regular expression powah Programming 2 06-30-2006 11:27 AM
bash script pattern matching thedude2010 Programming 9 06-02-2006 03:39 AM
Pattern Matching Help in Bash script cmfarley19 Programming 1 04-07-2004 10:22 AM


All times are GMT -5. The time now is 05:26 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration