在一个shell中,我浏览几个文件来重新格式化它们,所以我生成一个输出,文件名+ .cpy扩展名。
例如,我生成到test.des.utf8.cpy的test.des.utf8,我想要做的是从test.des.utf8到TEST.cpy。
我试过这样的方法,但对我没有用:
"$f" "${f%.txt}.text"下面是输出重定向的shell:
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发布于 2022-01-06 10:57:03
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"发布于 2022-01-06 12:07:30
如果使用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://stackoverflow.com/questions/70605929
复制相似问题