用于在新csv中创建新列的Case / if-else语句

时间:2017-01-16 00:23:14

标签: bash csv

我正在尝试在分析列的CSV文件(例如class MyButton : UIButton { var indexPath: IndexPath? } class personTableCell { var inviteButton: MyButton } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { cell.inviteButton.indexPath = indexPath // ... } func addInvite(sender:MyButton!) { print("Invite pressed for index \(sender.indexPath)") } )上执行case / if-else语句,然后在新的csv中创建新列(例如{{1} })。

源数据(myfile.csv)如下所示:

myfile_new.csv

我正在尝试进行两次转换:

  • 对于第二个字段,如果输入文件在字段中有任何数据,则为1,否则为0.
  • 第三个字段被展平为三个字段。如果输入文件在第三个字段中有myfile.csv,则第三个输出字段为unique_id,variable1,variable2 1,,C 2,1, 3,,A 4,,B 5,1, ,否则为A; 10以及输出文件中的第四个/第五个字段相同。

我希望结果(B)看起来像这样:

C

我正试图在myfile_new.csv

中做相同的事情
unique_id,variable1,variable2_A,variable2_B,variable2_C
1,0,0,0,1
2,1,0,0,0
3,0,1,0,0
4,0,0,1,0
5,1,0,0,0

我愿意接受任何内容,但CSV文件大小为500GB - 1TB,因此需要使用该大小的文件。

2 个答案:

答案 0 :(得分:2)

这是一个awk解决方案:

awk 'BEGIN {
    FS = ","
    OFS = ","
}

NR == 1 {
    $3 = "variable2_A"
    $4 = "variable2_B"
    $5 = "variable2_C"
    print
    next
}

{
    $2 = ($2 == "") ? 0 : 1
    $3 = ($3 == "A" ? 1 : 0) "," ($3 == "B" ? 1 : 0) "," ($3 == "C" ? 1 : 0)
    print
}' myfile.csv > myfile_new.csv

BEGIN块中,我们将输入和输出文件分隔符设置为逗号。

NR == 1块为输出文件创建标题并跳过第三个块。

第三个块检查第二个字段是否为空,并在其中存储01; $3语句连接三次使用三元运算符?:的结果,逗号分隔。

输出

unique_id,variable1,variable2_A,variable2_B,variable2_C
1,0,0,0,1
2,1,0,0,0
3,0,1,0,0
4,0,0,1,0
5,1,0,0,0

答案 1 :(得分:1)

使用while循环快速而肮脏的解决方案。

#!/bin/bash
#Variables:
line=""
result=""
linearray[0]=0

while read line; do
    unset linearray #Clean the variables from the previous loop
    unset result
    IFS=',' read -r -a linearray <<< "$line" #Splits the line into an array, using the comma as the field seperator
    result="${linearray[0]}""," #column 1, at index 0, is the same in both files.
    if [ -z "${linearray[1]}" ]; then #If column 2, at index 1, is empty, then...
        result="$result""0""," #Pad empty strings with zero
    else #Otherwise...
        result="$result""${linearray[1]}""," #Copy the non-zero column 2 from the old line
    fi
    #The following read index 2, for column 3, and add on the appropriate text. Only one can ever be true.
    if [ "${linearray[2]}" == "A" ]; then result="$result""1,0,0"; fi
    if [ "${linearray[2]}" == "B" ]; then result="$result""0,1,0"; fi
    if [ "${linearray[2]}" == "C" ]; then result="$result""0,0,1"; fi
    if [ "${linearray[2]}" == "" ]; then result="$result""0,0,0"; fi
    echo $result >> myfile_new.csv #append the resulting line to the new file
done <myfile.csv