francis fernandes
francis fernandes

Reputation: 41

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. 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 "[email protected]"  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 "[email protected]"  francis_test.csv| grep "$get_date")

To delete the record I use the below command

sed '/"$record"/d'

Upvotes: 0

Views: 623

Answers (2)

francis fernandes
francis fernandes

Reputation: 41

thanks but now i am facing issue with the the below command

get_date=$(grep -n "[email protected]" 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)

cant get the output all of a sudden it fails

Upvotes: 0

P.P
P.P

Reputation: 121357

Single quote prevents the variable record from being expanded. 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.bak '/'"$record"/'d'

Providing a suffix to -i makes a backup of the original file.

Upvotes: 1

Related Questions