gpt4 book ai didi

linux - awk 命令来查找和替换值

转载 作者:行者123 更新时间:2023-12-03 09:59:04 25 4
gpt4 key购买 nike

我需要帮助 w.r.t.下面的代码我很难理解。如果有人能抽出一点时间来解释这一点,真的很感激。

它的主要作用是根据 file1 的 col1 中的值在 file2 中基于 col1 定位一行并替换 file2 中的 7 个列值,这些列的位置通过 -v 标志作为输入提供。

awk --lint '-F|' -v col1pos=5 -v col2pos=29 -v col3pos=114 -v col4pos=115 -v col5pos= -v col6pos=26 -v col7pos= -v col8pos= 'BEGIN{OFS = FS; a[var 2] = col1pos; a[var 3] = col2pos; a[var 4] = col3pos; a[var 5] = col4pos; a[var 6] = col5pos; a[var 7] = col6pos; a[var 8] = col7pos; a[var 9] = col8pos} NR == FNR {a[$1] = $2; for (i
= 3; i <= 9; ++i) a[$1 FS i] = $i; next} $col1pos in a {for (i = 9; i >= 3; --i) if (length(a[var i]) > 0) $(a[var i]) = a[$col1pos FS i]; $col1pos = a[$col1pos]}1' file1 file2

最佳答案

由于没有提供示例,因此很难理解您的完整问题;如果我们谈论代码,那么我已尽力解释如下。请仔细阅读,如有任何疑问,请告诉我。

代码说明:

awk --lint '-F|' -v col1pos=5 -v col2pos=29 -v col3pos=114 -v col4pos=115 -v col5pos= -v col6pos=26 -v col7pos= -v col8pos= '                   ##Starting awk program from here and mentioning all variables with values as per OP need.
BEGIN{ ##Starting BEGIN section of awk program which will be executed before Input_file reading.
OFS = FS ##Setting OFS to FS (1st point)
a[var 2] = col1pos ##Creating an array named a whose index is var 2(2nd point) and setting its value to variable col1pos. Similarly creating other elements of array in next statements.
a[var 3] = col2pos
a[var 4] = col3pos
a[var 5] = col4pos
a[var 6] = col5pos
a[var 7] = col6pos
a[var 8] = col7pos
a[var 9] = col8pos
} ##Closing BLOCK for BEGIN section here.
NR == FNR{ ##Checking condition FNR==NR which will be TRUE when first Input_file named file1 is being read.
a[$1]=$2 ##Creating an array named a whose index is $1 and value is $2 of current line.
for(i=3;i<=9;i++){ ##Starting a for loop which starts from i=3 to till value of 9 here.
a[$1 FS i]=$i ##Creating an array named a whose index is $1 FS and i and value is $i(i variable value column)
}
next ##next will make sure no further statements are getting executed from here.
} ##Closing BLOCK for FNR==NR condition here.
($col1pos in a){ ##Checking condition if field number whose value is variable col1pos is present in array a, if yes then go further.
for(i = 9; i >= 3; --i){ ##Starting a for loop from i=9 to till value of 3.
if(length(a[var i]) > 0){ ##Checking condition if length of array a with index var i value is greater than 0 then do following.
$(a[var i]) = a[$col1pos FS i] ##Assigning field value(which will be decided on value of variable a[var i]) and setting it to value of array a with index $col1pos FS i.
} ##Closing if condition BLOCK here.
$col1pos = a[$col1pos] ##Setting value of field(which will be decided as per variable value col1pos) and setting it to array a value whose value is, value of field(col1pos).
} ##Closing BLOCK for; for loop here.
} ##Closing BLOCK for condition here.
1 ##Mentioning 1 will print edited/non-edited lines.
' file1 file2 ##Mentioning Input_file names here.

注意:对于代码的改进,我提到了2点,在这里提到它们的含义。
  • 第一点:无需设置 OFS=FS因为默认情况下两者都与空格具有相同的值。
  • 第二点:设置数组索引时要小心,如果它是一个字符串,则使用 a["string"]或者在使用变量 try a[variable] 的情况下
  • 关于linux - awk 命令来查找和替换值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59052960/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com