linux file operation two

When processing some data files in the Linux system, it is sometimes necessary to filter out blank lines in them. Various tools provided in the system can complete this function.

The following describes how to delete blank lines in a file using the grep command, sed command, awk command and tr command.

1. grep command

grep. data.txt
grep -v '^ $' data.txt
grep '[^ $]' data.txt

2. sed command

sed '/^$/d' data.txt
sed '/^\s*$/d' data.txt #This command can also delete blank lines composed of complete spaces, tabs, etc.
# The character class \s will match the whitespace characters <tab> and <space>.

3. awk command

awk NF data.txt # This can also delete blank lines composed of spaces, tabs, etc.
awk '!/^$/' data.txt

4. tr command

tr -s '\n' < data.txt

Among the above commands, the efficiency of "grep . data.txt" is relatively high;
you can compare the performance of these commands when dealing with large amounts of data.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326777296&siteId=291194637