Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

awk to remove last two -*-*

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-04-2013
yanglei_fage yanglei_fage is offline
Registered User
 
Join Date: Jan 2009
Last Activity: 23 April 2015, 12:09 AM EDT
Posts: 286
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 Unix and Linux 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 Unix and Linux 01-05-2013
yanglei_fage yanglei_fage is offline
Registered User
 
Join Date: Jan 2009
Last Activity: 23 April 2015, 12:09 AM EDT
Posts: 286
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 Unix and Linux 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 Unix and Linux 01-05-2013
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 24 May 2015, 9:01 AM EDT
Location: Amsterdam
Posts: 10,206
Thanks: 332
Thanked 2,694 Times in 2,410 Posts
Try:

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


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

Sponsored Links
    #6  
Old Unix and Linux 01-05-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 24 May 2015, 9:18 AM EDT
Location: Aachen, Germany
Posts: 5,955
Thanks: 105
Thanked 1,590 Times in 1,494 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 05:14 PM.