LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 10-14-2006, 05:36 PM   #1
pete1234
Member
 
Registered: May 2005
Distribution: Slack, FreeBSD,NetBSD, OpenBSD, Open Solaris, Minix
Posts: 172

Rep: Reputation: 30
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.
 
Old 10-14-2006, 06:39 PM   #2
coaic
LQ Newbie
 
Registered: Apr 2006
Location: Australia
Distribution: Ubuntu, SuSE, FC, xBSD, Darwin/Fink
Posts: 13

Rep: Reputation: 0
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.
 
Old 10-14-2006, 07:05 PM   #3
pete1234
Member
 
Registered: May 2005
Distribution: Slack, FreeBSD,NetBSD, OpenBSD, Open Solaris, Minix
Posts: 172

Original Poster
Rep: Reputation: 30
Thanks coaic, think I've got it now.
 
  


Reply



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
Code explanation ... ? dbee Linux - Software 1 12-17-2005 01:06 AM
need clarification in this reg exp/awk code mselvam Linux - General 1 07-08-2005 03:57 PM
awk code on linux suchi_s Programming 6 01-13-2005 11:02 PM
fat fs code explanation ramya272 Programming 0 03-06-2004 09:55 AM
Pascal Code Explanation Gerardoj Programming 1 09-25-2003 01:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration