在bash中遍历多个文件

在bash中遍历多个文件,bash,Bash,我有一个文件夹,其中包含几个文件,其名称如下: file.001.txt.gz,file.002.txt.gz,file.150.txt.gz 我要做的是使用循环对每个文件运行一个程序。我是这样想的(只是一个草图): 首先,我不知道这样做是否有效,其次,我不知道如何保持文件中的三位数字('001'而不仅仅是'1') 非常感谢bash中范围的语法是 {1..150} 不是{1:150} 此外,如果bash足够新,可以添加前导零: {001..150} for循环的正确语法需要do和done f

我有一个文件夹,其中包含几个文件,其名称如下:

file.001.txt.gz,file.002.txt.gz,file.150.txt.gz

我要做的是使用循环对每个文件运行一个程序。我是这样想的(只是一个草图):

首先,我不知道这样做是否有效,其次,我不知道如何保持文件中的三位数字('001'而不仅仅是'1')


非常感谢

bash中范围的语法是

{1..150}
不是
{1:150}

此外,如果bash足够新,可以添加前导零:

{001..150}
for循环的正确语法需要
do
done

for i in {001..150} ; do
    # ...
done

现在还不清楚脚本中的
$1
包含什么内容。

bash中范围的语法是

{1..150}
不是
{1:150}

此外,如果bash足够新,可以添加前导零:

{001..150}
for循环的正确语法需要
do
done

for i in {001..150} ; do
    # ...
done

现在还不清楚脚本中包含了什么
$1

要遍历文件,我认为更简单的方法是: (假设目录中没有名为“file.*.txt”的文件,并且您的输出文件可以有不同的名称)


要迭代文件,我认为更简单的方法是: (假设目录中没有名为“file.*.txt”的文件,并且您的输出文件可以有不同的名称)


使用
find
命令:

# Path to the source directory
dir="./"

while read file
do
  output="$(basename "$file")"
  output="$(dirname "$file")/"${output/#file/output}
  echo "$file ==> $output"
done < <(find "$dir" \
  -regextype 'posix-egrep' \
  -regex '.*file\.[0-9]{3}\.txt\.gz$')
样本输出

(对于
$dir=/home/ruslan/tmp/

描述

脚本迭代
$dir
目录中的文件。
$file
变量由从
find
命令读取的下一行填充。
find
命令返回与正则表达式
'.[0-9]{3}\.txt\.gz$'对应的路径列表

$output
变量由两部分组成:basename(无目录的路径)和dirname(文件目录的路径)


${output/#file/output}
表达式使用
find
命令将文件替换为
$output
变量前端的输出(请参见)

# Path to the source directory
dir="./"

while read file
do
  output="$(basename "$file")"
  output="$(dirname "$file")/"${output/#file/output}
  echo "$file ==> $output"
done < <(find "$dir" \
  -regextype 'posix-egrep' \
  -regex '.*file\.[0-9]{3}\.txt\.gz$')
样本输出

(对于
$dir=/home/ruslan/tmp/

描述

脚本迭代
$dir
目录中的文件。
$file
变量由从
find
命令读取的下一行填充。
find
命令返回与正则表达式
'.[0-9]{3}\.txt\.gz$'对应的路径列表

$output
变量由两部分组成:basename(无目录的路径)和dirname(文件目录的路径)

${output/#file/output}
表达式将文件替换为
$output
变量前端的输出(请参见)

Try-

for i in $(seq -w 1 150)     #-w adds the leading zeroes
do
  gunzip file."$i".txt.gz
  ./my_program file."$i".txt output."$1".txt
  gzip file."$1".txt
done
试一试-

范围的语法是,但在文件上迭代时,通常需要使用glob。如果您知道所有文件的名称中都有三位数字,则可以在数字上进行匹配:

shopt -s nullglob
for i in file.0[0-9][0-9].txt.gz file.1[0-4][0-9] file.15[0].txt.gz; do
  gunzip file.$i.txt.gz
  ./my_program file.$i.txt output.$i.txt
  gzip file.$i.txt
done
这将只遍历存在的文件。如果使用范围表达式,则必须格外小心,不要尝试对不存在的文件进行操作

for i in file.{000..150}.txt.gz; do
    [[ -e "$i" ]] || continue
    ...otherstuff
done
范围的语法是,但在文件上迭代时,通常需要使用glob。如果您知道所有文件的名称中都有三位数字,则可以在数字上进行匹配:

shopt -s nullglob
for i in file.0[0-9][0-9].txt.gz file.1[0-4][0-9] file.15[0].txt.gz; do
  gunzip file.$i.txt.gz
  ./my_program file.$i.txt output.$i.txt
  gzip file.$i.txt
done
这将只遍历存在的文件。如果使用范围表达式,则必须格外小心,不要尝试对不存在的文件进行操作

for i in file.{000..150}.txt.gz; do
    [[ -e "$i" ]] || continue
    ...otherstuff
done

您需要将“output.$1.txt”更改为“output.$i.txt”作为第一步您需要将“output.$1.txt”更改为“output.$i.txt”作为第一步虽然此代码可能有助于解决此问题,但它没有解释为什么和/或如何回答此问题。提供这种额外的环境将大大提高其长期教育价值。请在回答中添加解释,包括适用的限制和假设。虽然此代码可能有助于解决问题,但它没有解释为什么和/或如何回答问题。提供这种额外的环境将大大提高其长期教育价值。请在回答中添加解释,包括适用的限制和假设。