awk简单使用

实践下来使用awk对文件进行读取和操作,要比shell本身来操作更快捷,并且awk本身支持数学运算,也可以使用rand函数获取随机数。

#/bin/bash
#echo $RANDOM
file="random_org.csv"

library="FICD_Gemini_6D27_PNL_V0_20250409_XXH"
cell="split1_coe_aa_array"
layout="layout"
position="lowerLeft"
angle="R0"
rvalue=10
output_file="random_cal.csv"

#check the file
if [ ! -f "$file" ]; then
  echo "CSV file doesn't exist"
  exit
fi

#read the file
#使用awk读取文件,并按照需要的格式输出
#-v可以将shell变量转换为awk变量,-F 按照行读取
#Begin 为读取文件前要执行的代码,OFS指定输出的间隔符
#当使用printf格式化输出时,OFS卸载格式化中,否则还是以空格来进行间隔
#NR>2表示只读取大于两行的数据

awk -v library="$library" -v cell="$cell" -v layout="$layout" -v position="$position" -v angle="$angle" -v rvalue="$rvalue" -F, '
  BEGIN {OFS=","}; 
  NR>2 {randx=(rand()*rvalue-rvalue/2)+$3;randy=(rand()*rvalue-rvalue/2)+$4; 
  printf "%s%s%s%s%s%s%.4f%s%.4f%s%s%s%s\n",library,OFS,cell,OFS,layout,OFS,randx,OFS,randy,OFS,position,OFS,angle}
  ' $file >> $output_file
#while IFS= read -r line; do
#done < $file
echo "work down!output $output_file"

 

posted @ 2025-06-05 13:38  color_bar  阅读(5)  评论(0)    收藏  举报