RunRyder RC
WATCH
 1 page 550 views POST REPLY
Home🌌Off TopicsOff Topics Main Discussion › Need some help with a sed script
01-11-2018 11:13 PM  25 months ago
Topic Vote0Post 1
Xterra

rrApprentice

USA

MyPosts All Forum Topic
Need some help with a sed script
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
SHARE  PM  EMAIL  HOMEPAGE  GALLERY  Attn:RR  Quote
01-12-2018 01:04 AM  25 months ago
AirWolfRC

rrProfessor

42½ N, 83½ W

MyPosts 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
SHARE  PM  EMAIL  HOMEPAGE  GALLERY  Attn:RR  Quote
01-12-2018 01:12 AM  25 months ago
Xterra

rrApprentice

USA

MyPosts 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
SHARE  PM  EMAIL  HOMEPAGE  GALLERY  Attn:RR  Quote
01-12-2018 01:14 AM  25 months ago
mdu6

rrKey Veteran

Montreal

MyPosts 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.
SHARE  PM  EMAIL  GALLERY  Attn:RR  Quote
01-12-2018 02:00 AM  25 months ago
Xterra

rrApprentice

USA

MyPosts 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
SHARE  PM  EMAIL  HOMEPAGE  GALLERY  Attn:RR  Quote
01-12-2018 02:10 AM  25 months ago
mdu6

rrKey Veteran

Montreal

MyPosts 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.
SHARE  PM  EMAIL  GALLERY  Attn:RR  Quote
01-12-2018 02:25 AM  25 months ago
Xterra

rrApprentice

USA

MyPosts 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
SHARE  PM  EMAIL  HOMEPAGE  GALLERY  Attn:RR  Quote
01-12-2018 02:38 AM  25 months ago
mdu6

rrKey Veteran

Montreal

MyPosts 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
SHARE  PM  EMAIL  GALLERY  Attn:RR  Quote
01-12-2018 02:50 AM  25 months ago
Xterra

rrApprentice

USA

MyPosts 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
SHARE  PM  EMAIL  HOMEPAGE  GALLERY  Attn:RR  Quote
01-12-2018 03:04 AM  25 months ago
mdu6

rrKey Veteran

Montreal

MyPosts 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
SHARE  PM  EMAIL  GALLERY  Attn:RR  Quote
WATCH
 1 page 550 views POST REPLY
Home🌌Off TopicsOff Topics Main Discussion › Need some help with a sed script
 Print TOPIC  Make Suggestion 

 2  Topic Subscribe

Wednesday, February 26 - 12:19 pm - Copyright © 2000-2020 RunRyder   EMAILEnable Cookies

Login Here
 New Subscriptions 
 Buddies Online