将bash函数更改为awk [英] Changing the bash function to awk

查看:42
本文介绍了将bash函数更改为awk的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的团队设计了以下功能.它在Bash中.我必须为具有数十万条记录的文件调用此函数.这花费了太多时间.你们中有人可以建议我将以下功能更改为awk吗?

My team has designed below function. It is in Bash. I have to call this function for the file which has hundreds of thousands of records. This is taking too much time. Can anyone of you please suggest me to change the below function into awk?

 data_mask() {

  col_val=$1
  l_ret_str=""
  l_an=0
  l_lp=0
  l_mod=0
  absnum=0
  austart=65
  auend=90
  aclsize=26
  alstart=97
  alend=122
  nstart=48
  nend=57
  nclsize=10

  l_lp=`expr length "$col_val"`
  if [[ $l_lp -ne 0 ]]; then
    for i in `eval "echo {1..$l_lp}"`
    do
      single_char=$(SUBSTR "$col_val" $i)
      ascii_num_val=$(ASCII "$single_char")
      l_mod=$((l_mod+ascii_num_val))
    done

    l_mod=$((l_mod % nclsize))

    for i in `eval "echo {1..$l_lp}"`
    do
      single_char=$(SUBSTR "$col_val" $i)
      ascii_num_val=$(ASCII "$single_char")
      l_an=$ascii_num_val
      tempvar=$((l_an - l_lp - l_mod - i))
      absnum=$(ABS $tempvar)
      if [[ $l_an -ge $austart && $l_an -le $auend ]]; then
        tempmodval=$((absnum % aclsize))
        tempasciival=$((austart + tempmodval))
        l_ret_str=$l_ret_str$(CHR $tempasciival)
      elif [[ $l_an -ge $alstart && $l_an -le $alend ]]; then
        tempmodval=$((absnum % aclsize))
        tempasciival=$((alstart + tempmodval))
        l_ret_str=$l_ret_str$(CHR $tempasciival)
      elif [[ $l_an -ge $nstart && $l_an -le $nend ]]; then
        tempmodval=$((absnum % nclsize))
        tempasciival=$((nstart + tempmodval))
        l_ret_str=$l_ret_str$(CHR $tempasciival)
      else
        tempmodval=$((absnum % nclsize))
        tempasciival=$((austart + tempmodval))
        l_ret_str=$l_ret_str$(CHR $tempasciival)
      fi

    done
  fi
  echo "$l_ret_str"
}

我通过使用以下登录名来调用此函数.我必须称呼它为特定的专栏.该列由用户输入.所以我将字符串分成三部分.

I am calling this function by using below login. I have to call it for specific column. That column is entered by the user. So I am splitting the string into 3 parts.

  while read p; do

  if [[ $line -le $skip_line ]]; then
    echo "$p" >> $outputfile
  else
    pre_str=`echo $p | cut -d'|' -f1-$((colnum - 1))`
    column_value=`echo $p | cut -d'|' -f$colnum`
    post_str=`echo $p | cut -d'|' -f$((colnum + 1))-$totalcol`
    echo "column_value=$column_value"
    maskvalue=$(data_mask "$column_value")
    #echo $pre_str"|"$maskvalue"|"$post_str >> $outputfile
#    awk -v col=2 'BEGIN { FS=OFS="|" } col<=NF { $col = data_mask(" $col ") } 1' $temp_outputfile >>123.txt
     awk -v col=3 'BEGIN { FS=OFS="|" } col<=NF { $col = $maskvalue; print }' $temp_outputfile >123.txt
#     awk -F"|" -vOFS="|" 'NR==1{$3=100} {print}' file
  fi

  line=$((line + 1))

  done < $file

你们中的任何人都可以建议我如何进行优化吗?

Could anyone of you please suggest me what to do for optimization?

推荐答案

尝试一下:

awk -v col=3 'col<=NF { $col = "FUNCTION(" $col ")" } 1' file

您可以轻松地将分隔符更改为管道:

You can change separator to pipe easily:

awk -v col=3 'BEGIN { FS=OFS="|" } col<=NF { $col = "FUNCTION(" $col ")" } 1' file

这可以在任何AWK中使用.

This should work in any AWK.

这篇关于将bash函数更改为awk的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆