首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将输出文件重命名为大写,不带扩展名bash

将输出文件重命名为大写,不带扩展名bash
EN

Stack Overflow用户
提问于 2022-01-06 10:44:58
回答 2查看 117关注 0票数 1

在一个shell中,我浏览几个文件来重新格式化它们,所以我生成一个输出,文件名+ .cpy扩展名。

例如,我生成到test.des.utf8.cpy的test.des.utf8,我想要做的是从test.des.utf8到TEST.cpy。

我试过这样的方法,但对我没有用:

代码语言:javascript
复制
"$f" "${f%.txt}.text"

下面是输出重定向的shell:

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-01-06 10:57:03

bash参数展开使上大小写变量的扩展和移除部分结果字符串变得容易:

代码语言:javascript
复制
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"
票数 1
EN

Stack Overflow用户

发布于 2022-01-06 12:07:30

如果使用Gnu Awk,可以使用以下独立的awk脚本处理所有文件:

myAwkScript

代码语言:javascript
复制
#!/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
}

使脚本可执行:

代码语言:javascript
复制
chmod +x ./myAwkScript

运行脚本来处理所有文件:

代码语言:javascript
复制
./myAwkScript somewhere/*
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70605929

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档