仅在 awk 命令的第一个参数中删除变音符号(重音符号)

Remove diacritics (accents) only in the first parameter of awk command

我有一个主要 shell 可以将文本从源文件重新格式化为目标文件 :

来源:

Libelléacte;CHAR(20);Libellé de l'acte;

目标:

 * Libellé de l'acte.
   
    05 Libelléacte PIC X(20).

我只想删除第一个参数的变音符号。我尝试使用 iconv 命令将我的文件转换为 ascii//TRANSLIT//IGNORE,但它删除了所有变音符号,这不是我想要的。

这是我的重新格式化代码:

for f in $TEMP_DIRECTORY 

do 
    b=$(basename "$f")
    echo "Generating $f file in copy.."; 
    awk -F ';' '
toupper()=="TABLE" {printf "01 %s.\n\n", ; next} 
toupper()=="EXTRACTION" {printf "01 %s.\n\n", ; next} 
{
  result = 
  if ( ~ /^Numérique [0-9]+(\.[0-9]+)?$/) {
    nr=split(,a,"[ .]")
    result = "PIC 9(" a[2] ")"
    if (nr == 3) {
      result = result ".v9(" a[3] ")"
    }    
  }
  sub(/CHAR/,"PIC X", result);
  sub(/Char/,"PIC X", result);
  sub(/char/,"PIC X", result);
  sub(/Entier/,"PIC 9(9)", result);
  sub(/entier/,"PIC 9(9)", result);
  gsub(/user/,"user-field");
  gsub(/User/,"user-field");
  gsub("/","_");
  printf "   * %s.\n\n     05 %s %s.\n\n", , , result;
}' "$f" > "$TARGET_DIRECTORY/${b%%.*}.cpy"
done

我只需要更改第一个参数就可以得到这个输出:

 * Libellé de l'acte.

    05 Libelleacte PIC X(20).

首先我会用cut获取分号前的第一个参数,然后iconv音译成ASCII,最后tr 通过删除 [:punct:] POSIX 字符 class.

来删除非字母数字
cat test | cut -d \; -f 1 | iconv -f UTF-8  -t ASCII//TRANSLIT | tr -d "[:punct:]"

我下面的原始答案是每行输入调用一次 iconv,这样效率会高得多:

$ iconv -f utf8 -t ascii//ignore file |
    awk 'BEGIN{FS=OFS=";"} NR==FNR{a[NR]=; next} {=a[FNR]; print}' - file
Libellacte;CHAR(20);Libellé de l'acte;

或者如果您愿意:

$ paste -d';' <(cut -d';' -f1 file | iconv -f utf8 -t ascii//ignore) <(cut -d';' -f2- file)
Libellacte;CHAR(20);Libellé de l'acte;

或者如果您总是知道输入字段的数量:

$ iconv -f utf8 -t ascii//ignore file | paste -d';' - file | cut -d';' -f1,6-
Libellacte;CHAR(20);Libellé de l'acte;

很多选项。

如果上面的调用不正确,请将 iconv 命令更改为您已经知道的任何内容(假设您在问题中说 I tried to convert my file to ascii//TRANSLIT//IGNORE with iconv command but it removes all diacritics)。


原回答:

#!/usr/bin/env bash
while IFS=';' read -r f1 rest; do
    printf '%s;%s\n' "$(iconv -f utf8 -t ascii//ignore <<<"$f1")" "$rest"
done < file
Libellacte;CHAR(20);Libellé de l'acte;