【问题标题】:Add time stamp to file which has at least one row in UNIX在 UNIX 中为至少有一行的文件添加时间戳
【发布时间】:2018-01-20 09:33:41
【问题描述】:

我在 ${POWERCENTER_FILE_DIR} 位置有文件列表。 这些文件由行标题和值组成。

MART_Apple.csv
MART_SAMSUNG.csv
MART_SONY.csv
MART_BlackBerry.csv

要求:

  1. 只选择至少有 1 行的文件。
  2. 为至少有 1 行的文件添加时间戳。

例如: 如果除 MART_BlackBerry.csv 之外的所有文件都至少有一行,那么我的输出文件名应该是

MART_Apple_20170811112807.csv
MART_SAMSUNG_20170811112807.csv
MART_SONY_20170811112807.csv

到目前为止尝试过的代码

#!/bin/ksh
infilename=${POWERCENTER_FILE_DIR}MART*.csv
echo File name is ${infilename}
if [ wc -l "$infilename"="0" ];
then
        RV=-1
        echo "input file name cannot be blank or *"
        exit $RV 
fi

current_timestamp=`date +%Y%m%d%H%M%S`
filename=`echo $infilename | cut -d"." -f1 `
sftpfilename=`echo $filename`_${current_timestamp}.csv

cp -p ${POWERCENTER_FILE_DIR}$infilename ${POWERCENTER_FILE_DIR}$sftpfilename

RV=$?
if [[ $RV -ne 0 ]];then
        echo Adding timestamp to ${POWERCENTER_FILE_DIR}$infilename failed ... Quitting
        echo Return Code $RV
        exit $RV
fi

遇到如下错误:

line 3: [: -l: binary operator expected
cp: target `MART_Apple_20170811121023.csv' is not a directory
failed ... Quitting
Return Code 1

坦率地说,我无法理解这些错误,也不确定我做对了。 unix脚本初学者。任何专家都可以指导我正确的方法。

【问题讨论】:

  • 标记为bash 并使用ksh?嗯?
  • [ wc -l "$infilename"="0" ]; 是错误的。好吧,看起来你必须在开始编写脚本之前做一些功课。例如 1) 命令替换在哪里,2) 你检查过wc -l 的输出,如果是的话 3) 你将如何过滤这些结果以获得你想要的结果 4) 你在这里做什么比较,整数或字符串 5) 等等。 [ shellcheck ] 也是一个很棒的修正工具,适用于想要 bash 的脚本编写者。祝你好运。 ;)
  • @sjsam 谢谢,更正了标记。是的,我犯了很多错误。 wc =0 对我没有帮助。感谢介绍 shellcheck 很好。
  • 顺便说一句,我的回答是为您提供现成的解决方案。如果它符合您的需要,您可以接受它作为答案。 :)

标签: shell unix scripting ksh


【解决方案1】:

简化的脚本类似于

#!/bin/bash 
# Note I'm using bash above, can't guarantee (but I hope) it would work in ksh too.
for file in ${POWERCENTER_FILE_DIR}/*.csv # Check Ref [1]
do
if [ "$( wc -l "$file" | grep -Eo '^[[:digit:]]+' )" -ne 0 ] # checking at least one row? Check Ref [2]
then
 mv "$file" "${file%.csv}$(date +'%Y%m%d%H%M%S').csv" # Check Ref [3]
fi
done

参考文献

File Globbing [1]
Command Substitution [2]
Parameter Substitution [3]

【讨论】:

    【解决方案2】:

    这是一个仅使用 findshmvbasenamedate 的示例:

    find ${POWERCENTER_FILE_DIR}MART*.csv ! -empty -execdir sh -c "mv {} \$(basename -s .csv {})_\$(date +%Y%m%d%H%M%S).csv" \;
    

    我建议阅读Unix Power Tools 以获得更多想法。

    【讨论】:

      【解决方案3】:

      当涉及到 shell 脚本时,很少有单一/一个/正确的方法来完成所需的任务。

      通常,您可能需要在可读性、可维护性、性能、遵守某些本地编码标准和 shell 环境可用性之间进行权衡(我相信还有一些权衡)。所以,fwiw ...


      根据您只对至少 1 行的文件感兴趣的要求,我认为这也意味着您只对大小 > 0 的文件感兴趣。

      一个简单的 ksh 脚本:

      #!/bin/ksh
      
      # define list of files
      filelist=${POWERCENTER_FILE_DIR}/MART*.csv
      
      # grab current datetime stamp
      dtstamp=`date +%Y%m%d%H%M%S`
      
      # for each file in our list ...
      for file in ${filelist}
      do
          # each file should have ${POWERCENTER_FILE_DIR} as a prefix;
          # uncomment 'echo' line for debugging purposes to display
          # the contents of the ${file} variable:
          #echo "file=${file}"
      
          #   '-s <file>' => file exists and size is greater than 0
          # '! -s <file>' => file doesn't exist or size is equal to 0, eg, file is empty in our case
          #
          # if the file is empty, skip/continue to next file in loop
          if [ ! -s ${file} ]
          then
              continue
          fi
      
          # otherwise strip off the '.csv'
          filebase=${file%.csv}
      
          # copy our current file to a new file containing the datetime stamp;
          # keep in mind that each ${file} already contains the contents of the
          # ${POWERCENTER_FILE_DIR} variable as a prefix; uncomment 'echo' line
          # for debug purposes to see what the cp command looks like:
      
          #echo "cp command=cp ${file} ${filebase}.${dtstamp}.csv"
          cp ${file} ${filebase}.${dtstamp}.csv
      done
      

      一些学习ksh的好资源:

      【讨论】:

        猜你喜欢
        • 2018-05-30
        • 2018-09-19
        • 2017-09-15
        • 2011-01-31
        • 1970-01-01
        • 2023-03-06
        • 2020-12-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多