создать папку из динамического пути

Опция Awk:

awk -F, '{ printf "%s","04/03/2017";for (i=2;i<=NF;i++) {printf ",%s",$i } }' filename

Распечатайте дату запроса, а затем просмотрите и распечатайте оставшиеся поля, разделенные символом

1
10.09.2019, 11:22
1 ответ

Просто небольшой тест с моей стороны, я надеюсь, что комментарии объяснят, что я сделал. Я предполагаю Баш.

По сути, поиск в Google «bash read csv file» помог мне. Остальное на самом деле просто некоторая логика if else и основные команды.

#!/usr/bin/env bash

OLDIFS=$IFS  # store old delimiter for csv files
IFS=" | "    # set delimiter of csv file

# attempt to get the filename of the only csv file in the current folder
# doesn't work if there are multiple csv files -> look into fuzzy finder or demenu for example
# doesn't work if csv file doesn't exist in current directory
# of course you can read the filename from command line with read command
# ls | grep *.csv might be useful to get all names of csv files in current directory
filename=$(ls | grep *.csv)  # store output of command pipe in filename
echo "$filename"  # debug, to see what is stored in $filename

while read f1 f2 f3  # read csv file, each line, 3 fields per line
do
    # debug, echo each field
    echo "field 1 is: $f1"
    echo "field 2 is: $f2"
    echo "field 3 is: $f3"

    echo ""
    echo "does $f2 exist?"
    [ -d $f2 ] && echo "yes" || echo "no"  # really just a short if else, in case you don't know this one

    if [ ! -d $f2 ]; then  # if directory in second column of csv doesn't exist
        echo "creating directory"
        mkdir -p $f2  # -p creates necessary parent directories if they don't exist
    fi

    echo "copying $f3 to $f2/$f3"
    cp $f3 $f2/$f3  # copying doesn' force you to move files for testing, replace with move

done < $filename

# script never reaches this point if anything before fails
# if you want to use this script more often you should think about a way to reliably reset $IFS
IFS=$OLDIFS  # reset delimiter

Мой CSV-файл:

whatever number | /existing_path/nonexisting_path/move_stuff_here | testfile.txt

testfile.txt должен существовать в вашем текущем рабочем каталоге, чтобы это работало.

Несуществующий _путь просто показывает mkdir -p.

0
28.01.2020, 00:00

Теги

Похожие вопросы