5

I want to delete the lines containing empty fields in the last row $7

File:

1 1479870 5022248660 1 40001 189445122 740020
1 1911574 3015889020 1 33001 162049034 633004
1 1569783 5029193930 1 22001 133687297 522216
1 2069616 1025856960 2 25001 185608704 
1 1741598 5021128160 1 44001 164870942 644027
1 1052941 5020319300 1 10001 156161802 610007
1 1686734 5020347480 1 13001 131405824 513304
1 1872263 5029089700 1 23001 185092353 723017

Desired output :

1 1479870 5022248660 1 40001 189445122 740020
1 1911574 3015889020 1 33001 162049034 633004
1 1569783 5029193930 1 22001 133687297 522216
1 1741598 5021128160 1 44001 164870942 644027
1 1052941 5020319300 1 10001 156161802 610007
1 1686734 5020347480 1 13001 131405824 513304
1 1872263 5029089700 1 23001 185092353 723017
2
  • awk 'NF >= 7' <file, note that it does not guarantee that the last field is empty, example the line were also deleted when one of other fields is empty.
    – cuonglm
    Commented Feb 27, 2017 at 10:06
  • How are your fields separated? Any amount of blank characters or one space character ? Is 1 2  4 5 6 7 (where there are two spaces in between 2 and 4) a line with a missing 3rd field or one with a missing 7th field? Commented Feb 27, 2017 at 10:12

2 Answers 2

11

Try with this one :

awk  '$7!=""' file > final_output
1
  • @Nydenn If helped mark it as answered.
    – Rakesh.N
    Commented Feb 27, 2017 at 10:12
0

If the fields are separated by one space character as opposed to any sequence of blank characters, that if

1 2  4 5 6 7

Is a line that has a 7th field but no 3rd field, you'd do:

awk -F '[ ]' '$7 != ""' < file > final_output

[ ] is a regular expression that matches the space character. We don't use -F ' ' as a single space as the field separator has a special meaning (gives the default field splitting behaviour where any sequence of one or more blank characters act as one separator and leading and trailing blanks are ignored).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .