简体   繁体   English

Bash 脚本删除 CSV 文件中的行

[英]Bash script to delete line in CSV file

I have a CSV file with million records for different users, there are multiple records for each user.我有一个 CSV 文件,其中包含不同用户的百万条记录,每个用户有多个记录。 I am doing some processing on the file and managed to get the record for each user which I want to delete.我正在对文件进行一些处理,并设法获取我要删除的每个用户的记录。

I am using the below commands, but I can't delete the line I want to我正在使用以下命令,但我无法删除我想要的行

get_date=$(grep -n "xyz@abc.com"  francis_test.csv| awk -F, '{print $22}' | sed 's/"//g' | awk '{print $1" "$2}'  |  sort -k 1.7n -k 1.4,1.5n -k 1.1,1.2n | tail -n1)

record=$(grep -n "xyz@abc.com"  francis_test.csv| grep "$get_date")

To delete the record I use the below command要删除记录,我使用以下命令

sed '/"$record"/d'

Single quote prevents the variable record from being expanded.单引号防止变量record被扩展。 Quote it like:像这样引用它:

sed '/'"$record"/'d'

sed also has an option -i to in-place modification (if you want to actually delete the lines from the file). sed还有一个选项-i用于就地修改(如果您想实际从文件中删除行)。

sed -i.bak '/'"$record"/'d'

Providing a suffix to -i makes a backup of the original file.-i提供后缀会备份原始文件。

thanks but now i am facing issue with the the below command谢谢,但现在我面临以下命令的问题

get_date=$(grep -n "xyz@abc.com" francis_test.csv| awk -F, '{print $22}' | sed 's/"//g' | awk '{print $1" "$2}' | sort -k 1.7n -k 1.4,1.5n -k 1.1,1.2n | tail -n1) get_date=$(grep -n "xyz@abc.com" francis_test.csv| awk -F, '{print $22}' | sed 's/"//g' | awk '{print $1" "$2}' |排序 -k 1.7n -k 1.4,1.5n -k 1.1,1.2n | 尾 -n1)

cant get the output all of a sudden it fails突然无法获得 output 它失败了

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

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