通过在bash中读取文件作为输入来创建目录

dps*_*dce 3 bash shell

我有一个输入文件说temp.txt,内容如下

  2013-08-13 /data/PSG/LZ/INVENTORY_FORECAST/load_date=2013-03-01
  2013-08-14 /data/PSG/LZ/INVENTORY_FORECAST/load_date=2013-03-02
  2013-08-15 /data/PSG/LZ/INVENTORY_FORECAST/load_date=2013-03-03
  2013-07-30 /data/PSG/LZ/INVENTORY_FORECAST/load_date=2013-07-30
  2013-07-31 /data/PSG/LZ/INVENTORY_FORECAST/load_date=2013-07-31
  2013-08-16 /data/PSG/LZ/INVENTORY_FORECAST/load_date=2013-08-13
Run Code Online (Sandbox Code Playgroud)

我需要迭代这个文件并创建具有在行开头指定的日期的目录,然后将日期之后指定的目录中的数据移动到此特定目录.

例如:对于第一行,我需要做一个

mkdir "2013-08-13" 
Run Code Online (Sandbox Code Playgroud)

然后

mv /data/PSG/LZ/INVENTORY_FORECAST/load_date=2013-03-01/  2013-08-13
Run Code Online (Sandbox Code Playgroud)

我正在努力做到这一点

  cat temp.txt | while read line ; do  mkdir "echo $line | awk '{print $0}'"; done;
Run Code Online (Sandbox Code Playgroud)

试图使用line作为数组

  cat temp.txt | while read line; do lineArray=($line) echo $line, ${lineArray[0]}, $lineArray[1];  done;
Run Code Online (Sandbox Code Playgroud)

但这些似乎都不起作用..关于如何解决这个问题的任何想法?

use*_*001 6

您可以将这些行读入两个变量.例如:

while read -r date path # reads the lines of temp.txt one by one, 
                        # and sets the first word to the variable "date", 
                        # and the remaining words to the variable "path"
do 
    mkdir -p -- "$date"  # creates a directory named "$date".
    mv -- "$path" "$date" # moves the file from the "$path" variable to the "$date folder"
done < temp.txt   # here we set the input of the while loop to the temp.txt file
Run Code Online (Sandbox Code Playgroud)

使用该--选项,以便如果文件以-它开头,则不会将其解释为选项,但将按字面处理.

-p--parents使mkdir命令,如果该目录存在不特罗错误,并在必要时做出的父目录.

  • 如果有多个相同的日期,您可能需要使用`mkdir -p`. (2认同)