-1

I have a text file with line containing only comma. How can I delete this particular line? Consider following example:

CREATE TABLE ENTERPRISE.TSDAILYACCTSUMMARY (
    TSDAILYACCTID FOR COLUMN TSDAYID    INTEGER INTEGER NOT NULL ,
    ,
    TIMESHEETDATE FOR COLUMN TSDATE     DATE DEFAULT NULL ,

How to delete third line?

4

try

 awk '$1 == "," && NF==1 { next ; } { print ; } ' file > other_file

or

 grep  -v '^[ ]*,[ ]*$' file > other_file

or

 sed -i -e '/^[ \t]*,[ \t]*$/d' original_file
1
perl -i -ne 'print if ! /^\s*,\s*$/' file
1

A more terse awk solution can be:

awk '!(NF==1&&$1==",")' /path/to/file
  • awk defaults to printing the entire line as long as the predicate/filter/condition is true.
  • In this case, the condition is not a single field (NF==1) being ",".
  • Therefore, only line 3 will not match the condition, and the other lines are printed out.

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.