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
jaduks
March 24, 2008, 2:14am
4
$ 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...
aajan
March 25, 2008, 12:48am
7
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--
Quote:
uid:Password
0050:thbs
1418:thbs
1432:thbs
i have to compare two files and where the uid is same i have to take the password and insert it above the telephone number.
the output should be like this--
uid : 1418 common so insert the password thbs above the line telephonenumber.
The first file is the first one which i mentioned earlier.
era
March 25, 2008, 6:05am
9
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.
era
March 25, 2008, 7:02am
11
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.
era
March 25, 2008, 7:14am
13
I'd do this in Perl, although I suppose it could be accomplished in awk just as well.
Read in the password file into an associative array.
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..
era
March 25, 2008, 7:22am
15
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.
era
March 25, 2008, 7:59am
17
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