Reputation: 151
In a shell i browse several files to reformat them, so i generate an output with the name of the file + a .cpy extension.
For exemple i have test.des.utf8 that i generate to test.des.utf8.cpy, what i would like to do is to go from test.des.utf8 to TEST.cpy.
I tried something like that but it didn't work for me:
"$f" "${f%.txt}.text"
Here is my shell with the output redirection :
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
Upvotes: 1
Views: 260
Reputation: 19555
If using Gnu Awk, you could process all your files with this stand-alone awk script:
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
}
make script executable:
chmod +x ./myAwkScript
run script to process all your files:
./myAwkScript somewhere/*
Upvotes: 0
Reputation: 52354
bash
parameter expansion makes both upper-casing the expansion of a variable and removing part of the resulting string easy:
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"
Upvotes: 1