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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
10-14-2006, 05:36 PM
|
#1
|
|
Member
Registered: May 2005
Distribution: Slack, FreeBSD,NetBSD, OpenBSD, Open Solaris, Minix
Posts: 172
Rep:
|
awk code explanation
This awk code is suppose to remove the username and all fields related to that user:
Code:
nawk -v name='1testone' -f kang.awk myFile.txt
/^[^ ]/ { found= ($1 == name) ? 1 : 0 }
!found
Here's the file:
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"
So, 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.
|
|
|
|
10-14-2006, 06:39 PM
|
#2
|
|
LQ Newbie
Registered: Apr 2006
Location: Australia
Distribution: Ubuntu, SuSE, FC, xBSD, Darwin/Fink
Posts: 13
Rep:
|
awk code explanation
found remains set to 0 or 1 until the RE /^[^ ]/ matches again on a subsequent line in the data file. An expression in a script line that evaluates non zero results in the line being printed. You can check this by trying the following:
nawk '1' myFile.txt # all lines in the file are printed
nawk '0' myFile.txt # no lines in the file are printed
The RE in question only matches lines that have at least the first character in the line non blank.
Remember awk is a report scripting tool - all patterns in a script file are applied sequentially to each line as it is read from the data file with the exception of the BEGIN and END patterns which match before the first line of the data file and after the last line of the data file, respectively.
Last edited by coaic; 10-14-2006 at 06:41 PM.
|
|
|
|
10-14-2006, 07:05 PM
|
#3
|
|
Member
Registered: May 2005
Distribution: Slack, FreeBSD,NetBSD, OpenBSD, Open Solaris, Minix
Posts: 172
Original Poster
Rep:
|
Thanks coaic, think I've got it now.
|
|
|
|
| Thread Tools |
Search 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
HTML code is Off
|
|
|
All times are GMT -5. The time now is 05:03 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|