Как это упростить?

curl -v --пользователь 'имя пользователя :пароль' --загрузить -файл {путь}/{binaire} {ваш _URL}/{binaire}

0
07.03.2021, 11:29
4 ответа

Использование GNU awk и соответствующего разделителя полей (FS )и разделителя полей вывода (OFS)

gawk -F '|' -v OFS=: -v yr="$(date +%Y)" '
{ 
  for (i=1; i<=NF; i++) {
    gsub(/^\s+|\s+$/, "", $i)
    sub(/.*:/, "", $i)
    if (i==NF) {
      sub(/[^-]+/, "", $i)
      $i += yr
    }
  }
  print NR, $0, "xxx"
}
' file

Выход:

1:Mercedes:Germany:300 SL:61:xxx
2:Lamborghini:Italy:Miura:51:xxx
3:Aston Martin:UK:DBS:56:xxx
4:Ford:United States of America:GT40:55:xxx
3
18.03.2021, 22:26

Использованиеawk:

BEGIN {
        # Initialize the input field separator to a
        # pattern matching the literal string " | "
        # (space-pipe-space).  Set the output field
        # separator to a colon.
        FS  = " [|] "; OFS = ":"

        # Pick out the current year.
        # (This bit is not standard awk.)
        year = strftime("%Y")
}

{
        # Go through the fields of the current line, one
        # by one, and remove the bit before the (first)
        # colon in each. We don't touch the last field
        # yet.
        for (i = 1; i < NF; ++i) sub("[^:]*:","",$i)

        # For the last field, remove everything but the
        # last four characters. Subtract this from the
        # current year.
        $NF = year - substr($NF,length($NF)-3)

        # Print the resulting line, preceded by the input
        # line number and a colon. Also put ":xxx" at the
        # end.
        printf "%d:%s:xxx\n", NR, $0
}

Опять же, без комментариев,

BEGIN {
        FS  = " [|] "; OFS = ":"
        year = strftime("%Y")
}

{
        for (i = 1; i < NF; ++i) sub("[^:]*:","",$i)
        $NF = year - substr($NF,length($NF)-3)
        printf "%d:%s:xxx\n", NR, $0
}

Приведенный выше код awkобрабатывает каждую строку входного файла как набор полей, разделенных | (пробелом -вертикальной чертой -пробелом ).

Основной блок начинается с перебора каждого поля (, кроме последнего ), и удаляет все до (первого )двоеточия в каждом.

Последнее поле($NF)изменено, чтобы быть текущим годом минус любое число, которое формируют последние четыре символа поля. Текущий год получен через strftime(). Эта функция не является стандартной функцией awk, но GNU awk, mawkи нативная awkв OpenBSD поддерживает ее.

Когда все поля соответствующим образом изменены, строка печатается с добавлением дополнительных данных (к номеру строки )и добавлению (строки xxx).

Тестирование:

$ awk -f script file
1:Mercedes:Germany:300 SL:61:xxx
2:Lamborghini:Italy:Miura:51:xxx
3:Aston Martin:UK:DBS:56:xxx
4:Ford:United States of America:GT40:55:xxx

Тестирование однострочного варианта:

$ awk -F ' [|] ' -v OFS=: 'BEGIN {y=strftime("%Y")} {for(i=1;i<NF;++i)sub("[^:]*:","",$i);$NF=y-substr($NF,length($NF)-3);printf "%d:%s:xxx\n",NR,$0}' file
1:Mercedes:Germany:300 SL:61:xxx
2:Lamborghini:Italy:Miura:51:xxx
3:Aston Martin:UK:DBS:56:xxx
4:Ford:United States of America:GT40:55:xxx
1
18.03.2021, 22:26

Вы можете использовать Miller для преобразования из (стандартного )ключа с разделителями -пары значений ("dkvp" )в формат "csvlite" (или " CSV"):

$ cat file
Car Brand:Mercedes | Country:Germany | Car Model:300 SL | Year:04-1960
Car Brand:Lamborghini | Country:Italy | Car Model:Miura | Year:10-1970
Car Brand:Aston Martin | Country:UK | Car Model:DBS | Year:12-1965
Car Brand:Ford | Country:United States of America | Car Model:GT40 | Year:09-1966

, затем

$ mlr --ifs ' | ' --ips ':' --ocsvlite --ofs ':' \
    put '$Item = NR; $Year = strftime(strptime($Year,"%m-%Y"),"%y"); ${Insurance Co} = "xxx"' \
    then reorder -f Item file
Item:Car Brand:Country:Car Model:Year:Insurance Co
1:Mercedes:Germany:300 SL:60:xxx
2:Lamborghini:Italy:Miura:70:xxx
3:Aston Martin:UK:DBS:65:xxx
4:Ford:United States of America:GT40:66:xxx

Добавьте --headerless-csv-output, если вам не нужен заголовок CSV.

1
18.03.2021, 22:26
awk -F "[:|]" '{print NR":"$2":"$4":"$6":",strftime("%Y-%m-%d")-substr($NF,4)":xxx"}'  filename

выход

1:Mercedes:Germany:300 SL:61:xxx
2:Lamborghini:Italy:Miura:51:xxx
3:Aston Martin:UK:DBS:56:xxx
4:Ford:United States of America:GT40:55:xxx
0
18.03.2021, 22:26

Теги

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