LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 01-21-2014, 02:29 AM   #1
threezerous
Member
 
Registered: Jul 2009
Posts: 94

Rep: Reputation: 15
Question grep for ['dir']


Not sure what I am missing here. I have file which has the string ['dir'] in multiple lines.

If I grep for dir I get correct results.
If I grep for \'dir\', I also get correct results.
However, if I grep for either [\'dir\'] or \[\'dir\'\]
I get all the lines (irrespective of string being present or not), . I need to search for the entire string including square brackets, since eventually I want to replace the string ['dir'] with ['appname']['conf'] using sed

So that a line such as
application_dir node['dir'] will change to
application_dir node['appname']['conf']

Something on the lines of

grep -rl "['dir']" | xargs sed -i "s/['dir']/['appname']['conf']/g"

However, cannot get my grep command to work with ['dir']

Any assistance/suggestion is much appreciated.
Thanks
 
Old 01-21-2014, 02:47 AM   #2
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 664

Rep: Reputation: 81
if you are going to use sed fo replacement, then you can go for sed to search too..



Code:
man sed

# echo "application_dir node['dir']"|sed "s/\['dir'\]/\['appname'\]\['conf'\]/g"
application_dir node['appname']['conf']
 
Old 01-21-2014, 02:47 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,493

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
Are you sure the grep is not working and not the sed?

when I do:
Code:
grep "['dir']" file
This works fine.
 
Old 01-21-2014, 03:59 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,188

Rep: Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673
[ and ] are interpreted as special characters, see the man page of grep:
Code:
Character Classes and Bracket Expressions

A bracket expression is a list of characters enclosed by [ and ]. It matches any single character in that list; if the first character of the list is the caret ^ then it matches any character not in the list. For example, the regular expression [0123456789] matches any single digit.
use grep -F "['dir']" or fgrep "['dir']" instead
 
1 members found this post helpful.
Old 01-21-2014, 09:26 AM   #5
threezerous
Member
 
Registered: Jul 2009
Posts: 94

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by grail View Post
Are you sure the grep is not working and not the sed?

when I do:
Code:
grep "['dir']" file
This works fine.
Yes I confirmed. It returns all lines having dir. I want only those lines which are of the format ['dir']
 
Old 01-21-2014, 10:03 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,493

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
Sorry my bad ... only had time to test with single entry ... pan64 has you on the right path
 
Old 01-21-2014, 10:32 AM   #7
threezerous
Member
 
Registered: Jul 2009
Posts: 94

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by pan64 View Post
[ and ] are interpreted as special characters, see the man page of grep:
Code:
Character Classes and Bracket Expressions

A bracket expression is a list of characters enclosed by [ and ]. It matches any single character in that list; if the first character of the list is the caret ^ then it matches any character not in the list. For example, the regular expression [0123456789] matches any single digit.
use grep -F "['dir']" or fgrep "['dir']" instead
The fgrep "['dir']" did give the correct results but sed fails now as

fgrep -r "['dir']" | xargs sed -i "s/['dir']/['appname']['conf']/g"
sed: invalid option -- 'x'

I am not specifying any --x option except inxargs.

Thanks for the help on fgrep
 
Old 01-21-2014, 10:43 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,493

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
Where has your -l gone that says pass the file names to xargs??

Also, you always seem to not actually list a file against the grep so not sure how you are getting any results at all
 
Old 01-21-2014, 10:43 AM   #9
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,188

Rep: Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673
in sed you can try:
Code:
sed -i "s/[[]'dir'[]]/['appname']['conf']/g"
 
1 members found this post helpful.
Old 01-22-2014, 10:22 AM   #10
threezerous
Member
 
Registered: Jul 2009
Posts: 94

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by grail View Post
Where has your -l gone that says pass the file names to xargs??

Also, you always seem to not actually list a file against the grep so not sure how you are getting any results at all
Sorry that was a type. I actually ran (unsuccessfully)
fgrep -r "['dir']" * | xargs sed -i "s/['dir']/['appname']['conf']/g"

I was able to solve the issue using the script below

find -type f | xargs sed -i "s/\['dir'\]/\['appname'\]\['conf'\]/g"

Thanks all for the various ideas. Learnt a few things along the way
 
  


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
copying files from home dir to another dir from another dir in a lower dir chomito44 Linux - General 5 10-19-2013 06:18 PM
tar dir and sub dir removing files but not existing not empty dir j-me Linux - General 2 08-12-2013 11:37 AM
[SOLVED] grep DIR and FILENAME with [SPACE] and \ characters in name ? czezz Programming 6 06-08-2012 11:37 AM
Command to display /dir, /dir/sub, /dir/sub/files knockout_artist Linux - Newbie 9 10-25-2007 02:57 PM
grep a line in a file from dir bkeating Linux - Newbie 4 07-24-2003 02:04 AM


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