-1

How to remove the lines in .csv file that second field contain the word content?

example :

ams-hbase-log4j,content:\n#LicensedtotheApacheSoftwareFoundation(ASF)underone\n#ormorecontributorlicenseagreements
ams-log4j,content:\n#\n#LicensedtotheApacheSoftwareFoundation(ASF)underone\n#ormorecontributorlicenseagreements.
ams-site,timeline.metrics.cache.size:150,

expected output

ams-site,timeline.metrics.cache.size:150,
  • Warning: This specific example does not appear to have any embedded commas in the first field. For example, in "blah, blah",content:\n ..., the comma inside the double-quotes is not a field separator. If someone reads this and has to deal with those, the answer provided here will not work correctly. Parsing CSV correctly to deal with such non-separating commas is tricky business. – Monty Harder Sep 13 '18 at 21:55
5

With sed and to remove the lines that second field contains content, you could do:

sed '/^[^,]*,[^,]*content/d' infile

or to remove the lines which second field stars with content:

sed '/^[^,]*,content/d' infile
4
awk -F, '$2 !~ /content/' file.csv

If you mean "remove lines where the 2nd field starts with "content", then

awk -F, '$2 !~ /^content/' file.csv
-1

basically what you can do in the easiest way is:

cat "/dir/yourfile.csv" | grep -v ",content:" > newcsvfile.csv

if you want to use the same file name then you can do:

cat "/dir/yourfile.csv" | grep -v ",content:" > tmpfile && mv tmpfile "/dir/yourfile.csv"
  • 2
    What does this do if "content" is in the third position of a line? – Christian Gibbons Sep 13 '18 at 18:07
  • that's an assumption if "content:" word will be only in the 2nd field, if it is in different field then the solution would not work and would be different – Javier Salas Sep 13 '18 at 18:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.