I would like to run a .sh script to rename a file which is in the directory desktop/reports/don
of my computer. An example of what I need is to rename:
我想运行一个.sh脚本来重命名我的计算机目录desktop / reports / don中的文件。我需要的一个例子是重命名:
TACOS_2013-Jan-22__00-50-00_UTC.csv
to
TACOS_20130122_005000.csv
I have the following script which was created using windows batch script(.bat file). I would like to convert this into linux shell script.
我有以下脚本,它是使用Windows批处理脚本(.bat文件)创建的。我想将其转换为linux shell脚本。
@echo off
setlocal
for /f "tokens=2-7 delims=_.-" %%A in ('dir /B TACOS_*') do (
setlocal enabledelayedexpansion
call :getmonth %%B
ren TACOS*_*%%A-%%B-%%C*_*%%D-%%E-%%F_UTC.csv TACOS_%%A!mon!%%C_%%D%%E%%F.csv
endlocal
)
:getmonth
if "%1" equ "Jan" set mon=01
if "%1" equ "Feb" set mon=02
if "%1" equ "Mar" set mon=03
if "%1" equ "Apr" set mon=04
if "%1" equ "May" set mon=05
if "%1" equ "Jun" set mon=06
if "%1" equ "Jul" set mon=07
if "%1" equ "Aug" set mon=08
if "%1" equ "Sep" set mon=09
if "%1" equ "Oct" set mon=10
if "%1" equ "Nov" set mon=11
if "%1" equ "Dec" set mon=12
goto :eof
endlocal
this is what i have done so far..Please help
这就是我到目前为止所做的事情。请帮助
#!/bin/bash
month["Jan"]=01
month["Feb"]=02
month["Mar"]=03
month["Apr"]=04
month["May"]=05
month["Jun"]=06
month["Jul"]=07
month["Aug"]=08
month["Sep"]=09
month["Oct"]=10
month["Nov"]=11
month["Dec"]=12
directory="desktop/reports/Don/"
for path in "${directory}TACOS_"*; do
path=${path#${directory}}
newpath=${path:0:10}${month[${path:11:3}]}${path:15:2}
newpath=${newpath}__$(tr -d '-' <<< ${path:19:8}).csv
echo "${directory}${path}" "${directory}${newpath}" # Run this one first!!!
# mv "YOUR/PATH/${path}" "YOUR/PATH/${newpath}"
done
1 个解决方案
#1
1
Now that you made it a bit clearer, I guess this is what you want:
既然你让它更清晰一点,我想这就是你想要的:
- an associative array to map "MONTH NAME" to "MONTH NUMBER";
- rename a file from "TACOS_YYYY-month-dd__HH-MM-SS*.csv" to "TACOS_YYYYmmdd_HHMMSS.csv".
一个关联数组,用于将“MONTH NAME”映射到“MONTH NUMBER”;
将文件从“TACOS_YYYY-month-dd__HH-MM-SS * .csv”重命名为“TACOS_YYYYmmdd_HHMMSS.csv”。
The solution in a bash script:
bash脚本中的解决方案:
#!/bin/bash
declare -A month
month["Jan"]=01
month["Feb"]=02
month["Mar"]=03
month["Apr"]=04
month["May"]=05
month["Jun"]=06
month["Jul"]=07
month["Aug"]=08
month["Sep"]=09
month["Oct"]=10
month["Nov"]=11
month["Dec"]=12
directory="YOUR/PATH/TACOS_"
for path in "${directory}"*; do
path=${path#${directory}}
newpath=${path:0:4}${month[${path:5:3}]}${path:9:2}
newpath=${newpath}__$(tr -d '-' <<< ${path:13:8}).csv
echo "${directory}${path}" "${directory}${newpath}" # Run this one first!!!
# mv "YOUR/PATH/${path}" "YOUR/PATH/${newpath}"
done
This converts the string path=TACOS_2013-Jan-22__00-50-00_UTC.csv
into newpath=TACOS_20130122__005000.csv
, and renames the initial file mv
'ing it to the new path constructed.
这会将字符串路径= TACOS_2013-Jan-22__00-50-00_UTC.csv转换为newpath = TACOS_20130122__005000.csv,并将初始文件mv'重命名为构造的新路径。
As in explanation, bash
offers you associative arrays, that you have to declare prior to any operation using declare -A assoc_array
.
在解释中,bash为您提供了关联数组,您必须在使用declare -A assoc_array的任何操作之前声明这些数组。
In bash
you can take string intervals, setting an offset
, a length
, and doing ${string:offset:length}
. Concatenation is performed by juxtaposition of strings, and assignments must have no spaces between left_value=right_value
.
在bash中,您可以获取字符串间隔,设置偏移量,长度以及执行$ {string:offset:length}。连接是通过并置字符串来执行的,并且赋值必须在left_value = right_value之间没有空格。
In addition, you have tr
command, translating your string from initial
to initial_without_characters
, since the flag -d
has been used. You may take a look at man tr
for further reference.
此外,您还有tr命令,将字符串从initial翻译为initial_without_characters,因为已使用标志-d。您可以查看man tr以获得进一步的参考。
Edit:
Since you don't have a more recent version of bash
, you can use the following code:
由于您没有更新版本的bash,因此可以使用以下代码:
#!/bin/bash
function month() {
case $1 in
"Jan") echo "01" ;;
"Feb") echo "02" ;;
"Mar") echo "03" ;;
"Apr") echo "04" ;;
"May") echo "05" ;;
"Jun") echo "06" ;;
"Jul") echo "07" ;;
"Aug") echo "08" ;;
"Sep") echo "09" ;;
"Oct") echo "10" ;;
"Nov") echo "11" ;;
"Dec") echo "12" ;;
esac
}
directory="YOUR/PATH/TACOS_"
for path in "${directory}"*; do
path=${path#${directory}}
newpath=${path:0:4}$(month ${path:5:3})${path:9:2}
newpath=${newpath}__$(tr -d '-' <<< ${path:13:8}).csv
echo "${directory}${path}" "${directory}${newpath}" # Run this one first!!!
# mv "YOUR/PATH/${path}" "YOUR/PATH/${newpath}"
done
#1
1
Now that you made it a bit clearer, I guess this is what you want:
既然你让它更清晰一点,我想这就是你想要的:
- an associative array to map "MONTH NAME" to "MONTH NUMBER";
- rename a file from "TACOS_YYYY-month-dd__HH-MM-SS*.csv" to "TACOS_YYYYmmdd_HHMMSS.csv".
一个关联数组,用于将“MONTH NAME”映射到“MONTH NUMBER”;
将文件从“TACOS_YYYY-month-dd__HH-MM-SS * .csv”重命名为“TACOS_YYYYmmdd_HHMMSS.csv”。
The solution in a bash script:
bash脚本中的解决方案:
#!/bin/bash
declare -A month
month["Jan"]=01
month["Feb"]=02
month["Mar"]=03
month["Apr"]=04
month["May"]=05
month["Jun"]=06
month["Jul"]=07
month["Aug"]=08
month["Sep"]=09
month["Oct"]=10
month["Nov"]=11
month["Dec"]=12
directory="YOUR/PATH/TACOS_"
for path in "${directory}"*; do
path=${path#${directory}}
newpath=${path:0:4}${month[${path:5:3}]}${path:9:2}
newpath=${newpath}__$(tr -d '-' <<< ${path:13:8}).csv
echo "${directory}${path}" "${directory}${newpath}" # Run this one first!!!
# mv "YOUR/PATH/${path}" "YOUR/PATH/${newpath}"
done
This converts the string path=TACOS_2013-Jan-22__00-50-00_UTC.csv
into newpath=TACOS_20130122__005000.csv
, and renames the initial file mv
'ing it to the new path constructed.
这会将字符串路径= TACOS_2013-Jan-22__00-50-00_UTC.csv转换为newpath = TACOS_20130122__005000.csv,并将初始文件mv'重命名为构造的新路径。
As in explanation, bash
offers you associative arrays, that you have to declare prior to any operation using declare -A assoc_array
.
在解释中,bash为您提供了关联数组,您必须在使用declare -A assoc_array的任何操作之前声明这些数组。
In bash
you can take string intervals, setting an offset
, a length
, and doing ${string:offset:length}
. Concatenation is performed by juxtaposition of strings, and assignments must have no spaces between left_value=right_value
.
在bash中,您可以获取字符串间隔,设置偏移量,长度以及执行$ {string:offset:length}。连接是通过并置字符串来执行的,并且赋值必须在left_value = right_value之间没有空格。
In addition, you have tr
command, translating your string from initial
to initial_without_characters
, since the flag -d
has been used. You may take a look at man tr
for further reference.
此外,您还有tr命令,将字符串从initial翻译为initial_without_characters,因为已使用标志-d。您可以查看man tr以获得进一步的参考。
Edit:
Since you don't have a more recent version of bash
, you can use the following code:
由于您没有更新版本的bash,因此可以使用以下代码:
#!/bin/bash
function month() {
case $1 in
"Jan") echo "01" ;;
"Feb") echo "02" ;;
"Mar") echo "03" ;;
"Apr") echo "04" ;;
"May") echo "05" ;;
"Jun") echo "06" ;;
"Jul") echo "07" ;;
"Aug") echo "08" ;;
"Sep") echo "09" ;;
"Oct") echo "10" ;;
"Nov") echo "11" ;;
"Dec") echo "12" ;;
esac
}
directory="YOUR/PATH/TACOS_"
for path in "${directory}"*; do
path=${path#${directory}}
newpath=${path:0:4}$(month ${path:5:3})${path:9:2}
newpath=${newpath}__$(tr -d '-' <<< ${path:13:8}).csv
echo "${directory}${path}" "${directory}${newpath}" # Run this one first!!!
# mv "YOUR/PATH/${path}" "YOUR/PATH/${newpath}"
done