I've been trying to get this working for a bit now... this question has helped, but I'm still struggling to get this to work. I'd prefer not to install homebrew, because this is a rare task that I'm performing right now.
I have a couple thousand files with a string of text, an underscore, more text, underscore and finally the important file name that I want to preserve. Dropping the first *_*_
and preserving the last part, with file extension, is attempted with \(*\)/\1/
.
I've tried a couple different things and all I've got is the original filenames being spit out again. Any help is appreciated. - not sure if it's a regex issue, or sed, or probably a little of both.
ls | sed 's/^*_*_\(*\)/\1/' > ouput.txt;
ls | sed 's/^*_*_\(*\$\)/\1/' > out.txt
ls | sed 's/\(^*_*_\)\(*\$\)/\2/' > out.txt
ls | sed 's/\(^.*_+.*_+\)\(.*\$\)/mv & \2/' > out.txt
Does this regex do the trick? If not, please report how its output is off (details) and I'll help you tune it.
ls -1 | sed -e 's/^[^_]*_[^_]*_//'
Note 1: You may want to use ls -1
to format the files into a single column.
Note 2: The approach above simply removes the unwanted part of your file names, rather than trying to store in a regex buffer the part that you do want.
EDIT
And here's a bash script that performs the rename.
for f in `ls -1`
do
new_name=`echo "$f" | sed 's/^[^_]*_[^_]*_//'`
mv "$f" "$new_name"
done
Can be written as a one-liner, but I went for clarity over brevity.