Jay0351 发表于 2021-11-30 05:29:39

关于bash语言对csv文件的修改

root@8b44b1b5215b:/# head -n 20 /bashdm.csv
INDEX-Name-Age-Country-Height-Hair_Colour-YLA-CONF
0-#]Jeanne Wallace-86-CODE:MY-157-White-1-YES
1-#]Anthony Gentry-49-CODE:QA-162-Black-1-YES
2-#]Marcia Jones-75-CODE:IE-186-Dyed-1-YES
3-"#]James Patch-Walker-Willis"-18-CODE:KI-166-Ginger-1-YES
4-#]Vickie White-42-CODE:CO-160-Blonde-1-YES
5-#]Dwayne Peterson-70-CODE:UZ-142-Dyed-1-YES
6-#]Rose Rubottom-79-CODE:PL-141-White-1-YES
7-#]Thomas Salas-30-CODE:ML-156-Black-1-YES
8-#]Xiao Uong-41-CODE:AD-159-Blonde-1-YES
9-#]Lakisha Stewart-48-CODE:CF-182-Blonde-1-YES
10-#]Claire Nunes-43-CODE:SN-171-Black-1-YES
11-#]Cheryl Person-73-CODE:CU-155-Blonde-1-YES


已知csv文件头20行如上
如何通过bash语言将Name前面的#] 去除掉并保存新的csv文件呢?
注意第三行开头还有一个”

谢谢大家

人造人 发表于 2021-11-30 11:21:45

$ cat bashdm.csv
INDEX-Name-Age-Country-Height-Hair_Colour-YLA-CONF
0-#]Jeanne Wallace-86-CODE:MY-157-White-1-YES
1-#]Anthony Gentry-49-CODE:QA-162-Black-1-YES
2-#]Marcia Jones-75-CODE:IE-186-Dyed-1-YES
3-"#]James Patch-Walker-Willis"-18-CODE:KI-166-Ginger-1-YES
4-#]Vickie White-42-CODE:CO-160-Blonde-1-YES
5-#]Dwayne Peterson-70-CODE:UZ-142-Dyed-1-YES
6-#]Rose Rubottom-79-CODE:PL-141-White-1-YES
7-#]Thomas Salas-30-CODE:ML-156-Black-1-YES
8-#]Xiao Uong-41-CODE:AD-159-Blonde-1-YES
9-#]Lakisha Stewart-48-CODE:CF-182-Blonde-1-YES
10-#]Claire Nunes-43-CODE:SN-171-Black-1-YES
11-#]Cheryl Person-73-CODE:CU-155-Blonde-1-YES
$ sed -r 's/^.*?#]//' bashdm.csv
INDEX-Name-Age-Country-Height-Hair_Colour-YLA-CONF
Jeanne Wallace-86-CODE:MY-157-White-1-YES
Anthony Gentry-49-CODE:QA-162-Black-1-YES
Marcia Jones-75-CODE:IE-186-Dyed-1-YES
James Patch-Walker-Willis"-18-CODE:KI-166-Ginger-1-YES
Vickie White-42-CODE:CO-160-Blonde-1-YES
Dwayne Peterson-70-CODE:UZ-142-Dyed-1-YES
Rose Rubottom-79-CODE:PL-141-White-1-YES
Thomas Salas-30-CODE:ML-156-Black-1-YES
Xiao Uong-41-CODE:AD-159-Blonde-1-YES
Lakisha Stewart-48-CODE:CF-182-Blonde-1-YES
Claire Nunes-43-CODE:SN-171-Black-1-YES
Cheryl Person-73-CODE:CU-155-Blonde-1-YES
$ sed -r 's/^.*?#]//' bashdm.csv > new.csv
$ cat new.csv
INDEX-Name-Age-Country-Height-Hair_Colour-YLA-CONF
Jeanne Wallace-86-CODE:MY-157-White-1-YES
Anthony Gentry-49-CODE:QA-162-Black-1-YES
Marcia Jones-75-CODE:IE-186-Dyed-1-YES
James Patch-Walker-Willis"-18-CODE:KI-166-Ginger-1-YES
Vickie White-42-CODE:CO-160-Blonde-1-YES
Dwayne Peterson-70-CODE:UZ-142-Dyed-1-YES
Rose Rubottom-79-CODE:PL-141-White-1-YES
Thomas Salas-30-CODE:ML-156-Black-1-YES
Xiao Uong-41-CODE:AD-159-Blonde-1-YES
Lakisha Stewart-48-CODE:CF-182-Blonde-1-YES
Claire Nunes-43-CODE:SN-171-Black-1-YES
Cheryl Person-73-CODE:CU-155-Blonde-1-YES
$

jackz007 发表于 2021-11-30 11:58:54

本帖最后由 jackz007 于 2021-11-30 15:45 编辑

      假定 csv 文件是当前目录下的 "data.csv",bash 脚本文件是当前目录下的 "x"
#!/bin/bash
while read -r line
do
    while [[ ${line} =~ "\\"" ]]
    do
      line=${line%%\\"*}${line#*\\"}
    done
    if [[ ${line} =~ "#]" ]]
    then
      line=${line%%#]*}${line#*#]}
    fi
    echo ${line}
done < data.csv
这样用
chmod u+x x
./x > data2.csv
      这样,当前目录下新产生的文件 "data2.csv" 中就会有处理结果了

Jay0351 发表于 2021-11-30 23:44:07

人造人 发表于 2021-11-30 11:21


谢谢~

Jay0351 发表于 2021-11-30 23:44:39

jackz007 发表于 2021-11-30 11:58
假定 csv 文件是当前目录下的 "data.csv",bash 脚本文件是当前目录下的 "x"

这样用


感谢感谢,学到了
页: [1]
查看完整版本: 关于bash语言对csv文件的修改