How can I use a shell script to add and replace lines in a file?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
Have you checked that? Actually the \ is not for = but to escape the white-space.
As far as I know, \ is used to escape any character that the shell would construe and you want it construed by the sed command. I'm pretty sure about that...
yes, but have a look at the placing. In both cases where \ is use it is located before a space, so in this instance it is not = that's being escaped, but the spaces.
#!/usr/bin/perl -wna
# splits into an array, -1 is the last field
$F[-1] = "XXX" if m/Timeout/;
$F[-1] = "XXX" if m/KeepAlive/;
$F[-1] = "XXX" if m/deliver_queue_load_max/;
# you get the idea?
print "@F\n" ;
perl -i.bak <script> <file>
will edit the file and create a backup .bak
You only need to use \ before "system" caracters if you don't want sed to interpret them...
like :
^ = start of line
$ = end of line
so if you want to replace :
My ^ gives me $$
by :
Hello world
Code:
echo 'My ^ gives me $$' | sed 's/My \^ gives me \$\$/Hello world/'
The backslash character isn't needed in your case because the sed expression is contained in single quotes. Suppose the name of the file you were changing was named "http one.conf" in that case you would either need to escape the space, or put the argument in quotes. If the filename contained the "$" character, then you would need either a backslash or single quotes.
All of the whitespace characters and special characters are listed in the bashref manual.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.