The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Replacing the last data of each line ina file jisha Shell Programming and Scripting 6 2 Weeks Ago 04:47 AM
How to combine text data into one line? rcky_mntere Shell Programming and Scripting 2 05-02-2008 10:48 AM
get data from next line whamchaxed UNIX for Dummies Questions & Answers 5 12-04-2007 03:27 PM
add data from command line to end of file bryan UNIX for Dummies Questions & Answers 3 05-23-2006 03:57 PM
Extracting data from each line csaha Shell Programming and Scripting 1 04-26-2006 08:49 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-23-2008
Registered User
 

Join Date: Aug 2007
Location: Bangalore
Posts: 285
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
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-23-2008
Registered User
 

Join Date: Dec 2007
Location: TamilNadu,INDIA
Posts: 46
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

Last edited by ramkrix; 03-23-2008 at 09:59 PM.
Reply With Quote
  #3 (permalink)  
Old 03-23-2008
Registered User
 

Join Date: Mar 2008
Posts: 3
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
Reply With Quote
  #4 (permalink)  
Old 03-23-2008
jaduks's Avatar
Registered User
 

Join Date: Aug 2007
Location: Assam,India
Posts: 141
Code:
$ sed -i '/^telephoneNumber:/ s/Extn/\nExtn/' tel.txt
//Jadu
Reply With Quote
  #5 (permalink)  
Old 03-23-2008
Registered User
 

Join Date: Jun 2007
Posts: 335
Code:
awk  '{
if($1=="telephoneNumber:" && $3=="Extn:")
printf("%s %s\n %s %s\n",$1,$2,$3,$4)
else
print
}' filename
Reply With Quote
  #6 (permalink)  
Old 03-24-2008
Registered User
 

Join Date: Aug 2007
Location: Bangalore
Posts: 285
thanks alot everyone,i got it working.
I appericiate the quick response of you guys.

Thanks...
Reply With Quote
  #7 (permalink)  
Old 03-24-2008
Registered User
 

Join Date: Jun 2007
Posts: 73
Hope This should work

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


Regards,
aajan
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 04:06 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0