How to replace a range of text with sed or awk?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to replace a range of text with sed or awk?
# 1  
Old 04-15-2009
How to replace a range of text with sed or awk?

Howdy!

I'm trying to automate editing of a configuration file (custom.conf for GDM). I need to find every line between a line that starts with "[daemon]" and the next line that starts with "[". When I find "[daemon]", I want to preserve that line, but then delete all the lines in that configuration section and then insert several new lines of text between the first match ("[daemon]") and the second match ("]").

I suspect that I can do this with sed or at least awk, but I'm not quite sure where to start Can anyone suggest which tool might be better suited to this task and provide some code that accomplishes what I'm talking about?

Thanks,
Tad
# 2  
Old 04-15-2009
Show an example please and use CODE tags, ty.
# 3  
Old 04-15-2009
I have the following file:

Code:
# Some comments
# S'more comments

[daemon]
TimedLoginEnable=true
TimedLoginDelay=10
TimedLogin=user1
# and maybe some other, unpredictable lines that I don't need or want

[security]

[xdmcp]

[gui]
...

I wish for it to become something like
Code:
# Some comments
# S'more comments

[daemon]
TimedLoginEnable=true
TimedLoginDelay=5
TimedLogin=user2

[security]

[xdmcp]

[gui]
...

So far, I have managed to come up with this awk script:
Code:
BEGIN {
    searching = 1; 
    done = 0
}
searching == 1 {
    print;
    if ($1 == "[daemon]") {
        searching = 0;
        print "TimedLoginEnable=true";
        print "TimedLoginDelay=5";
        print "TimedLogin=user2";
        print "";
    }
}
searching == 0 {
    if (done) {
        print;
    } else {
        getline;
        if (match($0, /^\[/)) {
            print;
            done = 1;
        }
    }
}

But I feel like the awk script should be more concise, and I also suspect that this can be done in sed, but I don't even know where to start on that one.

Thanks,
Tad

Last edited by TXTad; 04-15-2009 at 02:41 AM.. Reason: fixed code typo
# 4  
Old 04-15-2009
Clearing the lines between [daemon] and the next starting with [...
Code:
awk '/^\[daemon\]$/ {print; i=1; next} /^\[/ && i == 1 {print; i=0; next} i == 1 {next} i == 0 {print}' infile> outfile

Having your new values saved in an extra file called "replacement"; adding it's contents and writing it to "newfile":
Code:
awk '/^\[daemon]/ { print; while ((getline _ < "replacement") > 0) {print _} } {print} ' outfile > newfile

Thanks for the last part to prevent writing the last line 2 times goes to radoulov taken from this thread https://www.unix.com/shell-programmin...ther-file.html
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Help with awk or sed Command to Replace Text in Files

Hello Everyone, I have many files like so: file1.txt file2.txt file3.txt Within each file I have many lines of random text separated by commas like so: abcAAA,123,defAA,456777,ghiA,789 jklB,101,mnoBBB,11211,pqrB,13111 stuCC,415,vwxCCCC,161,yzaC,718 I am trying to use SED or AWK to... (4 Replies)
Discussion started by: D3U5X
4 Replies

2. UNIX for Beginners Questions & Answers

Sed/awk to delete a regex between range of lines

Hi Guys I am looking for a solution to one problem to remove parentheses in a range of lines. Input file module bist_logic_inst(a, ab , dhd, dhdh , djdj, hdh, djjd, jdj, dhd, dhp, dk ); input a; input ab; input dhd; input djdj; input dhd; output hdh; output djjd; output jdj;... (5 Replies)
Discussion started by: kshitij
5 Replies

3. Shell Programming and Scripting

sed or awk to remove specific column to one range

I need to remove specific column to one range source file 3 1 000123456 2 2 000123569 3 3 000123564 12 000123156 15 000125648 128 000125648 Output required 3 000123456 2 000123569 3 000123564 12 000123156 15 000125648 128 000125648 (6 Replies)
Discussion started by: ranjancom2000
6 Replies

4. Shell Programming and Scripting

awk to print text in field if match and range is met

In the awk below I am trying to match the value in $4 of file1 with the split value from $4 in file2. I store the value of $4 in file1 in A and the split value (using the _ for the split) in array. I then strore the value in $2 as min, the value in $3 as max, and the value in $1 as chr. If A is... (6 Replies)
Discussion started by: cmccabe
6 Replies

5. Shell Programming and Scripting

Get range out using sed or awk, only if given pattern match

Input: START OS:: UNIX Release: xxx Version: xxx END START OS:: LINUX Release: xxx Version: xxx END START OS:: Windows Release: xxx Version: xxx ENDHere i am trying to get all the information between START and END, only if i could match OS Type. I can get all the data between the... (3 Replies)
Discussion started by: Dharmaraja
3 Replies

6. Debian

Using awk and sed to replace text

Good Day Every one I have a problem finding and replacing text in some large files that will take a long time to manually edit. Example text file looks like this #Example Large Text File unix linux dos squid bind dance bike car plane What im trying to do is to edit all the... (4 Replies)
Discussion started by: linuxjunkie
4 Replies

7. UNIX for Dummies Questions & Answers

Using sed to replace a range of number

Trying to use SED to replace numbers that fall into a range but can't seem to get the logic to work and am wondering if SED will do this. I have a file with the following numbers 3 26 20 5. For the numbers that are greater than zero and less than 25, SED would add the word range after the... (7 Replies)
Discussion started by: jimmyf
7 Replies

8. Shell Programming and Scripting

sed replace range of characters in each line

Hi, I'm trying to replace a range of characters by their position in each line by spaces. I need to replace characters 95 to 145 by spaces in each line. i tried below but it doesn't work sed -r "s/^(.{94})(.{51})/\ /" inputfile.txt > outputfile.txt can someone please help me... (3 Replies)
Discussion started by: Kevin Tivoli
3 Replies

9. Shell Programming and Scripting

using sed/awk to replace a block of text in a file?

My apologies if this has been answered in a previous post. I've been doing a lot of searching, but I haven't been able to find what I was looking for. Specifically, I am wondering if I can utilize sed and/or awk to locate two strings in a file, and replace everything between those two strings... (12 Replies)
Discussion started by: kiddsupreme
12 Replies

10. Shell Programming and Scripting

sed/awk date range?

Hi, I am trying to grep out a date range in an access log file. I defined the date like so; DATE1=$(date --date '1 hour ago' '+%m/%d/%y:%H:%M:%S') DATE2=$(date '+%m/%d/%y:%H:%M:%S') Then I just used cat to get the hits to the url into a results.txt; touch /tmp/results.txt cat... (7 Replies)
Discussion started by: Epx998
7 Replies
Login or Register to Ask a Question