【问题标题】:select specific digits from csv file shell script从 csv 文件 shell 脚本中选择特定数字
【发布时间】:2018-12-16 20:05:51
【问题描述】:

我有一个这样的 csv 文件:

DATE , Name , Description
2012181605 , Meeting , Business Meeting with John
1911181200 , Eating , Eating with my wife

日期以数字形式存储:在本例中,日期等于 20-12-2018 16:05。

我想创建一个 shell 脚本,用户可以在其中键入他想要预览的日期。在这种情况下,我想以某种方式仅提取前 6 个数字,它们仅指示日期而不是确切的时间。

这就是我到目前为止所尝试的。

#!/bin/bash
echo "type the date you want to preview"
read date1
if [ $(grep $date1 text.csv | wc -l) -eq 1 ];then
grep $date1 text.csv
elif [ $(grep $date1 text.csv | wc -l) -eq 0 ]; then
echo "the date you typed doesnt't exist or isn't valid
fi

【问题讨论】:

    标签: bash shell csv


    【解决方案1】:

    我对您现有的 bash 脚本添加了一个小改动,

    #!/bin/bash
    echo "type the date you want to preview"
    read date1
    if [ $(grep $date1 text.csv | wc -l) -eq 1 ];then
    grep $date1 text.csv | cut -c 1-6   # this will only print 6 chars
    elif [ $(grep $date1 text.csv | wc -l) -eq 0 ]; then
    echo "the date you typed doesnt't exist or isn't valid
    fi
    

    【讨论】:

      猜你喜欢
      • 2013-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-15
      • 2020-06-18
      • 2022-11-14
      相关资源
      最近更新 更多