Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

awk to remove last two -*-*

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-04-2013   -   Original Discussion by yanglei_fage
yanglei_fage yanglei_fage is offline
Registered User
 
Join Date: Jan 2009
Last Activity: 11 February 2017, 7:38 AM EST
Posts: 307
Thanks: 9
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   -   Original Discussion by yanglei_fage
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 01:38 AM..
Sponsored Links
    #3  
Old Unix and Linux 01-05-2013   -   Original Discussion by yanglei_fage
yanglei_fage yanglei_fage is offline
Registered User
 
Join Date: Jan 2009
Last Activity: 11 February 2017, 7:38 AM EST
Posts: 307
Thanks: 9
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   -   Original Discussion by yanglei_fage
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   -   Original Discussion by yanglei_fage
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 21 November 2017, 11:11 PM EST
Location: Amsterdam
Posts: 11,625
Thanks: 517
Thanked 3,381 Times in 2,980 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   -   Original Discussion by yanglei_fage
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 22 November 2017, 1:45 PM EST
Location: Aachen, Germany
Posts: 11,644
Thanks: 321
Thanked 3,618 Times in 3,323 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 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 06:18 PM
AWK remove string in between () jimmy_y Shell Programming and Scripting 3 05-19-2010 05:08 AM
a SED/AWK way to remove everything except... necron Shell Programming and Scripting 4 03-25-2010 09:13 PM
Remove prefix using awk pinnacle Shell Programming and Scripting 5 06-02-2009 05:53 AM
How to remove ^Z (eof) using sub in awk placroix1 Shell Programming and Scripting 2 05-13-2005 11:40 AM



All times are GMT -4. The time now is 04:14 PM.