Method 1: Use Grep
grep -v ‘^\s*$’ test.txt
Note: -v means to reverse the matching result and the regular expression matches the blank line. (Blank lines can include whitespace characters such as space TAB characters)
Method 2: Use SED
sed ‘/^\s*$/d’ test.txt
Note: D stands for delete the line
Method 3: Use AWK
awk NF test.txt
Note: NF represents the number of fields in the current row. If the line is blank, the number of fields is 0, which is interpreted as false by AWK, so it will not be output.
All three of these methods can handle blank lines containing whitespace characters (Spaces, tabs, etc.).
Method 4: If blank lines are caused by ‘\n’, you can also use the tr command to remove them
tr -s ‘\n’ < test.txt
Note: -s represents the compression of multiple consecutive characters into a character. In this case, multiple ‘\n’ is compressed into a ‘\n’ to remove empty lines.
A weakness of method 4: If the first line is empty, the first line cannot be removed
The level is limited, if has the improper place, also hoped corrects!
grep -v ‘^\s*$’ test.txt
Note: -v means to reverse the matching result and the regular expression matches the blank line. (Blank lines can include whitespace characters such as space TAB characters)
Method 2: Use SED
sed ‘/^\s*$/d’ test.txt
Note: D stands for delete the line
Method 3: Use AWK
awk NF test.txt
Note: NF represents the number of fields in the current row. If the line is blank, the number of fields is 0, which is interpreted as false by AWK, so it will not be output.
All three of these methods can handle blank lines containing whitespace characters (Spaces, tabs, etc.).
Method 4: If blank lines are caused by ‘\n’, you can also use the tr command to remove them
tr -s ‘\n’ < test.txt
Note: -s represents the compression of multiple consecutive characters into a character. In this case, multiple ‘\n’ is compressed into a ‘\n’ to remove empty lines.
A weakness of method 4: If the first line is empty, the first line cannot be removed
The level is limited, if has the improper place, also hoped corrects!
Read More:
- Grep finds all files containing a string in Linux
- [Solved] Es delete all the data in the index without deleting the index structure, including curl deletion
- One of the methods to solve registry error while deleting key
- Removing duplicate lines from Linux shell files
- Several methods of executing multiple commands in Linux shell
- Methods of compiling and installing ffmpeg and libx264 in Linux
- Java – read all the files and folders in a certain directory and three methods to get the file name from the file path
- Latex letters with symbols, wavy lines, horizontal lines, horn, etc
- Extracting the first X lines of a file with Linux
- Solve the problem of “error empty block statement no empty” in the console (Vue project)
- Linux shell RM deletes all. O suffix files in the subdirectory
- Linux use ls to view the file color is all white solution, and Linux file color introduction
- Vs_ Code remote SFTP connects to the server to modify the nginx configuration information and reports all configured authentication methods failed
- How to empty a whole line in latex (simple and effective)
- The built-in filter function in Excel can’t contain all the items
- XML tag has empty body less… (Ctrl+F1) Reports empty tag body. The validation works in XML / JSP
- Linux find file text command (find | grep)
- Translate() and maketrans() methods of string in Python
- Pychar appears“ PEP:8 expected 2 blank lines ,found 1″
- [docker] error in deleting image: image is referenced in multiple repositories