LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-21-2008, 12:16 PM   #1
Curtor
Member
 
Registered: Feb 2008
Posts: 65

Rep: Reputation: 16
Automated editing of multiple lines in a file


I have a file that contains a multiple single lines that specify an item, and then a list under each item. I want to basically edit the file so that I can delete a list and replace it with my own list.

For example, /etc/network/interfaces:
Code:
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
address 192.168.0.1
netmask 255.255.255.0
gateway 192.168.1.1

auto eth1
iface eth1 inet dhcp

iface eth2 inet dhcp
auto eth2
How would I go about setting new parameters for all the fields under eth0? My thought was to delete everything from 'auto eth0' or 'iface eth0' to, but not including, 'auto eth?' or 'iface *' or 'EOF', and then append my own configuration onto the bottom. Is there a simple way to do this, or any suggestions about how to go about it?

Yes, I know that for this example, I could just 'sudo vim /etc/network/interfaces' and edit the file, but I would like to attempt to automate the process in C eventually.
 
Old 10-21-2008, 01:55 PM   #2
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 46
Moin,

to delete all lines from "auto eth0" to the next blank line you can use sed:
Code:
sed '/^auto eth0$/,/^$/d' file
To edit the file in place, use the -i option:
Code:
sed -i '/^auto eth0/,/^$/d' file
Jan
 
Old 10-21-2008, 09:56 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Just FYI, Perl is very good at this sort of stuff and less fiddly than C, but similar syntax.
If you're interested, see:

http://perldoc.perl.org/
http://www.perlmonks.org/?node=Tutorials
 
Old 10-23-2008, 05:24 PM   #4
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 46
Moin,

Quote:
Originally Posted by chrism01 View Post
Just FYI, Perl is very good at this sort of stuff and less fiddly than C, but similar syntax.
a little bit oversized for such a simple task, isn't it? But in principle I agree with you - Perl is also my favorite to process almost every kind of files.

Jan
 
  


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: read multiple lines in a file 300zxkyle Programming 7 07-29-2007 05:38 AM
multiple file editing using... sed? perl? bpk Linux - Software 2 10-07-2005 05:16 AM
[bash] removing or editing lines in file Erhnam Programming 12 03-15-2005 08:25 AM
batch editing multiple lines statmobile Programming 8 09-16-2004 09:05 PM
merge multiple lines of a single file into one line groverrajiv Linux - Newbie 4 05-26-2004 03:38 AM


All times are GMT -5. The time now is 06:37 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration