简体   繁体   English

如何使用 AWK 或 SED 替换 CSV 文件中的多个列?

[英]How to replace multiple columns in CSV file using AWK or SED?

I want to replace the column in a line with some keys in a file我想用文件中的一些键替换一行中的列

for val in "${keyarr[@]}";
do
    while read dataline <&3 && read keyline <&4; do
        echo $dataline |
        awk -F $ifs -v col=$val -v line=$keyline '{
            $col=line; print $0 ;} ' OFS=$ifs > sourcedata1
    done 3<sourcedata1 4<keysnotfound
done

val is the column number that I want to replace with a key. val是我想用一个键替换的列号。

So I may have multiple columns to replace.所以我可能有多个列要替换。

This solution does not work.此解决方案不起作用。 how can I replace multiple columns with the keys all at once.如何一次用键替换多个列。

for example I fetch a line from sourcedata1例如,我从sourcedata1获取一行

101, 102, 103 , 104

and from keynotfound in the while loop并来自 while 循环中的keynotfound

105

And at the first iteration of for loop val = 1在 for 循环的第一次迭代中val = 1

then I want to replace 1st column of the dataline with the key然后我想用键替换数据线的第一列

105, 102, 103, 104

and same for the second iteration if val = 3 .如果val = 3 ,则第二次迭代相同。

105, 102, 105, 104

Instead of using for loop, I want a solution in AWK or SED itself and it should update the source file with keys in multiple column.我不想使用 for 循环,而是想要 AWK 或 SED 本身的解决方案,它应该使用多列中的键更新源文件。

Your problem statement is rather obscure, but I'm guessing something like this refactoring should do what you are trying to ask.您的问题陈述相当晦涩,但我猜像这样的重构应该可以满足您的要求。

Instead of loop over the same files, and rewriting one of them, just pass in the array of column indices, and read the files just once.无需循环遍历相同的文件并重写其中一个,只需传入列索引数组,然后只读取一次文件。

cols=$(printf '%s;' "${keyarr[@]}")
awk -F "$ifs" -v c="${cols%;}" 'BEGIN { split(c, cols, ";"); OFS=IFS }
    NR==FNR { repl[FNR] = $0; next }
    { for(col in cols) $col = repl[FNR] }' keysnotfound sourcedata1

The Awk idiom NR==FNR {...; next } Awk 成语NR==FNR {...; next } NR==FNR {...; next } is a common way to read the first input file into memory (because the overall input line number NR is equal to the line number within the current file FNR ). NR==FNR {...; next }是将第一个输入文件读入 memory 的常用方法(因为整体输入行号NR等于当前文件FNR中的行号)。 We read the entries from keysnotfound into repl which is indexed by the line number.我们将keysnotfound中的条目读入repl ,repl 由行号索引。 With that, we can fall through and process the second file, where we loop over the column indices, and replace each one with the value we collected from the first file for this line number.有了这个,我们可以遍历并处理第二个文件,我们在其中循环列索引,并将每个索引替换为我们从第一个文件中为该行号收集的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM