change name several files
2
0
Entering edit mode
8 weeks ago
gubrins ▴ 130

Heys,

I'm trying to change a specific part of several files I have but I'm not accomplishing it. I looked for it in the internet but people usually want to change a specific pattern within the name. In my case, I have several files as: file1.10.csv file2.87.csv file3.345.csv

My goal is to remove the second "column", so the 10 in the first case, the 87 in the second and the 345 in the third. I know I can obtain that info with cut, but I'm not managing to errase it. The final results should be to obtain file1.csv, file2.csv and so on.

Thanks in advance!

bash • 373 views
ADD COMMENT
3
Entering edit mode
8 weeks ago
GenoMax 108k

Here is one solution:

$ ls -1 *.csv
file.345.csv
file1.10.csv
file2.87.csv

$ for i in *.csv; do name=$(echo ${i} | cut -f1 -d "." ); echo ${name} ; echo mv ${i} ${name}.csv; done
file
mv file.345.csv file.csv
file1
mv file1.10.csv file1.csv
file2
mv file2.87.csv file2.csv

Remove echo before mv when you are sure things look ok.

ADD COMMENT
0
Entering edit mode

thank you very much! I still don't have enough programming knowledge to do it!

ADD REPLY
3
Entering edit mode
8 weeks ago
My goal is to remove the second "column", so the 10 in the first case, the 87 in the second and the 345 in the third.

This statement is confusing. On one hand, you are (OP) saying you want to remove second column and on the other hand, you are saying 10 in first case. What is 10, 87 and 345 in first, second, third cases? Do you want to rename files or do you want to remove the second column in all the files?

If it is renaming and pattern is exactly same, try following dry-run:

$ tree .
.
├── file1.10.csv
├── file2.87.csv
└── file3.345.csv

$ rename -n 's/(.*)\..*\.(.*)/$1\.$2/' *.csv

'file1.10.csv' would be renamed to 'file1.csv'
'file2.87.csv' would be renamed to 'file2.csv'
'file3.345.csv' would be renamed to 'file3.csv'

Remove -n if you are okay with dummy run.

bash loop:

$ for i in *.csv; do echo $i ${i%%\.*}.csv;done

Replace echo with mv if you are okay with output

with parallel

$ parallel --dry-run --col-sep "\." mv {1}\.{2}\.{3} {1}\.{3}  ::: *.csv

Remove --dry-run if you are okay with output

ADD COMMENT
1
Entering edit mode

I interpreted it as column == field

ADD REPLY
1
Entering edit mode

seems so. At some point OP mentions that I know I can obtain that info with cut, but I'm not managing to errase it. cut is about column extraction. OP post is confusing.

ADD REPLY
0
Entering edit mode

Thank you very much, sorry for the confusion, your first interpretation was correct!

ADD REPLY

Login before adding your answer.

Traffic: 2466 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6