Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 09-18-2008, 07:09 AM   #1
Registered: Oct 2007
Posts: 33

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


In shell script i am searching for a pattern like this

value={ip = }

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

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

for example

value={ ip = }

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
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:
if [[ "aa" == " aa" ]]; then 
  echo match
  echo no match
Should output "no match" because of the leading space on the second operand. You can still use variables, e.g.
v=" aa"
if [[ "$v" == " aa" ]]; then 
  echo match
  echo no match
Should output "match".
Old 09-18-2008, 08:43 PM   #3
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,411

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
Do you really need to match on the '{' as well ?
Try a regex:
Old 09-18-2008, 08:50 PM   #4
Senior Member
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

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

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


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

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


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 > Forums > Non-*NIX Forums > Programming

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