Need to delete multiple lines in a file.

Hi,

I'm new to this forum, and searched through the previous posts, but didn't see anything close enough to what i'm looking for.

I have a radius file like this:

testone          Password = "11111"
                    Service-Type = "Framed-User",
                    Session-Timeout = "9000",
                    Ascend-Idle-Limit = "900"
testtwo          Password = "11111"
                    Service-Type = "Framed-User",
                    Session-Timeout = "9000",
                    Ascend-Idle-Limit = "900"
testthree        Password = "11111"
                    Framed-Address = "000.000.000.000",
                    Framed-Netmask = "255.255.255.0"
                    Ascend-Route-IP=Route-IP-Yes
                    Framed-Route = "000.000.000.000/29 000.000.000.000 1 n"
1testone        Password = "11111"
                    Service-Type = "Framed-User",
                    Ascend-Idle-Limit = "900"
testtwo5        Password = "11111"
                    Service-Type = "Framed-User",
                    Session-Timeout = "9000",
                    Ascend-Idle-Limit = "900"
###EOF###

I need to search for an exact match of the username such as testtwo and delete that line and all the lines below it until I reach the next username.

The main problem is there is no set number of lines below the username, it could be anywhere from 2-8 lines.

The only constants are:

The username will allways be at the beginning of a new line and the word password will always and only be on the same line as the username.

I'm on a linux server, so any combination of sed, awk or perl should work.

Thanks in advance for any help.

nawk -v name='1testone' -f kang.awk myFile.txt

kang.awk:

/^[^ ]/ { found= ($1 == name) ? 1 : 0 }
!found

Can not do better with sed (only worse).

Brilliant!

sed "/testtwo /,/Password/{/testtwo /d;/Password/!d;}" file

Could someone explain this? I understand that "/[1]/ " grabs all the usernames. and I think I understand the ternary operator; found equals true (1) if name (the username) is found in $1, or found equals false (0) if it's not. Then I get a bit lost, I think !found toggles the value of found, but why? And how does it all end up removing the username and all text between it and the next username. Thanks, pete.


  1. ^ ↩︎

Could someone explain this? I understand that "/[1]/ " grabs all the usernames. and I think I understand the ternary operator; found equals true (1) if name (the username) is found in $1, or found equals false (0) if it's not. Then I get a bit lost, I think !found toggles the value of found, but why? And how does it all end up removing the username and all text between it and the next username.


  1. ^ ↩︎

# for lines NOT starting with a 'space'....
# set 'found' to '1' if if the first field is 'name' and to '0' otherwise
/^[^ ]/ { found= ($1 == name) ? 1 : 0 }

# if 'found == 0' - print the current line. could be rewritten as 'found == 0 {print $0}'
!found