Shell remove file hollow line (blank line) method _linux Shell

Source: Internet
Author: User


This article details the use of shell to remove empty lines in the file method, learning the SED, awk, Shell file detection methods, the need for friends can make a reference.

The content of this section:
Shell remove file Hollow line



1,shell remove blank lines from files


code as follows:
Cat FileName | Sed-e '/^$/d ' > filename





2, keep the latest 9 files, other files delete the command statement





code as follows:
ls-t | awk ' {if (nr>9) {print $}} ' |xargs rm-f





Attached, special variables in the shell

$$
The shell itself PID (ProcessID)
$!
The PID of the Shell's last running background process
$?
End code of the last command to run (return value)
$-
Flag list using the SET command
$*
A list of all parameters. Output all parameters in the form of "$ $n", as in the case of "$*" with "".
$@
A list of all parameters. such as "$@" with "" in the case, to "$ $" ... Output all parameters in the form of "$n".
$#
Number of arguments added to the shell
$
File name of the shell itself
$1~ $n
The value of each parameter added to the shell. $ is the 1th parameter, $ is the 2nd parameter ...
Judge Test
Test-f whether to participate in and is a file
-D whether the parameter is in and is a directory
-E File exists



Mans test



7 Types of files are judged

Test-f $ && Cat normal files
test-d $ && LS-LD Catalog
Test-l $ && ls-ld Connection files
Test-p $ && ls-ld pipeline Files
Test-s $ && ls-ld sockets
Test-b $ && Ls-ld block devices
Test-c $ && ls-ld character devices



Here are a few examples, for you to learn the reference.



1, compare number size


Copy Code code as follows:

#!/bin/bash
Test $1-GT $ && echo $
Test $1-LT $ && echo $
Test $1-eq $ && echo $1=$2





2, Judge the string





 code as follows:

#!/bin/bash
Test $ && echo is not equal!=
Test $ = $ && echo $1=$2





3, combination judgment
1),





code as follows:

#!/bin/bash
Test $1-gt 5-a $1-lt && echo $
If
#!/bin/bash
if test $1-GT 5 (or if [$1-GT 5] pay attention to spaces)
Then
echo $
Fi





2),


 code as follows:

#!/bin/bash
Ping-c 1-w 1 192.168.0.$1 &>/dev/null (-C 1 Ping once, the-W target host can not reach timeout of 1 seconds)
If [$?-eq 0]
Then
echo Link OK
Esle
echo Link No
Fi





Shell deletes a blank line in a file or a blank line two:



Today I found a file with a hollow line too many, and intend to delete the blank line.



So using sed, check the command, and write:



Sed '/^$/d ' file1



It's baffling to find that there is still a lot of empty lines in the output.



So: sed '/^$/p ' file1 found that the output of the empty line is very small, but actually a lot of empty lines.



So use cat-a file1



found that the original many lines in the space character, resulting in a positive does not recognize it is a blank line



So the change is: sed '/^ *$/d ' file1 this is OK.



Also note that in Windows line breaks are ^m$



And in Linux is $, if the file under Windows need to be converted, use Doc2unix seems to be the tool bar.



Linux Shell Delete file empty row three:



Recently to see the log file extraction has a lot of empty lines, not conducive to the previous file can be compared, in order to backward-compatible, can only get the time to delete the blank line. Google himself a bit, using the grep method, efficiency is quite fast, 25000+ line 73 lines, an instant fix, should be able to take over.
Method One: (This is what I use)
Grep-v "^$" file to remove matching blank lines
Also use grep to see if the rows are blank when the row lookup is wrong, and then view the rows from the original log file to see why they don't have that information.
Add parameter-n grep-n "^$" file to find a blank line and then go to the original log file to view the information.




Method Two: Use the TR command
Cat filename |tr-s ' \ n '

Method three: Using sed command
Cat filename |sed '/^$/d '

Method Four: Use awk command
Cat filename |awk ' {if ($0!= "") Print} '
Cat filename |awk ' {if (length!=0) print $} '


Related Article

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.