1

How can I remove empty /blank lines in every txt file of a directory (ideally subdirectories too)?

find . -name '*.txt' -exec ex '+%s/\ / /g' -cwq {} \;

Above code is pulling list of files correctly but i am not sure what regular expression to pass to remove blank lines.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
Ammad
  • 4,031
  • 12
  • 39
  • 62

1 Answers1

2

With GNU find and GNU sed:

find . -name '*.txt' -exec sed -ri '/^\s*$/d' {} \;
Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • 1
    afaik inplace editing works with multiple files as well, so you can use bulk exec here – oguz ismail Oct 25 '19 at 17:35
  • The `-r` option is a GNU extension. This could comfortably be refactored to standard POSIX `sed`; but then the `-i` option is also not strictly standard (though widely available). Maybe for maximum portability try `find ... -exec perl -ni -e 'print unless /^\s*$/' {} \;` even though paradoxically Perl is not officially standardized at all. (Maybe also see if your `find` supports `-exec ... {} +`.) – tripleee Oct 25 '19 at 17:39