LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 09-18-2008, 06: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, 06:31 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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, 07:43 PM   #3
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
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, 07:50 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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, 08:25 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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 01:09 AM.
 
  


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

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

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