The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
regex to delete multiple blank lines in a file? fedora Shell Programming and Scripting 6 10-11-2007 04:36 PM
using sed command to delete a string spanning multiple lines radha.kalivar Shell Programming and Scripting 9 07-25-2007 10:26 AM
Delete multiple lines containting a variable string using SED. selkirk UNIX for Dummies Questions & Answers 2 04-27-2007 07:08 PM
delete multiple empty lines whatisthis Shell Programming and Scripting 3 11-09-2005 05:42 PM
Delete multiple lines w/ sed bookoo Shell Programming and Scripting 2 07-25-2003 10:03 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 5.00 average. Display Modes
  #1 (permalink)  
Old 10-12-2006
kangdom kangdom is offline
Registered User
  
 

Join Date: Oct 2006
Posts: 1
Post 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:

Code:
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.
  #2 (permalink)  
Old 10-12-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
nawk -v name='1testone' -f kang.awk myFile.txt

kang.awk:
Code:
/^[^ ]/ { found= ($1 == name) ? 1 : 0 }
!found
  #3 (permalink)  
Old 10-13-2006
ripat ripat is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2006
Location: Belgium
Posts: 438
Can not do better with sed (only worse).

Brilliant!
  #4 (permalink)  
Old 10-13-2006
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Code:
sed "/testtwo /,/Password/{/testtwo /d;/Password/!d;}" file
  #5 (permalink)  
Old 10-14-2006
petebear petebear is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 23
Quote:
Originally Posted by vgersh99
nawk -v name='1testone' -f kang.awk myFile.txt

kang.awk:
Code:
/^[^ ]/ { found= ($1 == name) ? 1 : 0 }
!found
Could someone explain this? I understand that "/^[^ ]/ " 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.
  #6 (permalink)  
Old 10-14-2006
petebear petebear is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 23
Quote:
Originally Posted by vgersh99
nawk -v name='1testone' -f kang.awk myFile.txt

kang.awk:
Code:
/^[^ ]/ { found= ($1 == name) ? 1 : 0 }
!found
Could someone explain this? I understand that "/^[^ ]/ " 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.

Last edited by petebear; 10-14-2006 at 06:26 PM..
  #7 (permalink)  
Old 10-16-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Code:
# 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

Last edited by vgersh99; 10-16-2006 at 12:05 PM..
Sponsored Links
Closed Thread

Bookmarks

Tags
linux

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:22 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0