Visit Jeremy's Blog.
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 11-04-2009, 04:36 AM   #1
LQ Newbie
Registered: Apr 2008
Posts: 6

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


i have in my test.txt



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, 04:57 AM   #2
Senior Member
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

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

Rep: Reputation: 21
Use sed

Use sed comand in a script as follows
sed '/setting2/{n;s/[01]/0/g };' test.txt > testTemp
mv testTemp test.txt
Give proper path to text files while running from cron.

Last edited by indiajoe; 11-04-2009 at 05:18 AM.
Old 11-04-2009, 08:23 PM   #4
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, 08:34 PM   #5
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 734Reputation: 734Reputation: 734Reputation: 734Reputation: 734Reputation: 734Reputation: 734
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, 09:17 PM   #6
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:
#!/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/) {
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 09:19 PM.
Old 11-04-2009, 11:06 PM   #7
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


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

All times are GMT -5. The time now is 01:16 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