LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   grep for ['dir'] (https://www.linuxquestions.org/questions/linux-newbie-8/grep-for-%5Bdir%5D-4175491983/)

threezerous 01-21-2014 02:29 AM

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

SAbhi 01-21-2014 02:47 AM

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']


grail 01-21-2014 02:47 AM

Are you sure the grep is not working and not the sed?

when I do:
Code:

grep "['dir']" file
This works fine.

pan64 01-21-2014 03:59 AM

[ 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

threezerous 01-21-2014 09:26 AM

Quote:

Originally Posted by grail (Post 5102043)
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']

grail 01-21-2014 10:03 AM

Sorry my bad ... only had time to test with single entry ... pan64 has you on the right path :)

threezerous 01-21-2014 10:32 AM

Quote:

Originally Posted by pan64 (Post 5102068)
[ 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

grail 01-21-2014 10:43 AM

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

pan64 01-21-2014 10:43 AM

in sed you can try:
Code:

sed -i "s/[[]'dir'[]]/['appname']['conf']/g"

threezerous 01-22-2014 10:22 AM

Quote:

Originally Posted by grail (Post 5102275)
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


All times are GMT -5. The time now is 06:20 AM.