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 |
if you are going to use sed fo replacement, then you can go for sed to search too..
Code:
man sed |
Are you sure the grep is not working and not the sed?
when I do: Code:
grep "['dir']" file |
[ and ] are interpreted as special characters, see the man page of grep:
Code:
Character Classes and Bracket Expressions |
Quote:
|
Sorry my bad ... only had time to test with single entry ... pan64 has you on the right path :)
|
Quote:
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 |
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 |
in sed you can try:
Code:
sed -i "s/[[]'dir'[]]/['appname']['conf']/g" |
Quote:
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. |