我需要在bash中根据用户输入从一个日期到另一个日期读取文件名



我需要你的帮助。我有一个脚本,需要在用户输入日期从一个日期到另一个日期读取命名文件。

目录中有很多文件的名称中有日期,ej:

xxxx20200930.txt_001
xxxx20200930.txt_002
xxxx20201001.txt_001
xxxx20201001.txt_002
xxxx20201002.txt_001
xxxx20201002.txt_002

例如:

Indicate star date: 20200930
Indicate finish date: 20200202

我需要在所有文件之间cat,然后grep(我已经解决了所有这些问题(

我试过使用日期功能,但总是遇到问题:

日期:fecha invâlida«日期输入2»日期:fecha invélida«dateinput»

startdate='dateinput'
enddate='dateinput2'

while [ ! "$(date -d ${dateinput})" ]; do
read -p "Indicate a date please: " dateinput
done
While [ ! "$(date -d ${dateinput2})" ]; do
read -p "Indicate a second date please: " dateinput2
done
enddate=$( date -d "$enddate" +%Y%m%d )   # rewrite in YYYYMMDD format
#  and take last iteration into account
thedate=$( date -d "$startdate" +%Y%m%d )
while [ "$thedate" != "$enddate" ]; do
printf 'The date is "%s"n' "$thedate"
thedate=$( date -d "$thedate + 1 days" +%Y%m%d ) # increment by one day
done

这是我在这里找到的一些代码,但我无法使其工作

有人能帮我吗?

感谢

使用我所拥有的,给定

$: printf "%sn" *.txt*
xxxx20200930.txt_001
xxxx20200930.txt_002
xxxx20201001.txt_001
xxxx20201001.txt_002
xxxx20201002.txt_001
xxxx20201002.txt_002
xxxx20201003.txt_001
xxxx20201003.txt_002
xxxx20201004.txt_001
xxxx20201004.txt_002

我用过这个:

start=20201001
end=20201003
started=0
ending=0
for f in *.txt*
do case "$f" in
*"$start".txt*) started=1; echo "$f"; continue ;;
*"$end".txt*) ending=1;  echo "$f"; continue ;;
*) (( started )) ||                   continue;
(( ending  )) &&                   continue;
echo "$f"                                   ;;
esac
done

输出-

xxxx20201001.txt_001
xxxx20201001.txt_002
xxxx20201002.txt_001
xxxx20201002.txt_002
xxxx20201003.txt_001
xxxx20201003.txt_002

严重依赖短路逻辑
文件名可能会破坏它。

非常感谢Paul,我为开始和结束相等的情况添加了一个if:

read -p "Ingrese Fecha comienzo" start
read -p "Ingrese Fecha final" end
read -p param1
read -p param2
read -p param3
start=$start
end=$end
started=0
ending=0
if [[ "$start" == "$end" ]];
then
(cat *"$start".txt*)
else
for f in *.txt*
do case "$f" in
*"$start".txt*) started=1; echo "$f"; continue ;;
*"$end".txt*) ending=1;  echo "$f"; continue ;;
*) (( started )) ||                   continue;
(( ending  )) &&                   continue;
echo "$f"                                   ;;
esac
done
fi

问题是,我需要对所有的文件都这样做,但我尝试了,但并没有只对第一个文件起作用,我试图为echo重新映射它$f";

cantlineas=`cat $f | grep "$param1" | grep "$param2" | grep "$param3" |  wc -l`

其他参数是提示输入用户。

最新更新