How shell removes empty lines of files (blank lines)

Source: Internet
Author: User
There are many blank lines after extracting the log files to be viewed recently, which is not conducive to the comparison of previous files. for backward compatibility, this article describes how to use shell to remove empty rows in a file. learn how to detect sed, awk, and shell files. if you need it, you can make a reference.

Content of this section:
Shell removes empty lines from the file

1. shell removes empty lines from the file.

cat filename | sed -e '/^$/d' > filename

2. retain the latest 9 files and delete command statements from other files.

ls -t | awk '{if(NR>9){print $0}}' |xargs rm -f

Appendix: special variables in shell

$
Shell PID (ProcessID)
$!
PID of the background Process last run by Shell
$?
End code of The Last Command (return value)
$-
Flag overview Set by using the Set command
$ *
List of all parameters. For example, when "$ *" is included in... $ N "to output all parameters.
$ @
List of all parameters. For example, when "$ @" is included in "... All parameters are output in the form of "$ n.
$ #
Number of parameters added to Shell
$0
Shell file name
$1 ~ $ N
Parameters added to Shell. $1 is the 1st parameter, $2 is the 2nd parameter...
Test
Whether the test-f parameter is in a file
-D whether the parameter is in the directory
-E: whether the file exists

Man test

Determine 7 file types

Test-f $1 & cat $1 Common File
Test-d $1 & ls-ld $1 Directory
Test-L $1 & ls-ld $1 Connection file
Test-p $1 & ls-ld $1 MPs queue file
Test-S $1 & ls-ld $1 socket
Test-B $1 & ls-ld $1 device
Test-c $1 & ls-ld $1 character device

Here are a few examples for your reference.

1. compare the numeric size

#!/bin/bash  test $1 -gt  $2 && echo $1  test $1 -lt  $2 && echo $2  test $1 -eq  $2  && echo $1=$2

2. judge the string

#! /Bin/bash test $1! = $2 & echo not equal test $1 = $2 & echo $1 = $2

3. combined judgment
1 ),

#! /Bin/bash test $1-gt 5-a $1-lt 10 & echo $1 if #! /Bin/bash if test $1-gt 5 (or if [$1-gt 5] Note space) then echo $1 fi

2 ),

#! /Bin/bash ping-c 1-W 1 192.168.0. $1 &>/dev/null (-c 1 indicates a ping, and The-W target host cannot reach a timeout value of 1 second) if [$? -Eq 0] then echo link OK esleecho link no fi

SHELL deletes empty lines or blank lines in the file:

Today, we found that there are too many empty lines in a file and we intend to delete them.

So I used sed and checked the command to write it like this:

Sed '/^ $/d' file1

I found that there are still a lot of blank lines in the output content, which is inexplicable.

So: sed '/^ $/p' file1 found that there are few empty rows output, but actually there are many empty rows.

So cat-A file1 is used.

It was found that many rows contain space characters, which causes regular expressions to not recognize empty rows.

So modify it to: sed '/^ * $/d' file1. then OK.

In addition, note that the line break in windows is ^ M $

In LINUX, $ is used. if it is a file in WINDOWS, it needs to be converted. it seems like this tool is used to use doc2unix.

Linux shell deletes empty row 3:

There are many blank lines after extracting the log files to be viewed recently, which is not conducive to the comparison of previous files. in order to be backward compatible, only empty lines can be deleted when obtained. I Googled myself and used the grep method. the efficiency was quite fast. there were 73 blank lines in more than 25000 rows. I should be able to take over the lines in an instant.
Method 1: (I used this)
Grep-v "^ $" file removes matching empty rows
In addition, you can also use grep to view the rows that are empty during troubleshooting, so that you can view the behavior information from the original log file,
Add the parameter-n grep-n "^ $" file to find the empty line and view the information in the original log file.


Method 2: Use the tr command
Cat file name | tr-s '\ n'

Method 3: Use the sed command
Cat file name | sed '/^ $/D'

Method 4: Use the awk command
Cat file name | awk '{if ($0! = "") Print }'
Cat file name | awk '{if (length! = 0) print $0 }'


For more details about how to remove the empty lines (blank lines) in shell files, please follow the PHP Chinese network!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.