-2

This is my awk code:

awk -F"," 'NF!= 8' myfile.csv 

How can I delete only lines that have 8 fields.

jxn
  • 7,685
  • 28
  • 90
  • 172
  • 3
    What's the difference in your mind between `delete lines with 8 fields` and `print lines that do not have 8 fields`? You're not seriously asking how to change `!=` to `==` are you? – Ed Morton Aug 18 '15 at 19:12
  • 1
    If you change `!=` to `==`, your code will print the lines that have 8 fields. Using `=` instead of `==` won't do what you want. However, I'm really not clear what you're asking... – Jonathan Leffler Aug 18 '15 at 19:12
  • I accidentally approved an edit to my question that was wrong. I just want to delete lines with fields ==8. – jxn Aug 18 '15 at 19:16
  • And the code in the updated question (`awk 'NF!=8'`) prints only those lines that do not have 8 fields, effectively deleting those that do have 8 fields. What's the problem? Why are you asking a question when your code contains the answer? – Jonathan Leffler Aug 18 '15 at 19:16
  • Is the question how to use that to modify the original file? – Etan Reisner Aug 18 '15 at 19:20
  • 1
    If @EtanReisner is correct then you want http://stackoverflow.com/a/16529730/1066031 – Chris Seymour Aug 18 '15 at 19:22
  • Post some sample input and expected output. I wonder if the problem you're having is that you're telling awk to use `,` as the Field Separator but your input file isn't separated by commas. I know the suffix is `.csv` but we see that with fields separate by tabs, semi-colons, pipe symbols, etc. – Ed Morton Aug 18 '15 at 20:12

1 Answers1

2

Here you go (this prints lines with 8 fields as originally asked)

awk -F, 'NF==8' myfile.csv

The question changed, you want to remove the lines with 8 fields. One way to do this

awk -F, 'NF!=8' myfile.csv > temp && mv temp mvfile.csv

NB. updated as per comments

karakfa
  • 66,216
  • 7
  • 41
  • 56
  • This prints the lines that have 8 fields. I want to use awk to delete those lines with 8 fields. (Sorry someone edited my question and it turned out wrong.) – jxn Aug 18 '15 at 19:14