Home > Net >  Rename output file in uppercase without the extensions bash
Rename output file in uppercase without the extensions bash

Time:01-06

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

CodePudding user response:

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"
  •  Tags:  
  • Related