在 shell 中,我瀏覽了幾個檔案以重新格式化它們,因此我生成了一個帶有檔案名 .cpy 擴展名的輸出。
例如,我有 test.des.utf8 生成到 test.des.utf8.cpy,我想做的是從 test.des.utf8 到 TEST.cpy。
我嘗試了類似的方法,但它對我不起作用:
"$f" "${f%.txt}.text"
這是我的帶有輸出重定向的外殼:
for f in $SOURCE_DIRECTORY
do
b=$(basename "$f")
echo "Generating $f file in copy..";
awk -F ';' '
$1=="TABLE" && $3==" " {
printf "01 %s.\n\n", $2;
next
}
{
result = $2
if ($2 ~ /^Numérique [0-9] (\.[0-9] )?$/) {
nr=split($2,a,"[ .]")
result = "PIC 9(" a[2] ")"
if (nr == 3) {
result = result ".v9(" a[3] ")"
}
}
sub(/CHAR/,"PIC X", result);
printf " * %s.\n\n 05 %s %s.\n\n", $3, $1, result;
}' "$f" > "$TARGET_DIRECTORY/$b.cpy"
done
uj5u.com熱心網友回復:
bash 引數擴展使變數的大寫擴展和洗掉部分結果字串變得容易:
filename=test.des.utf8
# Upper-cased version of filename
newfilename="${filename^^}"
# Remove everything from the first . to the end and add a new extension
newfilename="${newfilename%%.*}.cpy"
# Remove echo when happy with results
echo cp "$filename" "$newfilename"
uj5u.com熱心網友回復:
如果使用 Gnu awk,您可以使用這個獨立的 awk 腳本處理所有檔案:
myAwkScript
#!/usr/bin/env -S awk -f
BEGIN {
FS=";"
targetDir="./"
}
# Before processing each file passed as argument
BEGINFILE {
# Split the file path
i=split(FILENAME, splitpath, "/")
# File name part without path is last element
name = splitpath[i]
# Split file name on dot
split(name, splitname, ".")
# Compose outputPath to targetDir with:
# Uppercase first part before dot and .cpy suffix
outputPath = targetDir toupper(splitname[1]) ".cpy"
# Erase the content of the outputPath
printf "" > outputPath
}
# Your original awk script will process each line of each file
$1=="TABLE" && $3==" " {
# Changed this to append to the outputPath file
printf "01 %s.\n\n", $2 >> outputPath
next
}
{
result = $2
if ($2 ~ /^Numérique [0-9] (\.[0-9] )?$/) {
nr=split($2,a,"[ .]")
result = "PIC 9(" a[2] ")"
if (nr == 3) {
result = result ".v9(" a[3] ")"
}
}
sub(/CHAR/,"PIC X", result)
# Changed this to append to the outputPath file
printf " * %s.\n\n 05 %s %s.\n\n", $3, $1, result >> outputPath
}
使腳本可執行:
chmod x ./myAwkScript
運行腳本來處理所有檔案:
./myAwkScript somewhere/*
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/405842.html
標籤:
上一篇:在fifo管道中添加行尾或換行符
