Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex Linux Shell脚本-用于过滤文件名和其中的日期的正则表达式_Regex_Linux_Bash_Shell_Scripting - Fatal编程技术网

Regex Linux Shell脚本-用于过滤文件名和其中的日期的正则表达式

Regex Linux Shell脚本-用于过滤文件名和其中的日期的正则表达式,regex,linux,bash,shell,scripting,Regex,Linux,Bash,Shell,Scripting,我有数千个文件采用这种命名格式: cdr_ABSHCECLUSTER_02_201709072214_987392 我正在使用下面的批处理脚本,但我发现,它将根据修改日期重新定位文件,而不是实际创建文件的时间。我如何修改它以从文件名中提取年、月 由于文件可以移动,我发现文件可能会根据“修改日期”而不是创建日期放在错误的目录中 统计显示选项: 通道 被改进的 改变 如果您有任何见解,我将不胜感激。谢谢大家! 这里有一种方法可以从文件名中的日期戳填充年和月变量 从变量文件中的文件名开始 file=

我有数千个文件采用这种命名格式:

cdr_ABSHCECLUSTER_02_201709072214_987392
我正在使用下面的批处理脚本,但我发现,它将根据修改日期重新定位文件,而不是实际创建文件的时间。我如何修改它以从文件名中提取年、月

由于文件可以移动,我发现文件可能会根据“修改日期”而不是创建日期放在错误的目录中

统计显示选项: 通道 被改进的 改变


如果您有任何见解,我将不胜感激。谢谢大家!

这里有一种方法可以从文件名中的日期戳填充
变量

从变量
文件中的文件名开始

file=cdr_ABSHCECLUSTER_02_201709072214_987392
使用下划线(
)作为分隔符,将
文件
拆分为单独的字符串,放入名为
ar
的数组中;我们将在数组中循环以显示组件

IFS='_' read -ra ar <<< "${file}"
for i in "${!ar[@]}"
do
    echo "ar[${i}] = ${ar[${i}]}"
done

# output from for loop:

ar[0] = cdr
ar[1] = ABSHCECLUSTER
ar[2] = 02
ar[3] = 201709072214
ar[4] = 987392
但是您的脚本需要
month
格式的
Mmm
格式(
date+%b
),因此需要进行一些小的调整

# convert our 2-character month to a 3-character 'Mon'th

month=$(date -d "${mo}" +%b)

# confirm our variables:

echo "year=${year} ; month=${month}"

# output from echo command:

year=2017 ; month=Sep
此时,我们已经从文件名中的日期戳填充了
变量,现在您可以继续执行脚本的其余部分

总而言之:

# once the 'file' variable is populated:

IFS='_' read -ra ar <<< "${file}"
year=${ar[3]:0:4}
mo=${ar[3]:4:2}
month=$(date -d "${mo}" +%b)
#填充“file”变量后:

如果s=''''read-ra ar这里有一种方法,可以从文件名中的日期戳填充
变量

从变量
文件中的文件名开始

file=cdr_ABSHCECLUSTER_02_201709072214_987392
使用下划线(
)作为分隔符,将
文件
拆分为单独的字符串,放入名为
ar
的数组中;我们将在数组中循环以显示组件

IFS='_' read -ra ar <<< "${file}"
for i in "${!ar[@]}"
do
    echo "ar[${i}] = ${ar[${i}]}"
done

# output from for loop:

ar[0] = cdr
ar[1] = ABSHCECLUSTER
ar[2] = 02
ar[3] = 201709072214
ar[4] = 987392
但是您的脚本需要
month
格式的
Mmm
格式(
date+%b
),因此需要进行一些小的调整

# convert our 2-character month to a 3-character 'Mon'th

month=$(date -d "${mo}" +%b)

# confirm our variables:

echo "year=${year} ; month=${month}"

# output from echo command:

year=2017 ; month=Sep
此时,我们已经从文件名中的日期戳填充了
变量,现在您可以继续执行脚本的其余部分

总而言之:

# once the 'file' variable is populated:

IFS='_' read -ra ar <<< "${file}"
year=${ar[3]:0:4}
mo=${ar[3]:4:2}
month=$(date -d "${mo}" +%b)
#填充“file”变量后:

IFS=''read-ra ar这个脚本就是您所需要的

find /sftphome/*/CDR -type f -maxdepth 2 | 
    while read file 
    do
        date=`basename "$file" | cut -d_ -f4`
        newdir="$(cut -d/ -f-4 <<< "$file")/${date:0:4}/${date:4:2}"
        mkdir -p "$newdir"
        mv -f "$file" "$newdir"
    done

这个脚本就是你所需要的

find /sftphome/*/CDR -type f -maxdepth 2 | 
    while read file 
    do
        date=`basename "$file" | cut -d_ -f4`
        newdir="$(cut -d/ -f-4 <<< "$file")/${date:0:4}/${date:4:2}"
        mkdir -p "$newdir"
        mv -f "$file" "$newdir"
    done
请看shell操作符,它可以用于使用模式提取部分变量。请看shell操作符,它可以用于使用模式提取部分变量。