【问题标题】:how to parse out file name and import the data into csv files using shellscript?如何使用 shellscript 解析文件名并将数据导入 csv 文件?
【发布时间】:2015-08-13 05:00:19
【问题描述】:

我有一个 Matlab 程序将一些数据输出到文件名中,例如文件名看起来像

Temperature_10_Volumn_5.dat

文件里面是空的,但是我需要解析出10和5。我有1000个这样的文件。解析数字并将它们写入 csv 文件以便 Matlab 可以读取 csv 文件并绘制图形的最简单的 shellscript 是什么?谢谢!

【问题讨论】:

  • 你想制作一个包含 10;5 的文件,对吗?
  • matlab 可以轻松读取 10;5 吗?
  • 确定你可以从 Matlab 做到这一点,但我不明白所有这些文件的意义。它们实际上是否包含值(csv 值?)。您是要根据这些索引(10 5 等...)将这些值附加到一个文件中,还是 10 是实际温度数据和 5 是实际体积数据?
  • 10 和 5 是实际数据,我知道这是一种愚蠢的方法......这是我们正在做的一个非常简化的版本:(

标签: matlab shell csv


【解决方案1】:

这是将所有值打印在单个文件中的脚本,其中值用“;”分隔

用法是:

parse_file.sh /home/user/your_dir_to_files output.csv

#!/bin/bash
#title         :parse_file.sh
#description   :parse file name into a single file
#author        :Bertrand Martel
#date          :13/08/2015

file_list=`ls $1`

IFS=$'\n'     #line delimiter

#empty your output file
cp /dev/null "$2"

for i in $file_list; do

    #get temperature between _ and _
    temperature=`echo $i | awk -v FS="([_]|[_])" '{print $2}'`

    #remove everything before Volumn
    second_part=`echo $i | sed 's/.*Volumn//'`
    
    #get volume between _ and .
    volume=`echo $second_part | awk -v FS="([_]|[.])" '{print $2}'`

    new_line="$temperature;$volume"

    #append to file
    echo $new_line >> "$2"
done

cat "$2"

我用 https://gist.github.com/bertrandmartel/fd68c0373af35eaba934 文件创建了一个要点

我假设你的目录输出中只有所谓的文件

【讨论】:

    猜你喜欢
    • 2019-04-24
    • 2021-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-19
    • 2023-03-23
    • 1970-01-01
    相关资源
    最近更新 更多