sed script to print a value from txt file

Hello guys,

I would appreciate if someone can help me to write a shell script using sed. From a larget text file I need to print a fixed value of a word. In another words whenever it finds that word, it needs to grab the other line containing "dn" and prints its value. For example:

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

--------------------

in this example txt there are thousands of similar ones.

I need to be able to print "cquestion" and related "dn" for it using sed in a shell script. I would appreciate if someone can help me and email at "cmontr@verizon.com"

Thank you very much

This looks more like a job for awk, but it's not entirely clear what you want.

Do you want to print any line containing "cquestion:" and the earlier line containing "dn:"?

awk '
   /^dn:/ { dn = $0 }
   /^cquestion:/ { print dn; print }' FILE

Hi again, I appreciate for looking at my question. The answer is yes. I would like to see and print cquestion, and whenever it finds cquestion then it goes back and find the dn value for that cquestion. Thank you so much if you can help.

Hi cfa - I tested that awk - works great! Thank you so much. I was wondering if there is a way you may know to use sed or perl ? I really apprecaite for the help.

input(a):

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: leo210375

output:

cquestion: xyf88561 ------> dn: uid=poi.1111,ou=abc,o=cc.com
cquestion: leo210375 ------> dn: uid=poi.2222,ou=def,o=dd.com

code:

awk '
{
if (index($0,"dn")!=0)
str=$0
if (index($0,"cquestion")!=0)
{
print $0,"------>",str
str=""
}
}' a

I do appreciate for the script...If possible would you also let me know how I can pass the paremeter for cquestion ? For example there may be many different questions and I would like print for certain ones needed..because log (txt) file can be huge. I appreciate in advance. If anyone else has different methods that can help, would also be appreciated. Thanks again.

Could anyone please help me to pass a parameter 'cquestion' to extract data from a txt file from this code ? I really appreciate. I wanted to print cquestions when needed because the txt file can be over 8GB. Not want to print all. Thanks much...
----------------------------

awk '
{
if (index($0,"dn")!=0)
str=$0
if (index($0,"cquestion")!=0)
{
print $0,"------>",str
str=""
}
}' a

Hello all, and I appreciate for the help...

Previously I asked similar questions but with a different requirement now...Can someone please help me to print cquestion and its value by passing parameter that way it doesnt print the whole big file over 8GB...Any challenges would be really appreciated...Thanks much...

Here is the chunk of the txt file contains an example of cquestion and the dn value of it :

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: leo210375