[bash] edit lines in a file
Hello,
Normally I solve all my problems in C++. However, few thinks can be solved easily with bash programming. Now I have the following problem. I have a file like: [DebugMode] Mode=0 StandAlone = 1 Now I want to change this to Mode=1 or Mode = 1 and StandAlone = 0 I have also a file where I want to comment out a line with # and insert a new line with text. Like: remote 10.22.22.1 2201 I want to comment this and insert a new line like: #remote 10.22.22.1 2201 remote mail.google.com 2201 This should not be so hard. thanks, Peter |
Now I want to / This should not be so hard.
OK, since you didn't ask for us to do your work for you, show us what you got. |
You probably need sed, (read 'man sed'). The most interesting switches are -i and -e, and you need s command.
|
Ok sorry,
I did not proceed with my problem until today. I read a good site http://www.grymoire.com/Unix/Sed.html#uh-0 now I have this and it works. openvpn.conf looks like: #remote 10.0.0.1 1941 remote mail.google.com 1900 #remote mail.google.com 1912 #remote mail.google.com 2301 It deletes old lines with the context google and insert a new one. Also some duplicate lines are removed. #!/bin/bash sed -e '/^remote/s/remote/#remote/g /remote.*google/d' openvpn.conf |uniq > tmp.txt sed -e '/remote/a\ remote mail.google.com 2201 ' tmp.txt > tmp1.txt mv tmp1.txt openvpn.conf rm tmp.txt And I have a script to change ini file variables for 0 to 1 like: #!/bin/bash sed -e'/Mode.*=/s/1/0/g /StandAlone.*=/s/1/0/g /PreSave.*=/s/1/0/g' Prog.ini > Prog.new mv Prog.new Prog.ini Maybe these scrips can be modified. However, have fun with it. Thanks, Peter |
or, in one go:
perl -pi.bak -e '/Mode/ and s/1/0/g' file |
All times are GMT -5. The time now is 03:42 AM. |