-3

I have daily files contain too many rows, one of the column sometime there value is zero, I need to delete this row if the value is zero, in Linux, I have used awk to find the column but I don`t know how to delete the row.

2,4,3,1,2,2,2

2,4,3,1,0,2,2

2,4,3,1,2,2,2

2,4,3,1,2,2,2

in the above example the second row I need to delete it because there is zero in column 5


I am sorry it`s my first day here,

to get the row number where the zero I found I used : awk -F',' '{print NR, $28;}' then delete the rows from awk output, is it a good idea or find other ideas?

  • Please, post some sample data with the expected output along with the awk program you have tried with. Not my downvote or close vote, btw. – James Brown Sep 01 '20 at 09:17
  • I have added example, thanks – Hamza Sabatin Sep 01 '20 at 09:32
  • 2
    `awk -F, '$5 != 0' input.csv > output.csv` – Shawn Sep 01 '20 at 10:32
  • Does this answer your question? [How to delete from a text file, all lines that contain a specific string?](https://stackoverflow.com/questions/5410757/how-to-delete-from-a-text-file-all-lines-that-contain-a-specific-string) – Tsyvarev Sep 01 '20 at 21:19

2 Answers2

0

As you didn't show any effort, I won't give you a full solution, but a basic approach: instead of deleting the row, containing a zero, you might use grep -v ",0" to show only the rows who do not contain a zero value (you might need to extend this with the case where the row starts with a zero), and overwrite the file with only those rows.

Dominique
  • 16,450
  • 15
  • 56
  • 112
  • am sorry it`s my first day here, and I am trying to have the idea, not a full solution. I used awk to get row number for each column with zero then if I used sed is will help after getting result from awk ? awk -F',' '{print NR, $28;}' – Hamza Sabatin Sep 01 '20 at 11:27
0

dears, thanks for your support
I have tried @shawn comment and they success as below :

for i in `ls -lrt file_name* |cut -c51-76`    do    awk -F, '$28 != 0' $i > $i.new    mv $i $i.org    mv $i.new $i    done