Remove all empty lines in a file

You can translate the content of this page by selecting a language in the select box.

AI Unraveled: Demystifying Frequently Asked Questions on Artificial Intelligence

How to Remove all empty lines in a file on Linux and Windows?

Remove empty lines from file.txt via Linux command line

  • Option 1: sed -i ‘/^$/d’ file.txt
  • Option 2: awk ‘NF > 0’ file.txt > output.txt
  • Option 3: perl -i.backup -n -e “print if /\S/” file.txt
  • Option 4: grep . file.txt > output.txt

Remove empty lines from file.txt using Powershell script on Windows

  • (gc file.txt) | ? {$_.trim() -ne “” } | set-content file.txt
  • Search all files containing a specific string

    You can translate the content of this page by selecting a language in the select box.

    AI Unraveled: Demystifying Frequently Asked Questions on Artificial Intelligence

    How to search all files containing a specific string on Linux and Windows?

    • On Linux

      grep -rnw ‘directory’ -e “pattern”
      grep –include=\*.{txt,log} -rnw ‘directory’ -e “pattern”
      This will only search for files with .txt or .log extension.
      grep –exclude=*.txt -rnw ‘directory’ -e “pattern”
      This will exclude files with .txt extensions.

    • On Windows

      CD Location
      FINDSTR /L /S /I /N /C:”pattern” *.log

    error: Content is protected !!