Gsub regex not working | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Gsub regex not working

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-07-2013
unknownn unknownn is offline
Registered User
 
Join Date: Jul 2012
Last Activity: 20 July 2013, 10:58 PM EDT
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Gsub regex not working

I have a number of files that I pass through awk/gsub.

I believe to have found a working regex and on 'test bed' sites it matches, however within gsub it does not.

Examples:
Initial data:

Code:
/Volumes/Daniel/Public/Drop Box/[sage]_Hellsing_Ultimate_OVA_-_10_[720p-AAC][4B4C84C5].mkv

gsub & regex:

Code:
gsub("\[[^\]]+\]",""

I've been able to remove mostly everything with other expressions but the first brackets "[sage]" still remain. The content within the brackets varies a lot (e.g. [SFW-sage].
I'd like to keep the script as simple as possible (one line).
Sponsored Links
    #2  
Old 01-07-2013
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 24 April 2014, 3:37 AM EDT
Location: San Jose, CA, USA
Posts: 3,496
Thanks: 141
Thanked 1,208 Times in 1,023 Posts
One way to do it is using [ and ] as field separators is:

Code:
awk -F '[]]|[[]' '{for(i=1; i<=NF; i+=2)printf("%s",$i);printf("\n")}' in

or using gsub():

Code:
awk '{gsub("[[][^]]+[]]","")}1' in

If you're using a Solaris system, use /usr/xpg4/bin/awk or nawk instead of awk.

Last edited by Don Cragun; 01-07-2013 at 10:32 PM.. Reason: provide two ways to do it
The Following User Says Thank You to Don Cragun For This Useful Post:
unknownn (01-07-2013)
Sponsored Links
    #3  
Old 01-07-2013
unknownn unknownn is offline
Registered User
 
Join Date: Jul 2012
Last Activity: 20 July 2013, 10:58 PM EDT
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Don Cragun View Post
One way to do it is:

Code:
awk -F '[]]|[[]' '{for(i=1; i<=NF; i+=2)printf("%s",$i);printf("\n")}' in

If you're using a Solaris system, use /usr/xpg4/bin/awk or nawk instead of awk.
Afraid I'm running this under OS X.
awk gives me:

Code:
awk: can't open file in source line number 1

nawk is unavailable.

Edit: Your second solution works wonderfully. Thank you very much.

Last edited by unknownn; 01-07-2013 at 10:43 PM..
    #4  
Old 01-07-2013
rdrtx1 rdrtx1 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 17 April 2014, 5:28 PM EDT
Location: Houston, Texas, USA
Posts: 660
Thanks: 0
Thanked 200 Times in 192 Posts
try:

Code:
gsub("[[][^]]*]","")

Sponsored Links
    #5  
Old 01-07-2013
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 24 April 2014, 3:37 AM EDT
Location: San Jose, CA, USA
Posts: 3,496
Thanks: 141
Thanked 1,208 Times in 1,023 Posts
You didn't say what the name was for the file that contains your "initial data". Both of the scripts I provided assume that the text you want to process is in a file named in . I've tried both of them on Mac OS X Version 10.7.5. Since OS X is a Mac OS X system (not a Solaris system), use awk.
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
GSUB/Regex Help nitrobass24 Shell Programming and Scripting 6 08-22-2012 04:41 PM
awk gsub(): general regex lucasvs UNIX for Dummies Questions & Answers 9 04-05-2012 03:48 AM
matching a regex using egrep not working skrtxao Shell Programming and Scripting 6 11-28-2011 11:16 AM
gnu sed regex grouping not working? Allasso Shell Programming and Scripting 3 01-20-2009 08:25 PM



All times are GMT -4. The time now is 07:29 AM.