将文本文件中的所有数字缩写转换为数值

yfjy0ee7  于 2021-06-26  发布在  Impala
关注(0)|答案(9)|浏览(358)

我想将文本文件中的所有数字缩写(如1k、100k、1m等)转换为纯数字值(如1000、100000、1000000等)。
例如,如果我有以下文本文件:

1.3K apples
87.9K oranges
156K mangos
541.7K carrots
1.8M potatoes

我想在bash中将其转换为以下内容:

1300 apples
87900 oranges
156000 mangos
541700 carrots
1800000 potatoes

我使用的命令是将匹配的数字缩写字符串替换为完整的数字值,如下所示:

sed -e 's/1K/1000/g' -e 's/1M/1000000/g' text-file.txt

我的问题是,当发生变化时,我无法找到并替换所有可能的数字缩写。我想这样做,直到至少有一个十进制缩写。

wlwcrazw

wlwcrazw1#

鉴于:

$ cat file
1.3K apples
87.9K oranges
156K mangos
541.7K carrots
1.8M potatoes

只是为了傻笑,纯粹的bash(sed和bc):

while read -r x y 
do 
    new_x=$(echo "$x" | sed -E 's/^([[:digit:].]*)[kK]/\1\*1000/; s/^([[:digit:].]*)[mM]/\1\*1000000/' | bc)
    printf "%'d %s\n" "$new_x" "$y"
done <file

印刷品:

1,300 apples
87,900 oranges
156,000 mangos
541,700 carrots
1,800,000 potatoes
p5cysglq

p5cysglq2#

这将执行全局替换(如果每行有>1个字符串要转换):

perl -pe 's{\b(\d+(?:\.\d+)?)([KM])\b}{ $1*1000**(index("KM",$2)+1) }ge' file
wqsoz72f

wqsoz72f3#

使用 numfmt 从GNUCoreutils,不要重新发明轮子。

$ numfmt --from=si <file
1300 apples
87900 oranges
156000 mangos
541700 carrots
1800000 potatoes

如果缩写数字可能显示为任何字段,则可以使用:

numfmt --from=si --field=- --invalid=ignore <file
nukf8bse

nukf8bse4#

另一个 awk 变体:

awk '{q = substr($1, length($1));
$1 *= (q == "M" ? 1000000 : (q=="K"?1000:1))} 1' file

1300 apples
87900 oranges
156000 mangos
541700 carrots
1800000 potatoes
pprl5pva

pprl5pva5#

以编程的方式,基于这个答案,您可以创建所有可能的转换因子的列表,并在需要时执行乘法:

awk 'BEGIN{f["K"]=1000; f["M"]=1000000}
     match($1,/[a-zA-Z]+/){$1 *= f[substr($1,RSTART,RLENGTH)]}
     1' file
x8diyxa7

x8diyxa76#

gnu awk for gensub():

$ awk '
    BEGIN { mult[""]=1; mult["k"]=1000; mult["m"]=100000 }
    { $1 *= mult[gensub(/[^[:alpha:]]/,"","g",tolower($1))] }
1' file
1300 apples
87900 oranges
156000 mangos
541700 carrots
180000 potatoes
l2osamch

l2osamch7#

你能试着用gnu里的样品来写,测试一下吗 awk .

awk '
{
  if(sub(/[kK]$/,"",$1)){
    $1*=1000
  }
  if(sub(/[mM]$/,"",$1)){
    $1*=1000000
  }
}
1
' Input_file

说明:增加了对以上内容的详细说明。

awk '                     ##Starting awk program from here.
{
  if(sub(/[kK]$/,"",$1)){ ##Checking condition if 1st field ends with k/K then do following. Substituting k/K in first field with NULL here.
    $1*=1000              ##Multiplying 1000 with current 1st field value here.
  }
  if(sub(/[mM]$/,"",$1)){ ##Checking condition if 1st field ends with m/M then do following. Substituting m/M in first field with NULL here.
    $1*=1000000          ##Multiplying 1000000 with current 1st field value here.
  }
}
1                         ##1 will print current line here.
' Input_file              ##Mentioning Input_file name here.

输出如下。

1300 apples
87900 oranges
156000 mangos
541700 carrots
1800000 potatoes
kb5ga3dv

kb5ga3dv8#

这可能适用于您(gnu-sed):

sed -E '1{x;s/^/K00M00000/;x}
        :a;G;s/([0-9])(\.([0-9]))?([KM])(.*)\n.*\4(0*).*/\1\3\6\5/i;ta
        P;d' file

创建查找并将其存储在保留空间中。
将查找附加到每行,并使用模式匹配将查找中的键替换为其值。
最后在没有找到匹配项时打印行。

e5nszbig

e5nszbig9#

另一种选择可能是仅使用bash和带有捕获组的模式,您可以在其中捕获任意一个组 M 或者 K . 如果模式匹配,则测试其中一个并设置乘数和使用 bc ```
while IFS= read -r line
do
if $line =~ ^(:digit:+(.:digit:+)?)([MK])( .*)$ ;then
echo "$(bc <<< "${BASH_REMATCH[1]} * $([ ${BASH_REMATCH[3]} == "K" ] && echo "1000" || echo "1000000") / 1")${BASH_REMATCH[4]}"
fi
done < text-file.txt

输出

1300 apples
87900 oranges
156000 mangos
541700 carrots
1800000 potatoes

bash演示

相关问题