Tiago Minuzzi
Tiago Minuzzi

Reputation: 141

Rename files sequentially keeping first part of name

I have these files:

NIHMS735398-supplement-Supplemental_Table_-_Top_Down_MS_Identificaitons.csv.0
NIHMS735398-supplement-Supplemental_Table_-_Top_Down_MS_Identificaitons.csv.1
NIHMS735398-supplement-Supplemental_Table_-_Top_Down_MS_Identificaitons.csv.2
NIHMS735398-supplement-Supplemental_Table_-_Top_Down_MS_Identificaitons.csv.3
NIHMS735398-supplement-Supplemental_Table_-_Top_Down_MS_Identificaitons.csv.4

And I'd like to rename like this:

NIHMS735398-supplement-Supplemental_Table_-_Top_Down_MS_Identificaitons_1.csv
NIHMS735398-supplement-Supplemental_Table_-_Top_Down_MS_Identificaitons_2.csv
NIHMS735398-supplement-Supplemental_Table_-_Top_Down_MS_Identificaitons_3.csv
NIHMS735398-supplement-Supplemental_Table_-_Top_Down_MS_Identificaitons_4.csv
NIHMS735398-supplement-Supplemental_Table_-_Top_Down_MS_Identificaitons_5.csv

I've tried the following (and some variations) but got no success:

rename -v 's/(NIHMS.+tons).csv.+/\1_[0-9]\.csv/' *.csv*

and

ls *csv* | sed -E 's/(NIHM.+tons)\.csv.*$/\1_{1..5}.csv/'

and

ls *csv* | cat -n | while read n f; do mv "$f" "$f_$n.csv"; done

The closest I got was using the last one that gave me like:

1.csv 2.csv 3.csv 4.csv 5.csv

I'm pretty sure I can do this using python, but I'm trying via command line/bash and preferably using regex (which I'm trying to learn better).

Upvotes: 0

Views: 199

Answers (4)

Mark Setchell
Mark Setchell

Reputation: 207465

Untested as I am not at a computer, but based on your own efforts with rename, this should be close:

rename -n '($a,$b)=split /.csv./; $_=$a . ($b+1) . ".csv"' *csv*

The split should put everything before ".csv." into $a and everything after it into $b. The second part sets the output ($_) to $a followed by $b+1 with ".csv" appended.

Upvotes: 0

Benjamin W.
Benjamin W.

Reputation: 52152

With Bash regular expressions:

re='(.*)\.csv\.([[:digit:]]+)$'
for f in *; do
    [[ $f =~ $re ]]
    echo mv "$f" "${BASH_REMATCH[1]}_$(( ${BASH_REMATCH[2]} + 1 )).csv"
done

The echo can be removed when the output seems correct.

If the goal is not to incremenent the input file number, but have sequentially numbered files starting from 1, the code can be changed to the following:

re='(.*)\.csv\.[[:digit:]]+$'   # Just one capture group
i=1
for f in *; do
    [[ $f =~ $re ]]
    echo mv "$f" "${BASH_REMATCH[1]}_$(( i++ )).csv"
done

Now the files will be numbered 1, 2, 3, ... in alphabetical input order.

Upvotes: 1

l'L'l
l'L'l

Reputation: 47169

One way you could do this is by using bash parameter substitution:

for f in *.csv.* ; do d="${f##*.}" ; mv "$f" "${f%*.csv*}__$((d+1)).csv" ; done

The d variable gets set to the last digit in the filename, then replaces it in the mv.

Upvotes: 2

Jack
Jack

Reputation: 6158

Create the new name with sed, saving sections of the filename and swapping them:

for file in *.csv.[0-9]*; do
  new_name=$( echo $file | sed 's/\(.*\)\.csv\.\([0-9]*\)/\1_\2.csv/' )
  echo mv $file $new_name
done

For safety, I put in the echo in front on the mv. If the results look good, take out the echo. Here are my results:

[jack@marta ~]$ for file in *.csv.[0-9]*; do new_name=$(echo $file | sed 's/\(.*\)\.csv\.\([0-9]*\)/\1_\2.csv/'); echo mv $file $new_name; done
mv NIHMS735398-supplement-Supplemental_Table_-_Top_Down_MS_Identificaitons.csv.0 NIHMS735398-supplement-Supplemental_Table_-_Top_Down_MS_Identificaitons_0.csv
mv NIHMS735398-supplement-Supplemental_Table_-_Top_Down_MS_Identificaitons.csv.1 NIHMS735398-supplement-Supplemental_Table_-_Top_Down_MS_Identificaitons_1.csv
mv NIHMS735398-supplement-Supplemental_Table_-_Top_Down_MS_Identificaitons.csv.10 NIHMS735398-supplement-Supplemental_Table_-_Top_Down_MS_Identificaitons_10.csv
mv NIHMS735398-supplement-Supplemental_Table_-_Top_Down_MS_Identificaitons.csv.11 NIHMS735398-supplement-Supplemental_Table_-_Top_Down_MS_Identificaitons_11.csv
mv NIHMS735398-supplement-Supplemental_Table_-_Top_Down_MS_Identificaitons.csv.2 NIHMS735398-supplement-Supplemental_Table_-_Top_Down_MS_Identificaitons_2.csv
mv NIHMS735398-supplement-Supplemental_Table_-_Top_Down_MS_Identificaitons.csv.3 NIHMS735398-supplement-Supplemental_Table_-_Top_Down_MS_Identificaitons_3.csv
mv NIHMS735398-supplement-Supplemental_Table_-_Top_Down_MS_Identificaitons.csv.4 NIHMS735398-supplement-Supplemental_Table_-_Top_Down_MS_Identificaitons_4.csv
mv NIHMS735398-supplement-Supplemental_Table_-_Top_Down_MS_Identificaitons.csv.5 NIHMS735398-supplement-Supplemental_Table_-_Top_Down_MS_Identificaitons_5.csv
mv NIHMS735398-supplement-Supplemental_Table_-_Top_Down_MS_Identificaitons.csv.6 NIHMS735398-supplement-Supplemental_Table_-_Top_Down_MS_Identificaitons_6.csv
mv NIHMS735398-supplement-Supplemental_Table_-_Top_Down_MS_Identificaitons.csv.7 NIHMS735398-supplement-Supplemental_Table_-_Top_Down_MS_Identificaitons_7.csv
mv NIHMS735398-supplement-Supplemental_Table_-_Top_Down_MS_Identificaitons.csv.8 NIHMS735398-supplement-Supplemental_Table_-_Top_Down_MS_Identificaitons_8.csv
mv NIHMS735398-supplement-Supplemental_Table_-_Top_Down_MS_Identificaitons.csv.9 NIHMS735398-supplement-Supplemental_Table_-_Top_Down_MS_Identificaitons_9.csv
[jack@marta ~]$ 

Upvotes: 2

Related Questions