Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
[ 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.
[ 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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.