将文件列出到数组然后将它们转换为管道分隔的文本文件

时间:2014-04-15 19:12:04

标签: arrays sed file-rename tr file-listing

我有几个.dpff文件。我想在Solaris 10 Sparc中执行以下操作

  1. 监听/等待导演/ cm / vic / digital / orcr / vic_export以获取一个或多个.dpff文件。然后
  2. 删除所有.dpff文件中的^ M字符
  3. 将文件路径添加到所有.dpff文件的第一列 文件路径为:/ cm / vic / digital / orcr / vic_export
  4. .dpff文件在制表符分隔文件中是当前的,因此我想将它们转换为管道分隔文件。
  5. 最后,使用时间戳重命名每个文件,例如。 20140415140648.txt
  6. 我的代码如下。我无法达到预期的效果。

    请建议。

    #! /bin/bash
    
    declare -a files
    declare -a z
    
    i=1
    z=`ls *.dpff`
    c=`ls *.dpff`|wc -l
    echo "Start listening for the  .dpff files"
    while :;
            do [ -f /cm/vic/digital/orcr/vic_export/*.dpff ]
            sleep 60;
    
    echo " Assing array with list of .dpff files"
    for i in c
        do
            dirs[i]=$z
        done
    
    echo " Listing files"
    
    for i in c
        do
            sed   's/^/\/cm\/vic\/digital\/orcr\/\vic_export\//' $files[i] > `date +"%Y%m%d%H%M%S"`.dpfff
            tr '\t' '|' < $files[i] > t.txt
    
        done
    done
    

1 个答案:

答案 0 :(得分:3)

很难确定你真正在问什么:你的描述和你的代码并没有真正匹配。不过,这是怎么回事?

#! /bin/bash

declare -a files files_with_timestamp

echo "Start listening for the  .dpff files"
while :; do

    # Listen/wait for the director /cm/vic/digital/orcr/vic_export for the
    # arrival of one or more .dpff file 
    while :; do
        files=( /cm/vic/digital/orcr/vic_export/*.dpff )
        (( ${#files[@]} > 0 )) && break
        sleep 60;
    done

    timestamp=$( date "+%Y%m%d%H%M%S" )
    for file in "${files[@]}"; do
        sed -i '
            # Remove the ^M character in all the .dpff files  
            s/\r//g

            # add the file path to the first column of all .dpff files 
            s@^@/cm/vic/digital/orcr/vic_export/@

            # .dpff files are currenlty in tab delimited file so I want to then
            # convert them to the pipe delimited file.
            s/\t/|/g
        ' "$file"
        newfile="${file%.dpff}.$timestamp.dpff"
        mv "$file" "$newfile"
        files_with_timestamp+=( "$newfile" )
    done

    echo ".dpff files converted:"
    printf "%s\n" "${files_with_timestamp[@]}"
done