batch file rename. change at the second occurrence.

General support questions
Post Reply
supertight
Posts: 171
Joined: 2017/02/07 21:47:51

batch file rename. change at the second occurrence.

Post by supertight » 2018/03/05 00:44:34

I have a batch of files with "item.id-number.variable.useless-info.avi"
I'm not sure why the security system saves the files to the server like this and I've looked all over the OS for some type of adjustment. But no dice.

I want to remove the "variable.useless-info" portion.
Normally, if the value after the 2nd "." in the file name was fixed, I would use something like this.

Code: Select all

for file in *.avi; do mv "$file" "${file/.v*/.avi}"  ; done
However, Given that the value after the 2nd "." is variable, I can't do that.
I know for sure there is a way to have the command to pass the 1st "." and operate at the 2nd "."
I've been googling like a maniac. I can't seem to find the correct format.

Any help with this is greatly appreciated.

hunter86_bg
Posts: 2019
Joined: 2015/02/17 15:14:33
Location: Bulgaria
Contact:

Re: batch file rename. change at the second occurrence.

Post by hunter86_bg » 2018/03/05 07:54:26

Maybe something like this should do the trick:

Code: Select all

for i in *.avi ; do echo -n "$i ---> "; NEW=$(echo "$i" | sed 's/.v*.avi/.avi/'); mv "$i" "$NEW" ;echo ; done

pjsr2
Posts: 614
Joined: 2014/03/27 20:11:07

Re: batch file rename. change at the second occurrence.

Post by pjsr2 » 2018/03/05 09:06:39

If you are sure you always have five parts in the name separated by four dots, you can do the following:

Code: Select all

for i in *.*.*.*.avi ; do 
    mv "$i" "$(echo $i | cut -d . -f 1,2,5)"
done

supertight
Posts: 171
Joined: 2017/02/07 21:47:51

Re: batch file rename. change at the second occurrence.

Post by supertight » 2018/03/05 22:47:34

hunter86_bg wrote:Maybe something like this should do the trick:

Code: Select all

for i in *.avi ; do echo -n "$i ---> "; NEW=$(echo "$i" | sed 's/.v*.avi/.avi/'); mv "$i" "$NEW" ;echo ; done
But what if the value after the second "." is not "v"? It's different every time.

supertight
Posts: 171
Joined: 2017/02/07 21:47:51

Re: batch file rename. change at the second occurrence.

Post by supertight » 2018/03/05 22:48:59

pjsr2 wrote:If you are sure you always have five parts in the name separated by four dots, you can do the following:

Code: Select all

for i in *.*.*.*.avi ; do 
    mv "$i" "$(echo $i | cut -d . -f 1,2,5)"
done

I see what you did here... It's not 4 every time. sometimes there is a time stamp too.

pjsr2
Posts: 614
Joined: 2014/03/27 20:11:07

Re: batch file rename. change at the second occurrence.

Post by pjsr2 » 2018/03/06 10:01:42

In bash scripts, * is a "greedy" operator: it matches as much as possible.
So *.*.*.avi will match both a.b.c.avi and a.b.c.d.avi. In other words: *.*.*.avi matches anything ending in .avi with at least 4 dots.

If you want to get a list of only those names that contain exactly 4 dots, you need to match anything that has at least 4 dots and filter out anything that has at least five dots. You can do this in the following way:

Code: Select all

for i in $(ls *.*.*.*.avi *.*.*.*.*.avi | sort | uniq -u) ; do
    mv "$i" "$(echo $i | cut -d . -f 1,2,5)"
done

Post Reply