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 and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Processing a CSV file janemary.a High Level Programming 1 05-11-2007 06:27 AM
Have a shell script check for a file to exist before processing another file heprox Shell Programming and Scripting 3 11-14-2006 02:26 AM
File processing on perl garric Shell Programming and Scripting 2 09-01-2006 11:25 PM
processing line in file fablef00 Shell Programming and Scripting 8 01-23-2006 10:41 AM
Processing a text file TheCrunge UNIX for Dummies Questions & Answers 1 11-09-2005 10:47 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-04-2007
Registered User
 

Join Date: Oct 2007
Posts: 13
processing a file with sed and awk

Hello,

I have what is probably a simple task in text manipulation, but I just can't wrap my brain around it.

I have a text file that looks something like the following. Note that some have middle initials in the first field and some don't.

john.r.smith:john.smith@yahoo.com
george.w.bush:gwbush@whitehouse.gov
larry.doby:ldoby@hotmail.com
tom.t.hall:tom.t.hall@nashville.com


I want to end up with a file that looks something like this:


john<tab>smith<tab>john.smith@yahoo.com
george<tab>bush<tab>gwbush@whitehouse.gov
larry<tab>doby<tab>ldoby@hotmail.com
tom<tab>hall<tab>tom.t.hall@nashville.com

So, I want to split each line into two fields separated by a tab. I was able to easily do this with awk and wrote it to file.

awk -F':' '{print $1"\t"$2}' inputfile > outputfile

I want to eliminate the middle initial in field 1, if present. I can do that with sed, but how can I process only field 1 and leave field 2 intact?

Your suggestions are most welcome.
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-04-2007
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,042
One way.
Code:
sed -e 's%\.[a-z]\.%.%' -e 's%\.%        %' -e 's%:%     %'
note the big spaces are literal tabs, which you can usually get by typing ^v<tab>
Reply With Quote
  #3 (permalink)  
Old 10-04-2007
Registered User
 

Join Date: Oct 2007
Location: Bangalore
Posts: 513
there u go

due to the limitation of sed to interprete \t as a tab..and not wanting to tupe a TAB..i am using awk..combined with sed..

this shud work for you...

sed 's/\(.*\)\.\.*\(.*\)\.\.*\(.*\)\:/\1\.\3\:/g' inputfile |nawk -F':' '{sub(/\./,"\t",$1);print $1,"\t",$2}'

cheers,
Devaraj Takhellambam
Reply With Quote
  #4 (permalink)  
Old 10-11-2007
Registered User
 

Join Date: Oct 2007
Posts: 4
can you explain how that command works.

Thanks
S
Reply With Quote
  #5 (permalink)  
Old 10-11-2007
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,453
Assuming single-character middle initials:

Code:
awk '{sub(/\..\.*/,"\t",$1)}1' FS=":" OFS="\t" filename
Use nawk or /usr/xpg4/bin/awk on Solaris.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Bookmarks

Tags
None

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:




All times are GMT -4. The time now is 05:51 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66