ProgrammingThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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.
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...
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"
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.