使用bash脚本/ shell脚本读取多个文件

时间:2016-07-29 05:44:36

标签: bash awk sed netcdf

我是shell / bash脚本的新手。 我想使用bash或shell脚本从多个netcdf文件中提取数据。 每个文件都包含一系列温度值。 例如:

FileA.nc contains 20 20 21 22 23 24
FileB.nc contains 23 24 25 26 27 24
FileC.nc contains 21 20 19 18 22 23

我想提取每个文件的值并合并三个文件的结果。 输出应该看起来像保存为csv文件。

A 20 20 21 22 23 24
B 23 24 25 26 27 24
C 21 20 19 18 22 23

最简单的方法是什么? 非常感谢您的帮助。

3 个答案:

答案 0 :(得分:3)

您可以继续 -

    event.startDate = selectedDateValue.date; //The start date value 19-07-2016 8 am
    event.endDate = selectedDateValue.date; //The start date value 19-07-2016 8 am. set the same start date value as the end date, so you will be able have the particular event bound to the 8am time frame of the calendar date.

    NSDate * reccuranceEndDate = selectedEndDateValue.date;//The end date value 25-07-2016 

    EKRecurrenceEnd *recurrenceEnd = [EKRecurrenceEnd recurrenceEndWithEndDate:reccuranceEndDate];
    EKRecurrenceRule *rule = [[EKRecurrenceRule alloc] initRecurrenceWithFrequency:EKRecurrenceFrequencyDaily interval:1 end:recurrenceEnd];

    event.recurrenceRules = @[rule];

现在,您将拥有一个附加单个文件的巨大文件。 现在将空格变成逗号(如果你想要.csv格式)

for i in {A..C}
do 
echo -n "$i " >> master_file
cat File"$i".nc >> master_file
done 

答案 1 :(得分:2)

awk是你的朋友:

$ arry=( file{A..C}.nc ) # store all the filenames in an array
$ # Then feed all the files to awk like below
$ awk '{printf "%s %s\n",gensub(/file(.)\.nc/,"\\1","1",FILENAME),$0}' "${arry[@]}" >newfile
$ cat newfile 
A 20 20 21 22 23 24
B 23 24 25 26 27 24
C 21 20 19 18 22 23

注意

这需要你[ gnu awk ],我想你已经拥有了。{/ p>

答案 2 :(得分:1)

在AWK中:

$ awk '{gsub(/^file|\.nc$/,"",FILENAME); print FILENAME,$0}' file*.nc
A 20 20 21 22 23 24
B 23 24 25 26 27 24
C 21 20 19 18 22 23