LinuxQuestions.org
Visit the LQ Articles and Editorials section
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 02-23-2014, 01:08 PM   #1
raj k yadav
LQ Newbie
 
Registered: Dec 2008
Posts: 24

Rep: Reputation: 2
ignore space in string for grep


How can I grep a string of words separated with space.

I want to search the #URI line in ldap.conf but I can not.

[root@client ~]# cat /etc/openldap/ldap.conf
#
# LDAP Defaults
#

# See ldap.conf(5) for details
# This file should be world readable but not world writable.

#BASE dc=example,dc=com
#URI ldap://ldap.example.com ldap://ldap-master.example.com:666

#SIZELIMIT 12
#TIMELIMIT 15
#DEREF never

TLS_CACERTDIR /etc/openldap/certs
[root@client ~]#
[root@client ~]#
[root@client ~]#
[root@client ~]# cat /etc/openldap/ldap.conf | grep "^#URI ldap://"
[root@client ~]#
 
Old 02-23-2014, 01:38 PM   #2
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,294

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
Quote:
Originally Posted by raj k yadav View Post
How can I grep a string of words separated with space.
Enclose your search string in quote marks.

With this InFile ...
Code:
Once upon a midnight dreary, while I pondered weak and weary,
Over many a quaint and curious volume of forgotten lore,
While I nodded, nearly napping, suddenly there came a tapping,
As of some one gently rapping, rapping at my chamber door.
"'Tis some visitor," I muttered, "tapping at my chamber door -
Only this, and nothing more."
... this grep ...
Code:
grep "chamber door" $InFile >$OutFile
... produced this OutFile ...
Code:
As of some one gently rapping, rapping at my chamber door.
"'Tis some visitor," I muttered, "tapping at my chamber door -
Daniel B. Martin
 
Old 02-23-2014, 01:46 PM   #3
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,974

Rep: Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850
Hi,

I don't understand why you want to match the whitespace. You could use
Code:
grep "^#\?URI" /etc/openldap/ldap.conf
with grep or
Code:
egrep "^#?URI" /etc/openldap/ldap.conf
with egrep. Whitespace can be searched with \s or [[:space:]] which would in egrep lead to this expression
Code:
egrep "^#?URI[[:space:]]" /etc/openldap/ldap.conf
or
Code:
egrep "^#?URI\s" /etc/openldap/ldap.conf
Hope this helps.

Markus
 
Old 02-23-2014, 04:33 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 13,248

Rep: Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287
If you (really) need to include whitespace use the character class - you never can be sure there is only one
Code:
[[:space:]]+
 
  


Reply

Tags
bash, grep, search


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
[SOLVED] Issue trying to grep a string, but keep a similar string Supp0rtLinux Linux - Software 7 10-04-2011 06:35 PM
[SOLVED] grep a string with space vikas027 Programming 5 02-09-2010 04:22 PM
grep a particular string john83reuben Linux - Newbie 3 12-07-2009 09:34 AM
grep string with space (2 word string) casperdaghost Linux - Newbie 7 08-24-2009 02:11 AM
How to configure 'grep' to ignore 'No such file or directory n179911 Linux - Newbie 4 07-08-2009 04:14 AM


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