LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 11-04-2009, 05:36 AM   #1
ablanc
LQ Newbie
 
Registered: Apr 2008
Posts: 6

Rep: Reputation: 0
search and replace specific line in the file


Hi,

i have in my test.txt
[setting1]
enable=1

[setting2]
enable=1

[setting3]
enable=1

i want to create a script so i can change
e.g enable in [setting2] to 0

i want to put in cron, so the 'enable' can be changed automatically,
is there any way i can do that? I hope there is someone can help me

many thanks
 
Old 11-04-2009, 05:57 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
what have you tried?
 
Old 11-04-2009, 06:16 AM   #3
indiajoe
Member
 
Registered: Jan 2009
Location: India
Distribution: Porteus atma
Posts: 84

Rep: Reputation: 21
Use sed

Hi
Use sed comand in a script as follows
Code:
#!/bin/sh
sed '/setting2/{n;s/[01]/0/g };' test.txt > testTemp
mv testTemp test.txt
Give proper path to text files while running from cron.
Cheers
-indiajoe

Last edited by indiajoe; 11-04-2009 at 06:18 AM.
 
Old 11-04-2009, 09:23 PM   #4
ablanc
LQ Newbie
 
Registered: Apr 2008
Posts: 6

Original Poster
Rep: Reputation: 0
ghostdog74: I'm drawing a blank, i found out that i can search and edit using perl but, i will change all the enabled line

indiajoe: thanks this works really nice.

i wonder if there is any other way without creating a new file

many thanks
 
Old 11-04-2009, 09:34 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
sed '/setting2/,+1{/enable/s/.$/0/}' filename

For the range from "setting2", plus 1 line: on a line containing "enable", replace 1 character at the end of the line with "0"
 
Old 11-04-2009, 10:17 PM   #6
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 59
If your file looks the way your sample at the top does, then in Perl you can use paragraph mode to treat each block (items delimited by two newlines) as one pseudo-line. This lets you use regular expressions easily within the blocks.

Anyhow, this works and does in-place editing on the sample data you gave. You will get a back-up as test.txt.bak:
Code:
#!/usr/bin/env perl
use strict;
use warnings;

$^I = '.bak';   # enable in-place editing
$/ = '';        # enable paragraph mode - a line is now marked by \n\n

while (<>) {
    if (/setting2/) {
        s/enable=1/enable=0/;
    }
    print;
}
In this version, you have to specify the filename you want to run it on from the command line. That is, save it as 'fixer' and run it as perl fixer test.txt.

The other way to do it that leaps to my mind is to create a variable $last that tracks whether the last line you saw matched setting2. Only change enable=1 if that variable is switched on.

Last edited by Telemachos; 11-04-2009 at 10:19 PM.
 
Old 11-05-2009, 12:06 AM   #7
ablanc
LQ Newbie
 
Registered: Apr 2008
Posts: 6

Original Poster
Rep: Reputation: 0
Hi pixellany & Telemachos

thank you very much, both are very great idea,
let me try to put it in my script.

once again.. many thanks
 
  


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
Muilti-line text search/replace Mleahy Linux - Software 8 07-15-2008 09:58 AM
vim search and replace only one line hakmed Programming 5 11-07-2006 08:13 PM
SED - replace text in file on specific line 3saul Linux - Software 1 03-04-2006 08:01 PM
Search and Replace with multiple-line strings ChristianNerds.com Programming 4 08-21-2005 03:32 PM
trying to search and replace text file for single & multiple line breaks separately brokenfeet Programming 7 08-29-2003 02:56 PM


All times are GMT -5. The time now is 01:00 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration