LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial 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 09-21-2007, 12:20 PM   #16
theNbomr
Senior Member
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 4,506

Rep: Reputation: 602Reputation: 602Reputation: 602Reputation: 602Reputation: 602Reputation: 602

cmontr:
Let me see if I can rephrase your question in a way that we can all understand unambiguously. Your ldap database contains records named 'cquestion', which have some values which may or may not be unique. You want the corresponding 'dn' records to be reported, but only for those 'cquestion' records which are unique. You do not want any of the 'cquestion' records reported. Does this accurately express your requirements?

Can someone who understands the ldap database confirm that the records in the database are organized in groups starting with a 'dn' record, and ending with a 'cquestion' record? The snippet of text given in the example would seem to indicate this, and if it is the case, it should simplify the problem.

--- rod.
 
Old 09-21-2007, 03:28 PM   #17
cmontr
Member
 
Registered: Sep 2007
Posts: 172

Original Poster
Rep: Reputation: 15
need help for the next level

Hi everyone...first thank you very much for helping out. I got this running but need to iterate now:

#
# LQcmontr.awk
#
BEGIN{ headingFound = 0; }
/^cquestion: What's your name/ { headingFound = 1; }
/^dn:/ {
if( headingFound == 1 ){
print $2;
headingFound = 0;
}
}

# awk -f LQcmontr.awk bigFile.txt


#######################

Ok this runs ok. Now I want to print this in another format - in another words I want to give an input as cquestion when I run the script. When I ran the script is should ask me something like : "Enter your cquestion: " And when I enter, it should do the printing for its dn value which is working. I appreciate if anyone is familiar with it and can help me. Thanks million again.
 
Old 09-21-2007, 08:37 PM   #18
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 237Reputation: 237Reputation: 237
Quote:
Originally Posted by cmontr View Post
cquestion when I run the script. When I ran the script is should ask me something like : "Enter your cquestion: " And when I enter, it should do the printing for its dn value which is working. I appreciate if anyone is familiar with it and can help me. Thanks million again.
add this in the BEGIN
Code:
awk 'BEGIN{ headingFound = 0; 
     print "what is cquestion? "  
     getline choice < "/dev/tty" 
}
...
then use choice in your code.
 
Old 09-21-2007, 09:07 PM   #19
theNbomr
Senior Member
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 4,506

Rep: Reputation: 602Reputation: 602Reputation: 602Reputation: 602Reputation: 602Reputation: 602
Quote:
Originally Posted by ghostdog74 View Post
add this in the BEGIN
Code:
awk 'BEGIN{ headingFound = 0; 
     print "what is cquestion? "  
     getline choice < "/dev/tty" 
}
...
then use choice in your code.

In awk, how does one match against the value of a variable?
If I do
Code:
/^cquestion: choice/  { handle specified choice here...
awk will not interpolate the variable 'choice', but will simply take it as the literal word. Perl makes this kind of trick easy.
--- rod.
 
Old 09-21-2007, 09:57 PM   #20
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 237Reputation: 237Reputation: 237
Quote:
Originally Posted by theNbomr View Post
In awk, how does one match against the value of a variable?
If I do
Code:
/^cquestion: choice/  { handle specified choice here...
awk will not interpolate the variable 'choice', but will simply take it as the literal word.
Code:
   awk 'BEGIN{ headingFound = 0; 
		print "what is cquestion? "
		getline choice < "/dev/tty" 
		toMatch = "cquestion: "choice
   }
$0 ~ toMatch { headingFound = 1;}
....
...
Quote:
Perl makes this kind of trick easy.
--- rod.
Probably because Perl takes the best of sed, c, and awk, lump them together into a powerful language to make it easier for people to program with..
 
Old 09-22-2007, 01:51 PM   #21
jschiwal
Moderator
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,263

Rep: Reputation: 562Reputation: 562Reputation: 562Reputation: 562Reputation: 562Reputation: 562
Quote:
Originally Posted by cmontr View Post
Hi - I am sorry but that line did not work....grep: can't open -B
Are you running on a Unix or BSD machine. Maybe the -B (as in before) option doesn't exist in your version of grep. My example doesn't place the dn: after cquestion:. To do that you could use sed, using the hold register. This is where you would be better off writing a sed script and running the command like
sed -f dn_cquestion.sed ldapjunk.txt
Where dn_cquestion is a sed script. Also if there are dn's without corresponding cquestion's then you may need to branch out when another dn is read.

The exceptions are what can make using sed very complicated. Often when you have patterns over a range of files, you need to use the advanced options and build up lines in the input register or use the hold register.
 
Old 11-01-2007, 12:37 PM   #22
cmontr
Member
 
Registered: Sep 2007
Posts: 172

Original Poster
Rep: Reputation: 15
need help pls

regarding to the same issue, I need to be able to print the whol record for the cquestion. Could anyone help me how ? Thanks very much.
 
Old 11-01-2007, 01:14 PM   #23
cmontr
Member
 
Registered: Sep 2007
Posts: 172

Original Poster
Rep: Reputation: 15
in another words, how can I print this from a "cquestion: what is xyz? " -- please let me know if anyone can help...thanks in advance...

and for every "what is xyz" print this:

dn: uid=poi.1111,ou=abc,o=cc.com
authpassword;orclcommonpwd: {SHA}adhnbckhsypzx=
businesscategory: abc
createtimestamp: 20050709198316x
creatorsname: cn=bulkload
modifiersname: cn=bulkload
modifytimestamp: 20050709198316x
objectclass: top
objectclass: cuser
cquestion: xyf88561
dn: uid=poi.2222,ou=def,o=dd.com
authpassword;orclcommonpwd: {SHA}adhnbckhsypzx=
businesscategory: abc
createtimestamp: 20050709198316x
creatorsname: cn=bulkload
modifiersname: cn=bulkload
modifytimestamp: 20050709198316x
objectclass: top
objectclass: cuser
cquestion: what is xyz?
 
Old 11-01-2007, 02:22 PM   #24
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,627
Blog Entries: 10

Rep: Reputation: 776Reputation: 776Reputation: 776Reputation: 776Reputation: 776Reputation: 776Reputation: 776
I've read the whole thread and still don't fully comprehend what you're
after ... does this help at all?


I assume that your file is proper LDIF?
Code:
awk 'BEGIN{RS=ORS="\n\n"; FS=OFS="\n"} /cquestion: what?/ {print $0}'

Cheers,
Tink
 
Old 11-01-2007, 02:35 PM   #25
cmontr
Member
 
Registered: Sep 2007
Posts: 172

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Tinkster View Post
I've read the whole thread and still don't fully comprehend what you're
after ... does this help at all?


I assume that your file is proper LDIF?
Code:
awk 'BEGIN{RS=ORS="\n\n"; FS=OFS="\n"} /cquestion: what?/ {print $0}'

Cheers,
Tink
========================

Hi - What I am trying to print out every field for the particular question in the file...earlier I was printing only dn value for the asked question in this file...I tried your script but did not work. Could you please take a look again? thank you very much. This was the old code to print dn from the question in the txt file.


#!/bin/sh
# ctest.awk
#
BEGIN{ headingFound = 0; }
/^cquestion:/ { headingFound = 1; }
/^dn:/ {
if( headingFound == 1 ){
print $2 $3;
headingFound = 0;
}
}

# awk -f test2.awk ab.txt > test2.txt
 
Old 11-01-2007, 02:45 PM   #26
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,627
Blog Entries: 10

Rep: Reputation: 776Reputation: 776Reputation: 776Reputation: 776Reputation: 776Reputation: 776Reputation: 776
What did it give you? And where did you create the file? Is it using
Unix or DOS line-ends? If it's from DOS my thing won't work - convert
it to Unix first ...


Cheers,
Tink
 
Old 11-01-2007, 02:51 PM   #27
cmontr
Member
 
Registered: Sep 2007
Posts: 172

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Tinkster View Post
What did it give you? And where did you create the file? Is it using
Unix or DOS line-ends? If it's from DOS my thing won't work - convert
it to Unix first ...


Cheers,
Tink
================

It is in unix - solaris os.

i put that in a script

#!/usr/bin/awk -f

awk 'BEGIN{RS=ORS="\n\n"; FS=OFS="\n"} /cquestion: What is your name?/ {print $0}'

and ran as:

awk -f test.awk a.txt > test.txt where a.txt contains the data file and test.txt is to be the extracted file

error I got was

awk: syntax error near line 3
awk: bailing out near line 3


I appreciate for the help...

Last edited by cmontr; 11-01-2007 at 02:53 PM.
 
Old 11-01-2007, 02:57 PM   #28
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,627
Blog Entries: 10

Rep: Reputation: 776Reputation: 776Reputation: 776Reputation: 776Reputation: 776Reputation: 776Reputation: 776
OK ... use the GNU awk instead of the Solaris default one - if it's not installed
(yet) you can grab if from sun's download site. The thing that comes with Solaris
by default is cr*p.


Cheers,
Tink
 
Old 11-01-2007, 03:02 PM   #29
cmontr
Member
 
Registered: Sep 2007
Posts: 172

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Tinkster View Post
OK ... use the GNU awk instead of the Solaris default one - if it's not installed
(yet) you can grab if from sun's download site. The thing that comes with Solaris
by default is cr*p.


Cheers,
Tink
==========

I am sorry but I only have user account on the server as oracle

is there any other way to resolve this?

Thanks much for the help
 
Old 11-01-2007, 03:20 PM   #30
cmontr
Member
 
Registered: Sep 2007
Posts: 172

Original Poster
Rep: Reputation: 15
thanks for the time....I got it fixed...here it worked as ...

BEGIN { RS = ""; FS = "\n"; IGNORECASE=1 }

/cquestion: What?{ print $0, "\n" }
 
  


Reply


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
awk and/or sed linux2man Linux - General 7 01-22-2007 10:02 AM
Sed and Awk Gins Programming 7 04-19-2006 10:32 AM
Sed/Awk command help needed. farmerjoe Programming 3 03-02-2005 11:13 AM
sed or awk help requested tonyfreeman Programming 7 10-03-2004 12:23 AM
awk/sed help pantera Programming 1 05-13-2004 11:59 PM


All times are GMT -5. The time now is 02:57 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
Open Source Consulting | Domain Registration