如果 2 个不同 CSV 文件中 2 个不同行的元素使用 Bash 脚本匹配,则添加数据

问题描述 投票:0回答:1

我对 Bash 脚本非常陌生,仍处于学习阶段。目前,我正在处理 CSV 文件,并且有一个要求,如果 2 行在 2 个不同的 CSV 文件中匹配,则应添加数据列。

例如,

csv_file1.csv:

Account,Entity,Data
A1101,E101,1000

csv_file2.csv:

Account,Entity,Data
A1101,E101,2000
A1101,E102,2000

执行 bash 脚本时,预期输出应该是一个新的 CSV 文件,其中包含以下行:

Account,Entity,Data
A1101,E101,3000
A1101,E102,2000

由于“csv_file1”第一行中的元素和“csv_file2”第一行中的元素匹配,因此应附加“数据”列中的数据。

下面是我尝试打印两个 CSV 文件内容的代码。但是,我无法从这里继续前进。

#!/bin/bash
#assigns the CSV file to a variable
FILE1="csv_file1.csv"

#reads each line and splits it into three variables

while IFS=, read -r Account Entity Data; do

echo "$Account#""$Entity#""$Data"

done < <(tail -n +2 $FILE1)

#assigns the CSV file to a variable
FILE2="csv_file2.csv"

#reads each line and splits it into three variables

while IFS=, read -r Account Entity Data; do

echo "$Account#""$Entity#""$Data"

done < <(tail -n +2 $FILE2)

如果你们能帮助我提出你们的想法,那就太好了。如果需要任何其他信息,请告诉我。非常感谢!

bash csv git-bash
1个回答
0
投票

== 文件:chk.awk ==

@load "filefuncs"
{
  #// input: file=xxx.csv

  #// check if $file defined
  if (file=="") {
    printf ("Please use `-v file=xxx.csv` to define a csv file\n");
    exit 2;
  }

  #// check if inout file exist
  ret=stat(file,fdata)
  if (ret<0) {
    printf ("File '%s' seems not exist !?\n", file);
    exit 3;
  }

  printf ("* NR=%d\n", NR);

  printf ("1st file:'%s'\n", $0);
  #// Read from 2nd file
  ret = getline sec < file
  if (ret)
    printf ("2nd file:'%s'\n", sec);
  else
    printf ("2nd file already EOF\n");
    #//? Terminated ? 
} END {
  #// 1st file END, but how about 2nd?
  if (ret) {
    printf ("2nd left: --\n");
    while (ret) {
      ret = getline sec < file
      if (ret) printf ("2nd: '%s'\n", sec );
    }
  }
}

打开命令行:

$ gawk -k  -v file=2.csv -f chk.awk 1.csv
* NR=1
1st file:'Account,Entity,Data'
2nd file:'Account,Entity,Data'
* NR=2
1st file:'A1101,E101,1000'
2nd file:'A1101,E101,2000'
2nd left: --
2nd: 'A1101,E102,2000'
2nd: 'A1101,E102,2000'
2nd: 'A1101,E102,2000'
2nd: 'A1101,E102,2000'

$ gawk -k  -v file=1.csv -f chk.awk 2.csv
* NR=1
1st file:'Account,Entity,Data'
2nd file:'Account,Entity,Data'
* NR=2
1st file:'A1101,E101,2000'
2nd file:'A1101,E101,1000'
* NR=3
1st file:'A1101,E102,2000'
2nd file already EOF
* NR=4
1st file:'A1101,E102,2000'
2nd file already EOF
* NR=5
1st file:'A1101,E102,2000'
2nd file already EOF
* NR=6
1st file:'A1101,E102,2000'
2nd file already EOF

您现在可以自己比较这2个文件的不同之处

© www.soinside.com 2019 - 2024. All rights reserved.