RunRyder RC
WATCH
 1 page 476 views POST REPLY
HomeOff Topics › Need some help with a sed script
01-11-2018 11:13 PM  10 months agoPost 1
Xterra

rrApprentice

USA

My Posts: All  Forum  Topic

For the scripters:
I saw a couple of posts on scripting so I decided to give it a shot.
I am changing files names from:
Sample_S1_L001_R1.txt
Sample_S10_L001_R1.txt
Sample_S11_L001_R1.txt
to:
MID-01_Sample_L001_R1.txt
MID-010_Sample_L001_R1.txt
MID-011_Sample_L001_R1.txt
Using the following script:
ls *.txt | sed -r 's/^(.*_)S([0-9]{1,2}_)(.*)/mv & MID-0\2\1\3/' | sh
Needless to say that the script works. However, I think there should be an easier way to accomplish the same task. I am looking for sed, awk or perl scripts if at all possible.
Any ideas?


Stratus
Raptor 50V2
T-rex SE
T-rex CDE
T-rex 250 SC
Kyosho M24

PM  EMAIL  HOMEPAGE  GALLERY  Attn:RR  Quote
01-12-2018 01:04 AM  10 months agoPost 2
AirWolfRC

rrProfessor

42½ N, 83½ W

My Posts: All  Forum  Topic

Might be easier to do it in two steps.

Replace Sample_S with MID-0
then
replace L001__R1 with Sample_L001_R1

PM  EMAIL  HOMEPAGE  GALLERY  Attn:RR  Quote
01-12-2018 01:12 AM  10 months agoPost 3
Xterra

rrApprentice

USA

My Posts: All  Forum  Topic

simplifying the scritp
Sample (identifier) can be anything. My folder contains millions of ids. If you see my current script, the id is kept intact. I am trying to simplify the process, not adding complexity to speed up the whole process


Stratus
Raptor 50V2
T-rex SE
T-rex CDE
T-rex 250 SC
Kyosho M24

PM  EMAIL  HOMEPAGE  GALLERY  Attn:RR  Quote
01-12-2018 01:14 AM  10 months agoPost 4
mdu6

rrKey Veteran

Montreal

My Posts: All  Forum  Topic

Very difficult to be more efficient/easier in a one liner.

Your usage of regular expression is pretty good - sed, awk and vanilla perl all rely on REGEX - so for such a simple task these more powerful tools (awk & perl) would not provide anything easier.

PM  EMAIL  GALLERY  Attn:RR  Quote
01-12-2018 02:00 AM  10 months agoPost 5
Xterra

rrApprentice

USA

My Posts: All  Forum  Topic

Mdu
I was just hoping someone could beat my one-liner. I know we have some advanced scripters on RR. I hope they get to see my post. I exhausted my options on my go-to script forum


Stratus
Raptor 50V2
T-rex SE
T-rex CDE
T-rex 250 SC
Kyosho M24

PM  EMAIL  HOMEPAGE  GALLERY  Attn:RR  Quote
01-12-2018 02:10 AM  10 months agoPost 6
mdu6

rrKey Veteran

Montreal

My Posts: All  Forum  Topic

Well - I started on Unix (SunBSD) in the late 80s !

What exactly are you trying to solve - besides a style exercice I don't see a problem with what your doing.

If you have a large amount of files to modify in 1 directory - just make sure you don't use ls *.txt - as you will reach most shell limits for pattern matching.

PM  EMAIL  GALLERY  Attn:RR  Quote
01-12-2018 02:25 AM  10 months agoPost 7
Xterra

rrApprentice

USA

My Posts: All  Forum  Topic

I am trying to reduce the processing time
I am very well within the limits on my current setup


Stratus
Raptor 50V2
T-rex SE
T-rex CDE
T-rex 250 SC
Kyosho M24

PM  EMAIL  HOMEPAGE  GALLERY  Attn:RR  Quote
01-12-2018 02:38 AM  10 months agoPost 8
mdu6

rrKey Veteran

Montreal

My Posts: All  Forum  Topic

If you have a very very large number (1M+) of files to process - then maybe it will be more efficient to do this in two steps then in a single very large pipeline.

So :

ls | sed -r 's/^(.*_)S([0-9]{1,2}_)(.*)/\/bin\/mv & MID-0\2\1\3/' > ../listtoprocess.sh

chmod 755 ../listtoprocess.sh
mv ../listtoprocess.sh .
./listtoprocess.sh

PM  EMAIL  GALLERY  Attn:RR  Quote
01-12-2018 02:50 AM  10 months agoPost 9
Xterra

rrApprentice

USA

My Posts: All  Forum  Topic

Creating an executable will likely increase the processing -I will time it tomorrow though. However, it is not exactly what I am looking for.
I am Curious to see if soneone can provide any other alternative rather than splitting the sed script ....


Stratus
Raptor 50V2
T-rex SE
T-rex CDE
T-rex 250 SC
Kyosho M24

PM  EMAIL  HOMEPAGE  GALLERY  Attn:RR  Quote
01-12-2018 03:04 AM  10 months agoPost 10
mdu6

rrKey Veteran

Montreal

My Posts: All  Forum  Topic

This is not splitting the sed script - rather having the shell execute the commands (/bin/mv) in a separate task.

Whatever you want to think about - will lead to a similar solution. For example a Perl script will use the same REGEX to change the names and then calling RENAME in a loop (which ends up calling the same underlying system calls as /bin/mv).

The shells are typically pretty efficient for such simple task. I don't think you will gain a lot of speed by using Perl for example.

Good luck

PM  EMAIL  GALLERY  Attn:RR  Quote
WATCH
 1 page 476 views POST REPLY
HomeOff Topics › Need some help with a sed script
 Print TOPIC  Make Suggestion 

 2  Topic Subscribe

Thursday, November 15 - 2:24 pm - Copyright © 2000-2018 RunRyder   EMAILEnable Cookies

The RC discussion world needs to consolidate. RR is now one choice for that. Its software is cutting edge. It hosts on-topic advertising. Help RR increase traffic buy making suggestions, posting in RR's new areas (sites) and by spreading the word.

The RunRyder Difference

• Category system to allow Rep/Vendor postings.
• Classifieds with sold (hidden) category.
• Classifieds with separate view new.
• Answer PMs offsite via email reply.
• Member gallery photos with advanced scripting.
• Gallery photo viewer integrated into postings.
• Highly refined search with advanced back end.
• Hosts its own high end fast response servers.
• Hosts thousands of HD event coverage videos.
• Rewrote entire code base with latest technology.
• No off-topic (annoying) click bait advertising.
Login Here
 New Subscriptions 
 Buddies Online