0

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'
3
  • 2
    I feel like with that many records, a proper database (Sqlite3 would be the simplest to use) might be better than CSV for pretty much anything you want to do with it. Commented Mar 22, 2021 at 11:26
  • 1
    millions of records you say? you might want to switch to at the very lest SQLlite. However you are using 3 tools here where you can use one. all you need is awk or sed. I think things are getting list in the piping. However you are using sed wrong here. you want sed -i "///" formate not sed with the d operator Commented Mar 22, 2021 at 11:30
  • 1
    Welcome to StackOverflow! For us to be able to help please provide your efforts as shown in Minimal, Reproducible Example. Commented Mar 22, 2021 at 11:32

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.