Cut data and put it in next line

here is my sample file

dn: cn=Anandmohan Singh,ou=addressbook,dc=thbs,dc=com
objectClass: top
objectClass: person
objectClass: organizationalPerson
objectClass: inetOrgPerson
cn: Anandmohan Singh
givenName: Anandmohan
mail: anand_ms@thbs.com
mobile: 9986010455
ou: null
physicalDeliveryOfficeName: ST-6th Floor
sn: Singh
telephoneNumber: 41827200 Extn: 7400
title: Associate Software Engineer - Trainee
uid: 1432

dn: cn=Riteshkumar Mohanty,ou=addressbook,dc=thbs,dc=com
objectClass: top
objectClass: person
objectClass: organizationalPerson
objectClass: inetOrgPerson
cn: Riteshkumar Mohanty
givenName: Riteshkumar
mail: ritesh_m@thbs.com
mobile: 9916953109
ou: null
physicalDeliveryOfficeName: ST-6th Floor
sn: Mohanty
telephoneNumber: 41827200 Extn: 7400
title: Associate Software Engineer - Trainee
uid: 1418

i want to cut the ext from the line where telephone number is given and put it in next line,something like this--

dn: cn=Anandmohan Singh,ou=addressbook,dc=thbs,dc=com
objectClass: top
objectClass: person
objectClass: organizationalPerson
objectClass: inetOrgPerson
cn: Anandmohan Singh
givenName: Anandmohan
mail: anand_ms@thbs.com
mobile: 9986010455
ou: null
physicalDeliveryOfficeName: ST-6th Floor
sn: Singh
telephoneNumber: 41827200
Extn: 7400
title: Associate Software Engineer - Trainee
uid: 1432

dn: cn=Riteshkumar Mohanty,ou=addressbook,dc=thbs,dc=com
objectClass: top
objectClass: person
objectClass: organizationalPerson
objectClass: inetOrgPerson
cn: Riteshkumar Mohanty
givenName: Riteshkumar
mail: ritesh_m@thbs.com
mobile: 9916953109
ou: null
physicalDeliveryOfficeName: ST-6th Floor
sn: Mohanty
telephoneNumber: 41827200
Extn: 7401
title: Associate Software Engineer - Trainee
uid: 1418

If you put your file content in a file called "hi.txt" then his is the command to obtain what you required:

[ramki@lindesk3 ramki]$ cat hi.txt | grep -i "ext"
telephoneNumber: 41827200 Extn: 7400

[ramki@lindesk3 ramki]$ cat hi.txt | grep -i "ext" | sed 's/ Extn/\nExtn/g'
telephoneNumber: 41827200
Extn: 7400

Thanks,
Ramkrix

heres a guideline of what i think needs to be done.
have input file hi.txt and outputfile hi2.txt

read each line of input file and output to new file.
on each line do an awk:

eg.

cat hi.txt | awk '{print $1 " " $2 " " $3 " " $4 } '
telephone: 345345435 Extn: 4545

now u have four fields seperate.

then when $1 = "telephone:"
output $1 and $2 then next line
output $3 and $4.

and at the eof
mv hi2.txt to hi.txt

$ sed -i '/^telephoneNumber:/ s/Extn/\nExtn/' tel.txt

//Jadu

awk  '{
if($1=="telephoneNumber:" && $3=="Extn:")
printf("%s %s\n %s %s\n",$1,$2,$3,$4)
else
print
}' filename

thanks alot everyone,i got it working.
I appericiate the quick response of you guys.

Thanks...

Hope This should work

grep "Extn:" filename | awk '{printf("%s%s\n",$1,$2);printf("%s%s\n",$3,$4)}'

Regards,
aajan

i have the other file--

file2--

The first file is the first one which i mentioned earlier.

This is tricky to do with just shell tools. I would write a quick Perl script, or reformat the files. Would it make sense to keep the data in a tab-separated record-per-line format, at least for processing purposes? Or a simple SQLite database? That would make the rest trivial. (Speculating that you will have still more similar follow-up questions after we solve this one.)

i will apperciate if you help me with any script.

Thanks.

So do you expect more modifications to be necessary as this develops, or is this the last finishing touch? The most useful approach will depend a lot on that information.

Asuming it the last finishing touch,help me out in this.

Thanks.

I'd do this in Perl, although I suppose it could be accomplished in awk just as well.

  1. Read in the password file into an associative array.
  2. Parse the main file from top to bottom. When you see an uid for which you have the password, modify it before printing it out.

Make sure you don't forget to print the very last record.

i am not aware with perl scripting,i have never done it before so need your help in this regard.

Thanks..

Are you getting paid for trying to solve this?

I am not aware of the perl scripting and i am not getting paid for this.

So tell me, how come the sum total of your posts in these forums suggest that you are a new junior sysadmin in a fairly large organization? See e.g. Give the command names