我有一个这样的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
答案 0 :(得分:0)
我对您现有的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