awk to remove last two -*-* | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

awk to remove last two -*-*

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-04-2013
yanglei_fage yanglei_fage is offline
Registered User
 
Join Date: Jan 2009
Last Activity: 7 October 2014, 11:33 AM EDT
Posts: 274
Thanks: 8
Thanked 0 Times in 0 Posts
awk to remove last two -*-*


Code:
fq-bar-something-1.0-r1.src.rpm  ----> fq-bar-something
fq-bar-xx-r1-rel.src.rpm   ---------> fq-bar-xx
fq-bar-ff-ver-11-rel.src.rpm   -------> fq-bar-ff-ver

any help?
Sponsored Links
    #2  
Old 01-05-2013
fuad_ fuad_ is offline
Registered User
 
Join Date: May 2012
Last Activity: 5 January 2013, 12:41 AM EST
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts

Code:
cat filename | awk -F '-' '{$NF=""; $(NF-1)=""; print }' | sed 's/ /-/g' | sed 's/--$//g'

Please next time seperate input and expected output in two different CODEQOUTE

Last edited by fuad_; 01-05-2013 at 12:38 AM..
Sponsored Links
    #3  
Old 01-05-2013
yanglei_fage yanglei_fage is offline
Registered User
 
Join Date: Jan 2009
Last Activity: 7 October 2014, 11:33 AM EDT
Posts: 274
Thanks: 8
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by fuad_ View Post
Try this


Code:
$ cat filename | awk -F '-' '{$NF=""; $(NF-1)=""; print }' | sed 's/ /-/g' | sed 's/--$//g' | sed 's/>-/> /g'

Here how it works


Code:
$ cat filename | awk -F '-' '{$NF=""; $(NF-1)=""; print }' | sed 's/ /-/g' | sed 's/--$//g' | sed 's/>-/> /g' 

fq-bar-something-1.0-r1.src.rpm------> fq
fq-bar-xx-r1-rel.src.rpm------------> fq
fq-bar-ff-ver-11-rel.src.rpm----------> fq-bar


That doesn't get what I want. please see my expected ouput
    #4  
Old 01-05-2013
fuad_ fuad_ is offline
Registered User
 
Join Date: May 2012
Last Activity: 5 January 2013, 12:41 AM EST
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts

Code:
 cat filename | awk -F '-' '{$NF=""; $(NF-1)=""; print }' | sed 's/ /-/g' | sed 's/--$//g'

Please next time seperate input and expected output in two different CODEQOUTE
Sponsored Links
    #5  
Old 01-05-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 25 October 2014, 7:19 PM EDT
Location: Amsterdam
Posts: 9,549
Thanks: 286
Thanked 2,428 Times in 2,175 Posts
Try:

Code:
awk 'NF-=2{$1=$1}1' FS=- OFS=- file


Code:
sed 's/-[^-]*-[^-]*$//' file

Sponsored Links
    #6  
Old 01-05-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 25 October 2014, 1:16 PM EDT
Location: Aachen, Germany
Posts: 4,427
Thanks: 73
Thanked 1,084 Times in 1,022 Posts
I had wanted to offer sth like
Code:
$ awk -F '-' '{for (i=1;i<NF-2;i++) printf "%s-", $i; printf "%s\n", $(i) }' file

but scutinizer's proposals beat them all!
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
can I remove the first char using AWK? Abdulelah Shell Programming and Scripting 2 01-03-2011 05:18 PM
AWK remove string in between () jimmy_y Shell Programming and Scripting 3 05-19-2010 04:08 AM
a SED/AWK way to remove everything except... necron Shell Programming and Scripting 4 03-25-2010 08:13 PM
Remove prefix using awk pinnacle Shell Programming and Scripting 5 06-02-2009 04:53 AM
How to remove ^Z (eof) using sub in awk placroix1 Shell Programming and Scripting 2 05-13-2005 10:40 AM



All times are GMT -4. The time now is 07:33 PM.