如何在bash中打开/操作多个文件?
我有一个bash脚本,它利用本地工具箱执行操作 我的问题相当简单 我有多个数量相同但时间步长不同的文件,我想先将它们全部解压,然后使用工具箱执行一些操作,但我不确定是否在正确的轨道上 ============================================= 文件如下 投入如何在bash中打开/操作多个文件?,bash,unix,outputstream,Bash,Unix,Outputstream,我有一个bash脚本,它利用本地工具箱执行操作 我的问题相当简单 我有多个数量相同但时间步长不同的文件,我想先将它们全部解压,然后使用工具箱执行一些操作,但我不确定是否在正确的轨道上 ============================================= 文件如下 投入 fname = a very large number or files with same name but numbering e.g wnd20121.grb wnd20122.grb
fname = a very large number or files with same name but numbering
e.g wnd20121.grb
wnd20122.grb
.......
wnd2012100.grb
命令
> cdo -f nc copy fname ofile(s)
(如果这是ofile(s)=输出文件,我如何存储它以供后续使用?从命令中获取ofile(输出文件)并将其用作/保存为下一个文件的输入,生成一个新的随后编号的ofile(s)2输出集)
(然后自动获取ofile(s)2并将其输入到下一个命令,依此类推,始终生成一个新的输出文件数组,其中包含我设置的特定集合名称,但不同的编号用于区分它们)
------------------------------------
为了让我的问题更清楚,我想知道我可以通过bash脚本基本上指示终端“抓取”多个文件的方式,这些文件通常是相同的名称,但具有不同的编号,以便将它们与记录的时间分开
e、 g.文件1文件2…文件n
然后获得多个输出,每个输出对应于它转换的文件的编号
e、 g.输出1输出2…输出n
如何设置这些参数,以便在生成这些参数时将其存储在脚本中,以便在以后的命令中使用?您的问题不清楚,但以下内容可能会有所帮助;它演示了如何将数组用作参数列表,以及如何将命令输出逐行解析为数组:
#!/usr/bin/env bash
# Create the array of input files using pathname expansion.
inFiles=(wnd*.grb)
# Pass the input-files array to another command and read its output
# - line by line - into a new array, `outFiles`.
# The example command here simply prepends 'out' to each element of the
# input-files array and outputs each (modified) element on its own line.
# Note: The assumption is that the filenames have no embedded newlines
# (which is usually true).
IFS=$'\n' read -r -d '' -a outFiles < \
<(printf "%s\n" "${inFiles[@]}" | sed s'/^/out-/')
# Note: If you use bash 4, you could use `readarray -t outFiles < <(...)` instead.
# Output the resulting array.
# This also demonstrates how to use an array as an argument list
# to pass to _any_ command.
printf "%s\n" "${outFiles[@]}"
#/usr/bin/env bash
#使用路径名扩展创建输入文件数组。
填充=(wnd*.grb)
#将输入文件数组传递给另一个命令并读取其输出
#-逐行-放入一个新数组“outFiles”。
#这里的示例命令只是将“out”前置到
#输入文件数组并在其自己的行上输出每个(修改的)元素。
#注意:假设文件名没有内嵌的换行符
#(这通常是正确的)。
IFS=$'\n'读取-r-d'-a输出文件<\
到现在为止你做了什么?请发布您的解决方案。@Jord我已经编写了序列,第一部分,我认为之后我可以扩展多个加载的逻辑并保存到其余的脚本,但我从这些开始您真的在使用**wnd10m.gdas.2010*.grb2*****文件***
在命令行上?cmd行上的多个*
字符与单个*
字符相同。重写为**wnd10m.gdas.2010*.grb2**ofile*
(这似乎仍然不太可能)。在你的问题中,不要把这个问题和这种“噪音”混为一谈。祝你好运。@Sheller不,我没有使用它们,我被要求粘贴代码,所以我这样做了,但在我感兴趣的文件下面用粗体键入,我会更正我使用的格式,以便更清晰,但很难知道这里发生了什么,因为我不知道cdo是做什么的。考虑把这个问题归结为1个问题,然后用你所使用的简单定义。不要试图把它列成一个问题清单。此外,您的代码>fname.grib='wnd10m.gdas*.grb2'
似乎可疑。允许fname.grib=
是一个有效的cdo
构造,标记为文件的东西在末尾有一堆空格似乎是不寻常的。你是说fname.grib='wnd10m.gdas*.grb2'
?不要在这里回答,用这个反馈来改进你的下一个问题。祝你好运非常感谢,这似乎可以帮助我基本上“循环”整个流程
>cdo sellon ofile(s)2 ofile(s)3
#!/usr/bin/env bash
# Create the array of input files using pathname expansion.
inFiles=(wnd*.grb)
# Pass the input-files array to another command and read its output
# - line by line - into a new array, `outFiles`.
# The example command here simply prepends 'out' to each element of the
# input-files array and outputs each (modified) element on its own line.
# Note: The assumption is that the filenames have no embedded newlines
# (which is usually true).
IFS=$'\n' read -r -d '' -a outFiles < \
<(printf "%s\n" "${inFiles[@]}" | sed s'/^/out-/')
# Note: If you use bash 4, you could use `readarray -t outFiles < <(...)` instead.
# Output the resulting array.
# This also demonstrates how to use an array as an argument list
# to pass to _any_ command.
printf "%s\n" "${outFiles[@]}"