need to print lines between repeated pattern

Hi all,

I have a file that looks like this:

uid=bessemsj
version: 1
dn: cn=Desk SpecialAdminDesk, ou=Desks, dc=DSS,c=nl,o=Vodafone
dn: cn=DSS Advisors, ou=Groups, dc=DSS,c=nl,o=Vodafone
dn: cn=DSS Dispatcher,ou=Groups,dc=DSS,c=nl,o=Vodafone
dn: cn=Desk Retention Desk,ou=Desks, dc=DSS,c=nl,o=Vodafone
uid=nijss
version: 1
dn: cn=Desk SpecialAdminDesk, ou=Desks, dc=DSS,c=nl,o=Vodafone
dn: cn=DSS Advisors, ou=Groups, dc=DSS,c=nl,o=Vodafone
dn: cn=DSS Dispatcher,ou=Groups,dc=DSS,c=nl,o=Vodafone
dn: cn=Desk Retention Desk,ou=Desks, dc=DSS,c=nl,o=Vodafone
uid=langenbergr
version: 1
dn: cn=Desk SpecialAdminDesk, ou=Desks, dc=DSS,c=nl,o=Vodafone
dn: cn=DSS Advisors, ou=Groups, dc=DSS,c=nl,o=Vodafone
dn: cn=DSS Dispatcher,ou=Groups,dc=DSS,c=nl,o=Vodafone
dn: cn=Desk Retention Desk,ou=Desks, dc=DSS,c=nl,o=Vodafone
.
.
.

i am looping this file for every uid value, and need to separate lines below every matched uid into a file , who can i do this

Thanks in advance :slight_smile:

Eman

---------- Post updated at 12:34 AM ---------- Previous update was at 12:31 AM ----------

please note that the number of lines VARIES after every uid!

Hi.....

May i see the output wat u need ..

Thanks
Anusurya.A:b:

awk '/^uid=/{uid=$2;next}uid!=""{print > uid ".txt"}' FS='=' file

Thanks a lot for your help :slight_smile:
may i explain further:

Please note the following
1- i use a loop on the uid inside the file and i know its value , i.e please let me pass the uid value into the command (will you try it for me please on the sample file i put above)

2- all what i need please is to extract lines from uid till the next uid is met and direct the output into a temp file for a certain purpose(actually i don't need to create a separate file for it)

3- can you write the cmd specifically plz for uid=nijss , i.e plz show me how to get the lines:

uid=nijss
version: 1
dn: cn=Desk SpecialAdminDesk, ou=Desks, dc=DSS,c=nl,o=Vodafone
dn: cn=DSS Advisors, ou=Groups, dc=DSS,c=nl,o=Vodafone
dn: cn=DSS Dispatcher,ou=Groups,dc=DSS,c=nl,o=Vodafone
dn: cn=Desk Retention Desk,ou=Desks, dc=DSS,c=nl,o=Vodafone

Hi,

Please,
use code tags for cods and data sample.
use proper english.

awk '/uid=/{p=0;}/uid=nijss/{p=1;}p' file

I didn't understand your requirement. Please explain a bit, Why you looping you uid. do you need to ignore some uid?

Cheers,
Ranga :slight_smile:

actually i am looping on uid from another file and need to get the details related to this uid from the file i sent you , actually this is data related to LDAP.

unfortunatelly i am getting :

-bash-3.00$ awk '/uid=/{p=0;}/uid=nijss/{p=1;}p'  shortfile
awk: syntax error near line 1
awk: bailing out near line 1

where shortfile is my filel name , i there something missing? :S

---------- Post updated at 06:01 AM ---------- Previous update was at 06:00 AM ----------

Please note i am not ignoring some uid, i just need plz to run the command successfully on 1 uid, and the i will be able to handle the rest

Please use code tags for codes and data sample click here to know about code tags (video-tutorial)

May i know whats you OS and awk version..?
To get OS

uname -a

To get awk version

awk --version

I am getting right output with Linux and GNU Awk 3.1.5.

Cheers,
Ranga :slight_smile:

Use nawk or /usr/xpg4/bin/awk on Solaris.

Try something like this..

Var_a="bessemsj"
awk -v var="$Var_a" '/^uid=/{(s=0);if($2 == var){s=1}}{if(s == 1){ print;next}}' FS='=' file

using

/usr/xpg4/bin/awk -v var="$Var_a" '/^uid=/{(s=0);if($2 == var){s=1}}{if(s == 1){ print;next}}' FS='=' file

works pretty well !
Many thanks

It's working finally :slight_smile:

************************************************
DIFFERENCE FOUND FOR UID = cremersy :
************************************************
a17
 leg 1 is missing following line(s):
dn: cn=Desk ESSD,ou=Desks,dc=WEO,c=nl,o=Vodafone
-bash-3.00$ cat diffreport


************************************************
DIFFERENCE FOUND FOR UID = wasbauej :
************************************************
8
 leg 1 is missing following line(s):
dn: cn=WEO Advisors, ou=Groups, dc=WEO,c=nl,o=Vodafone

but something annoying me , there is a set of strings that appear above the word 'leg 1', becoz of an extra printing in a while loop

can u tell me please how to remove a line above matched word 'leg 1'

Must be having in the input..

Please check input first.

Script doesn't add any extra thing...:wink:

yes you are correct
i used the command you sent me-which was perfect-inside a loop
i need to remove one line above pattern 'leg1' plz advise

Try with this...:slight_smile:

/usr/xpg4/bin/awk -v var="$Var_a" '/^uid=/{(s=0);if($2 ~ var){s=1;p=NR}}{if(s == 1){if((p+2) != NR){ print;next}}}' FS='=' file