LinuxQuestions.org
Visit the LQ Articles and Editorials section
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
 
LinkBack Search this Thread
Old 11-15-2007, 09:19 PM   #1
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Rep: Reputation: 15
awk question


Hello everyone:

I print some value from this code...but I want to print the the second row for the found value. with using awk, how could I do that?

Code:

BEGIN{ headingFound = 0; }
/^cquestion: What is your name?/ { headingFound = 1; }
/^dn:/ {
if( headingFound == 1 ){
print $2 $3 ;
headingFound = 0;
}
}

Output:

uid=TEST418,ou=ESERVICES,o=C.com
uid=TEST011,ou=ESERVICES,o=C.com
.
.
.

What I would like to print is for eash uid there is a usrstatus which is on the second line of each uid.

I appreciate if you can help.
 
Old 11-16-2007, 01:59 AM   #2
neek
LQ Newbie
 
Registered: Nov 2007
Posts: 6

Rep: Reputation: 0
Quote:
Originally Posted by cmontr View Post
Hello everyone:

I print some value from this code...but I want to print the the second row for the found value. with using awk, how could I do that?
I don't think awk ever considers more than one row at once, so you would probably have to use a little "printNextRow" flag, which you toggle on/off, e.g. in pseudo-code

BEGIN: printNextRow=0;
for every row considered, if printNextRow=1, print this row, and set printNextRow=0;
if match row to print, print that row, and set printNextRow=1;

If you find you want to print other rows, you might need a more complicated system. You could collect each row you're interested in in an array, and print the entire array of rows when you match a "beginning of record" row. I do this using Perl when collating ldif output from ldap.

for each row of input:

if beginning of record, e.g. match '^dn\:'
print entire array
empty array
fi

if row matches interesting row:
store row in array
fi

end loop

You might want to transform each row, either when you store it in the array, or when you print it. Of course in Perl I use a hash, not an array, but any collection would do.

Hope that helps With some real input data from you, some non-pseudo-code might be forthcoming :P
 
Old 11-16-2007, 02:15 AM   #3
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Original Poster
Rep: Reputation: 15
I guess I got confused...can you put an example code ? Thanks.
 
Old 11-16-2007, 02:27 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,361

Rep: Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910
Or simply
Code:
BEGIN{ headingFound = 0 }
/^cquestion: What is your name?/ { headingFound = 1 }
/^dn:/ {
   if( headingFound == 1 ){
      getline
      print $2 $3 ;
      headingFound = 0
   }
}
 
Old 11-16-2007, 02:36 AM   #5
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Original Poster
Rep: Reputation: 15
Would yo be more specific by saying "getline" ? Thanks...
 
Old 11-16-2007, 03:01 AM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,361

Rep: Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910
Come on... when you are given a suggestion or a solution, you can at least look at the documentation or manual pages! This is from "GAWK: Effective AWK Programming", the official guide of the GNU awk:
Quote:
The getline command can be used without arguments to read input from the current input file. All it does in this case is read the next input record and split it up into fields. This is useful if you’ve finished processing the current record, but want to do some special processing on the next record right now.
 
Old 11-16-2007, 03:11 AM   #7
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Original Poster
Rep: Reputation: 15
I am sorry ...I will investigate once again.
 
Old 11-16-2007, 03:26 AM   #8
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,361

Rep: Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910
No need to be sorry. I hope that usage of getline (without arguments) is clear now: it simply skips to the next line of the input file so that you can print it instead of the current one.
 
Old 11-16-2007, 04:17 AM   #9
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Original Poster
Rep: Reputation: 15
I appreciate for the help...although I got it working, it didnt solve what I was looking for....perhaps you can help...

in a txt file I have (I small piece of it)

dn: uid=SOMEID1,ou=ESERV,o=e.com
question: What is your pet's name?
userstatus: 0
dn: uid=SOMEID2,ou=someConsumer,o=e.com
question: What is your mother's maiden name?
userstatus: 0


What I was trying to print in the code I provided earlier is the one specific question's dn value and userstatus below it...code prints the dn section only...Any ideas would be appreciated...
 
Old 11-17-2007, 10:25 AM   #10
neek
LQ Newbie
 
Registered: Nov 2007
Posts: 6

Rep: Reputation: 0
Quote:
Originally Posted by cmontr View Post
What I was trying to print in the code I provided earlier is the one specific question's dn value and userstatus below it...code prints the dn section only...Any ideas would be appreciated...
Are you getting the data from an LDAP database? Sounds like you'd be better off using ldapsearch to retrieve only the objects you want, and only the attributes from those objects that you want.

The format of ldapsearch is probably:

ldapsearch <logon etc. arguments> <filter> <attributes>

Check 'man ldapsearch' on your platform.

e.g. this would print the 'dn' and 'userstatus' attributes for all objects matching the search filter "question=search question"

ldapsearch -h hostname -w password -D cn=someone,o=somewhere -b o=e.com "question=search question" dn userstatus

Is that relevant?
 
Old 11-18-2007, 08:02 AM   #11
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Original Poster
Rep: Reputation: 15
true, thanks for the information. I did use the ldapsearch and got it working fine. However, I am trying to not to touch dbb and use the log file from backup archieves. That was the reason working around with shell scripts etc. Whenever you have time we can talk about it if anyone likes. I wish there was a chat session with online users. Please let me know if anyone knows if there is a chat place for IT people. Thanks again.
 
Old 11-19-2007, 03:19 AM   #12
neek
LQ Newbie
 
Registered: Nov 2007
Posts: 6

Rep: Reputation: 0
Quote:
Originally Posted by cmontr View Post
I did use the ldapsearch and got it working fine. However, I am trying to not to touch dbb and use the log file from backup archieves.
Sorry, that sounds a bit crazy to me. If you have a database, query it. It's the easiest way forward, and would be using the right tools for the right purpose.

I'd have to re-learn awk to produce your code for you, and I just don't have the time or the patience. My initial pseudocode wasn't quite right for you, partly because my awk is very rusty, and partly because you hadn't fully explained your problem. As colucix said, please read the manuals, learn about getline, investigate, and try some attempts yourself. Try googling about awk and multiline data. I really think you'll learn the most by figuring it out yourself, using the information you have so far and the right approach to programming.

My general approach is based around analysing each row one at a time. You collect each interesting line of data, and when you recognise the end of a record, by which I mean several lines of ldif and not the awk notion of a one-line record, you output all those interesting lines of data, or do something more clever like collate them into one line of output. You might want to match an empty line as the end-of-record, or you might find it's easier to match the beginning of a record with "^dn" and output the previous record at that point, but then you have a problem with the last record, as there is no "start of next record" to match.

The getline approach is probably simpler. At the start of each record from ldif, i.e. when you see "^dn", do all processing for that record in one block i.e. code inside curly brackets { }. Use getline to fetch "the next" line into $0, and process it how you want.

Pick an approach, and start experimenting. Don't be afraid to start from an empty awk script if you feel things are getting too confusing. Just keep adding one more piece of the puzzle until you get where you're trying to go.

Last edited by neek; 11-19-2007 at 03:39 AM.
 
Old 11-19-2007, 11:35 AM   #13
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,950
Blog Entries: 11

Rep: Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860
Quote:
Originally Posted by cmontr View Post
Hello everyone:

I print some value from this code...but I want to print the the second row for the found value. with using awk, how could I do that?

Code:

BEGIN{ headingFound = 0; }
/^cquestion: What is your name?/ { headingFound = 1; }
/^dn:/ {
if( headingFound == 1 ){
print $2 $3 ;
headingFound = 0;
}
}

Output:

uid=TEST418,ou=ESERVICES,o=C.com
uid=TEST011,ou=ESERVICES,o=C.com
.
.
.

What I would like to print is for eash uid there is a usrstatus which is on the second line of each uid.

I appreciate if you can help.

Why don't you use the method I pointed out to you
in the other thread ?
I clearly pointed out to you HOW to use awk to work
on multi-line records rather than individual lines.



Cheers,
Tink
 
  


Reply

Tags
awk, multiline


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
an awk question zoshr Programming 5 04-13-2007 05:29 AM
awk question davidkline Linux - General 9 09-29-2006 12:37 PM
question about awk perfect_circle Programming 4 07-19-2006 02:34 PM
awk question puishor Programming 2 08-26-2005 09:44 AM
AWK question paraiso Linux - Newbie 5 05-12-2005 01:37 PM


All times are GMT -5. The time now is 04:17 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration