LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

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

Rep: Reputation: 0
[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
 
Old 01-21-2007, 07:55 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,307
Blog Entries: 54

Rep: Reputation: 2857Reputation: 2857Reputation: 2857Reputation: 2857Reputation: 2857Reputation: 2857Reputation: 2857Reputation: 2857Reputation: 2857Reputation: 2857Reputation: 2857
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
raskin
Senior Member
 
Registered: Sep 2005
Location: Russia
Distribution: NixOS (http://nixos.org)
Posts: 1,893

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
pieperp
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

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
 
Old 01-31-2007, 08:56 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

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

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


Reply


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


All times are GMT -5. The time now is 05:42 AM.

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