0

So I've got a CSV file, test.csv, with the following:

Apples
Pears
Oranges

They would be comma separated given its a csv file.

I have a variables

test="app"

I want to remove anything from the csv file with that variable contents in it. So the output would be in a new file test1.csv:

Pears
Oranges
Inian
  • 80,270
  • 14
  • 142
  • 161
Kingswoop
  • 148
  • 8
  • Does `apPles` would also be removed? – KamilCuk Feb 11 '20 at 11:41
  • http://geekbraindump.blogspot.com/2010/06/case-insensitive-regex-in-bash.html – JGFMK Feb 11 '20 at 12:04
  • `grep -vi "app" test.csv` – kvantour Feb 11 '20 at 13:06
  • 1
    Does this answer your question? [How to invert a grep expression](https://stackoverflow.com/questions/4373675/how-to-invert-a-grep-expression) in combination with [How to grep for case insensitive string in a file?](https://stackoverflow.com/questions/48492422/) – kvantour Feb 11 '20 at 13:07

2 Answers2

1

You can use sed to do that.

If you want to remove the entire row that contains test variable's value. Then try-

user@localhost$ sed "/$test/d" test.csv > output.csv

If you want to replace the word that holds the var test, then try -

user@localhost$ replacewith=''
user@localhost$ sed "s/$test/$replacewith/g" test.csv > output.csv

NB: Replace and Delete can mean same thing if you use blank string as replacement.

Raihan Kabir
  • 447
  • 2
  • 10
0

You can obtain such a result with sed:

sed '/dog/d' inputfile.csv > outputfile.csv

You can also make it case insensitive:

sed '/[dD][oO][gG]/d' inputfile.csv > outputfile.csv

The syntax used in the latter is RegEx, read more about it here.

Pitto
  • 8,229
  • 3
  • 42
  • 51