Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then 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.
For instance, for a file testfile.conf, with the contents:
key1:yes
key2:no
#key3:no
The following command will create a backup file testfile.conf.old, and uncomment the third line:
Code:
sed -i\.old 's/\(#\)\(key3.*\)/\2/' testfile.conf
Rob
Ok. That makes more sense! Do I have to include all the text from the certain line I want to uncomment? Also I don't really need a backup so what part of the code do I take out? Please forgive me I've never used sed for anything.
sed
#erm, sed -i
#edit the file in place, rather than having to direct
#output to another file \.old
#this is the suffix for the backup file. The backslash
#"escapes" the full stop (stops it having its special #meaning) 's/<pattern>/<substituted pattern>/'
#the sed substitute command \(...\)
#defines a block to be matched # and key3
#the literal strings .
#matches any single character *
#matches the previous character 0 or more times. Since the
#preceding character was . it means "match any number of
#any characters" \2
#Replay what was matched by the second block - i.e.
#key3 followed by any other characters. testfile.conf
#The file to be processed
No, you don't have to include all the text from the line to be matched. You just have to specify a pattern that will match that line (and no line that you do not want uncommenting!). So (in this very simple example) #key3.* matches #key3:no, but would also match #key31957:fnord! It's worth having a look at man regex, and googling for help on regular expressions (this form of pattern matching). Once you get your head around them, regexes are one of the most powerful tools for working with strings, and are used with many other linux tools (most notably grep) as well as the majority of programming languages.
And as for backups - if you omit the suffix following -i, one won't be created. I usually feel a lot happier if I back up a config file before changing it, though.
Of course you can - stick it in a script and pass the name on invocation if it changes on some/all of the sysytems if you like.
Tink was gently trying to nudge you in the direction of reading some doco - start with the man page then have a look at this for a tutorial. The fella that wrote it knows a few things about Linux.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.