Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 01-21-2007, 07:43 AM   #1
LQ Newbie
Registered: Jan 2007
Posts: 2

Rep: Reputation: 0
[bash] edit lines in a file


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:

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.

remote 2201

I want to comment this and insert a new line like:

#remote 2201
remote 2201

This should not be so hard.


Old 01-21-2007, 07:55 AM   #2
Registered: May 2001
Posts: 29,359
Blog Entries: 55

Rep: Reputation: 3546Reputation: 3546Reputation: 3546Reputation: 3546Reputation: 3546Reputation: 3546Reputation: 3546Reputation: 3546Reputation: 3546Reputation: 3546Reputation: 3546
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.
Old 01-21-2007, 08:51 AM   #3
Senior Member
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (
Posts: 1,900

Rep: Reputation: 68
You probably need sed, (read 'man sed'). The most interesting switches are -i and -e, and you need s command.
Old 01-31-2007, 07:32 AM   #4
LQ Newbie
Registered: Jan 2007
Posts: 2

Original Poster
Rep: Reputation: 0
Ok sorry,

I did not proceed with my problem until today.
I read a good site

now I have this and it works.
openvpn.conf looks like:
#remote 1941
remote 1900
#remote 1912
#remote 2301

It deletes old lines with the context google and insert a new one.
Also some duplicate lines are removed.

sed -e '/^remote/s/remote/#remote/g
/remote.*google/d' openvpn.conf |uniq > tmp.txt
sed -e '/remote/a\
remote 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:


sed -e'/Mode.*=/s/1/0/g
/PreSave.*=/s/1/0/g' Prog.ini >
mv Prog.ini

Maybe these scrips can be modified. However, have fun with it.


Old 01-31-2007, 08:56 AM   #5
Senior Member
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,421

Rep: Reputation: 200Reputation: 200Reputation: 200
or, in one go:

perl -pi.bak -e '/Mode/ and s/1/0/g' file


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH Scripting - printing select lines in a file bullfrog1870 Linux - Newbie 16 11-08-2006 09:33 AM
how can i EDIT file by BASH Guru Mind Programming 27 03-28-2006 03:38 AM
[bash] removing or editing lines in file Erhnam Programming 12 03-15-2005 07:25 AM
Can't get lines of a file with a Bash script.. barisdemiray Programming 2 08-11-2004 12:42 PM
[bash] remove lines from a file Drimo Programming 3 03-20-2004 11:16 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:43 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration