LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-02-2018, 01:35 PM   #1
zimbot
Member
 
Registered: Nov 2005
Location: cincinnati , ohio . USA
Distribution: ubuntu , Opensuse , CentOS
Posts: 179

Rep: Reputation: 17
bash reg expression to split many file names on _3charnumbers_


friends,

I think what I need to do is split strings with a regular expression

where : _ and any 3 numbers underscore is my deliminator

I need to split a string ( a dir full of file names , thousands of mov files )

they all have the various forms of this
jug_WNYW_0000013_001_CommercialBlock7.mov
jug_WNYW_0000013_002_CommercialBlock7.mov
all the way to
jug_WNYW_0000013_099_CommercialBlock7.mov
and here are other examples
jug_WNYW_0000013_041_WeatherTwo.mov
jug_WLWT_0000099_022_reds.mov
jug_WLWT_0001234_001_reds.mov
jug_WCPO_0008994_007_shewalkedintothedoor.mov
b99456666_007_shetakesawall.mov
c9902345_003_chocolatepieisgood.mov

what I really wish i could do get get what ever is LEFT of the underscore 3 wide number underscore

goodStuff_NNN_
i wish for the good stuff

I tried a reg expression with
tapeName="$(cut -d'_[0-9]{3}_' -f1 <<<"$jug_1978-03-31_wlwt_AIR_005_SubheadsTeasers")"

i Thought the _[0-9]{3} would be
underscore and any 3 numbers and underscore

I just want jug_1978-03-31_wlwt_AIR


I have had limited success with

$ echo "jug_1978-03-31_WDTN_AIR_005_SubheadsTeasers" | awk -F'_' '{print $1"_"$2"_"$3}'
returns
jug_1978-03-31_WDTN

and you see that will never work with a string like
c9902345_003_chocolatepieisgood.mov

so I am back to needing the LEFT of _NNN_
that is an increment _001_ to _999_

I have done some sed & awk .... but never reg expressions

also this is on a mac in bash....

it *seems the mac (os 10.11) also does not take sed from standard in .... isn't that nice


any assistance is appreciated - thank you

best regards

and THANK YOU

zimbot
 
Old 08-02-2018, 01:46 PM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,328
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
Do you need to count the files or just trim what is to the right of the triple digit number?

If you are limited to bash on OS X then parameter expansion might be relevant.

http://wiki.bash-hackers.org/syntax/pe

Otherwise it is easy with the perl-based version of the rename utility.
 
Old 08-02-2018, 05:45 PM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
if you're going from left to right
Code:
userx@slacurr.ent.org:~
$ fun=jug_WNYW_0000013_001_CommercialBlock7.mov

#furthest to the right 
$ sofun=${fun##*[0-9]_}

$ echo $sofun
CommercialBlock7.mov
or
Code:
userx@slacurr.ent.org:~
$ Name=jug_WLWT_0000099_022_reds.mov ;
 
$ leadingFname=${Name%%_[0-9]*} ;
 
$ endingFname=${Name##*[0-9]_} ;
 
$ newFileName="$leadingFname"_"$endingFname" ;
 
$ echo $newFileName
jug_WLWT_reds.mov
look under Substring Removal

Last edited by BW-userx; 08-02-2018 at 06:03 PM.
 
1 members found this post helpful.
Old 08-02-2018, 07:00 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,140

Rep: Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122
Quote:
Originally Posted by zimbot View Post
it *seems the mac (os 10.11) also does not take sed from standard in .... isn't that nice
I find that very hard to believe. But I wouldn't be caught dead using a mac, so I can't check.

Could be done with parameter substitution, but I always mess up complex globs. Does anyone know if/how you can include a (specific) repetition count in a glob in a substring removal like that ?.
I also agree the perl rename is best option as you can use regex to get the job done. But you'd better get it correct (for all cases).
 
1 members found this post helpful.
Old 08-03-2018, 12:54 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Perl
Code:
$var1="jug_1978-03-31_wlwt_AIR_005_SubheadsTeasers";
$var2=(split(/_[0-9]{3}_/,$var1))[0];
print "var2 = $var2\n";

# o/p
var2 = jug_1978-03-31_wlwt_AIR

String mangling is one of Perl's strengths...
 
1 members found this post helpful.
Old 08-06-2018, 10:31 AM   #6
zimbot
Member
 
Registered: Nov 2005
Location: cincinnati , ohio . USA
Distribution: ubuntu , Opensuse , CentOS
Posts: 179

Original Poster
Rep: Reputation: 17
thanks everybody

I will be giving some of this a try

I very much appreciate the perl suggestion .
due to convience I hope for a full bash solution

thanks again
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash: bash file names are different than openbox trash file names whatthefunk Programming 4 02-15-2011 01:52 AM
Reg EX - Trouble writing regular expression slackaddict Programming 18 08-18-2008 05:06 AM
Split a large file and get the names of output files using Perl Sherlock Programming 25 02-02-2007 12:43 PM
finding an offset directory with reg expression airswit Programming 1 02-15-2006 12:52 AM
Reg Expression Question windisch Programming 6 10-04-2005 11:08 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration