Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.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
从shell中的文件名提取时间戳部分_Shell_Unix - Fatal编程技术网

从shell中的文件名提取时间戳部分

从shell中的文件名提取时间戳部分,shell,unix,Shell,Unix,我有一个类似于SID_Statistics_20191127.xlsx的文件名。我必须从shell中的文件中仅提取时间部分。时间部分可能正在更改,但格式保持不变。请尝试以下操作 for file in *.xlsx; do val="${file##*_}";echo "${val%.*}"; done 或者,非一种线性形式的解决方案是: for file in *.xlsx do val="${file##*_}" echo "${val%.*}" done 上面将遍历所有.xsl

我有一个类似于SID_Statistics_20191127.xlsx的文件名。我必须从shell中的文件中仅提取时间部分。时间部分可能正在更改,但格式保持不变。请尝试以下操作

for file in *.xlsx; do val="${file##*_}";echo "${val%.*}"; done
或者,非一种线性形式的解决方案是:

for file in *.xlsx
do
  val="${file##*_}"
  echo "${val%.*}"
done
上面将遍历所有
.xslx
文件并打印时间戳。 有关@user1934428注释中参数扩展的更多详细信息,请参阅链接

解释:为上述代码添加详细的解释:

for file in *.xlsx          ##Starting for loop which will traverse through all xlxs format files.
do
  val="${file##*_}"         ##Creating a variable val whose value will be 20191127.xlsx, what it does is, it substitutes everything till _ last occurrence with NULL.
  echo "${val%.*}"          ##Doing substitution from DOT to till end of variable val value with NULL and printing which will print timestamp as per OP need.
done                        ##Closing this specific program for loop here.

你能试试下面的吗

for file in *.xlsx; do val="${file##*_}";echo "${val%.*}"; done
或者,非一种线性形式的解决方案是:

for file in *.xlsx
do
  val="${file##*_}"
  echo "${val%.*}"
done
上面将遍历所有
.xslx
文件并打印时间戳。 有关@user1934428注释中参数扩展的更多详细信息,请参阅链接

解释:为上述代码添加详细的解释:

for file in *.xlsx          ##Starting for loop which will traverse through all xlxs format files.
do
  val="${file##*_}"         ##Creating a variable val whose value will be 20191127.xlsx, what it does is, it substitutes everything till _ last occurrence with NULL.
  echo "${val%.*}"          ##Doing substitution from DOT to till end of variable val value with NULL and printing which will print timestamp as per OP need.
done                        ##Closing this specific program for loop here.

根据您的问题描述,如果文件的格式保持不变,并且只有时间戳部分在更改,则您可以尝试以下代码:

for file in *.xlsx
do
a=`echo $file|cut -d"_" -f3|cut -d"." -f1`
echo "$a"
done

第三行代码中的反勾号(a=
echo$file | cut-d“”-f3 | cut-d”。-f1
)用于执行反勾号中写入的代码。

根据您的问题描述,如果文件的格式保持不变,并且只有时间戳部分在更改,则您可以尝试以下代码:

for file in *.xlsx
do
a=`echo $file|cut -d"_" -f3|cut -d"." -f1`
echo "$a"
done

第三行代码中的反勾号(a=
echo$file | cut-d“”-f3 | cut-d”。-f1
)用于执行反勾号中编写的代码。

非常感谢它的工作。请解释一下它是如何工作的work@AnirudhS,当然,现在让我为它添加详细的解释,会让你知道的。@AnirudhS,在我的帖子中添加了详细的解释,您也可以参考BASH手册中的参数扩展,谢谢。@RavinderSingh13:参考不应该是BASH手册,因为OP通过指定shell标记来询问常规POSIX shell解决方案。所以正确的参考应该是。当然,您的解决方案在这两种情况下都有效。@user1934428,当然可以,谢谢。我现在已经在我的答案中添加了这个链接,干杯。非常感谢它正在工作。请解释一下它是如何工作的work@AnirudhS,当然,现在让我为它添加详细的解释级别,同样会让您知道。@AnirudhS,现在在我的帖子中添加了详细的解释级别,您也可以参考BASH手册中的参数扩展,干杯。@RavinderSingh13:不应该参考bash手册,因为OP通过指定shell标记来询问常规POSIX shell解决方案。所以正确的参考应该是。当然,您的解决方案在这两种情况下都有效。@user1934428,当然可以,谢谢。我已经在我的答案中添加了这个链接,干杯。删除所有非数字
name=“SID_Statistics_20191127.xlsx”;echo${name/[!0-9]/}
删除所有非数字
name=“SID_Statistics_20191127.xlsx”;echo${name/[!0-9]/}