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
How to export jisha BSD 4 01-21-2008 04:06 AM
combining fields in two text fields shocker Shell Programming and Scripting 3 01-16-2008 08:27 AM
export??? Justinkase Shell Programming and Scripting 1 12-03-2007 03:32 PM
difference between set and export shriashishpatil UNIX for Dummies Questions & Answers 1 02-22-2006 10:28 AM
export variables srishan Shell Programming and Scripting 4 07-06-2004 09:53 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 08-29-2006
Registered User
 

Join Date: Jul 2006
Posts: 73
export only last two fields?

export only last two fields?

here is my test file, with variable length of fields, each line is composed of letter only, and each line has at least 2 fields, fields were separated by dot (.)

abcd.abc.mlm
dft.dfasdf.bmk.kdjlka
ksdfalksdjfsl.tyu.ml
kasdjf.asdfkja.asdfj.asdf.df.m
lja.ml
aslkjdfjasldkf.sadlfkasdjlf.sdfasdfsa.asdfasdf.fdlm

I am trying to use sed to parse the test file, and only display the last two fields, like

abc.mlm
bmk.kdjlka
tyu.ml
df.m

At first my idea was something like ++ sed 's/!\.[a-z][a-z]*\.[a-z][a-z]*$/&/' test ++, and sure it did not work.

Any help? thank you!
Reply With Quote
Forum Sponsor
  #2  
Old 08-29-2006
Glenn Arndt's Avatar
Anomalous Lurker
 

Join Date: Feb 2006
Location: Indianapolis, IN
Posts: 255
fields.txt:
Code:
abcd.abc.mlm                                       
dft.dfasdf.bmk.kdjlka                              
ksdfalksdjfsl.tyu.ml                               
kasdjf.asdfkja.asdfj.asdf.df.m                     
lja.ml                                             
aslkjdfjasldkf.sadlfkasdjlf.sdfasdfsa.asdfasdf.fdlm
Code:
awk 'BEGIN { FS="."; OFS="." } { print $(NF-1),$NF }' fields.txt
Code:
abc.mlm      
bmk.kdjlka   
tyu.ml       
df.m         
lja.ml       
asdfasdf.fdlm
Reply With Quote
  #3  
Old 08-29-2006
Registered User
 

Join Date: Jul 2006
Posts: 73
Really cool and thanks Glenn Arndt!


But still I have great interest in whether sed can do this

I think "pattern keeping method" should work, as following

sed 's/^.*\(\.[a-z][a-z]*\.[a-z][a-z]*$\)/\1/'

but apparently it does not. I am reading more sed help file trying to figure out why.
Reply With Quote
  #4  
Old 08-29-2006
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,016
Code:
echo 'dft.dfasdf.bmk.kdjlka' | sed 's/.*[.]\(.*\)[.]\(.*\)/\1.\2/'
Reply With Quote
  #5  
Old 08-29-2006
Registered User
 

Join Date: Jul 2006
Posts: 73
Thank you vgersh99!
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 12:00 AM.


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

Content Relevant URLs by vBSEO 3.2.0