正则表达式之去除空行

在Linux中搭建服务的时候难免要查看配置文件,一般包含#号的行都是不会被执行的

故我们在查看配置文件的时候,特别的配置文件较长的时候,要用到正则表达式

[[email protected] code]# more my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html

[mysqld]

 Remove leading # and set to the amount of RAM for the most important data
 cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
 innodb_buffer_pool_size = 128M

 Remove leading # to turn on a very important data integrity option: logging
 changes to the binary log between backups.
 log_bin

# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
 port = .....
 server_id = .....
 socket = .....

# Remove leading # to set options mainly useful for reporting servers.
 The server defaults are faster for transactions and fast SELECTs.
 Adjust sizes as needed, experiment to find the optimal values.
 join_buffer_size = 128M
 sort_buffer_size = 2M
# read_rnd_buffer_size = 2M 

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 

以此代码为例,用正则挑选出对我们有用的行,并且去除空行

[[email protected] code]# grep -v ^‘#‘ my.cnf |grep -v ^$
[mysqld]
 Remove leading # and set to the amount of RAM for the most important data
 cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
 innodb_buffer_pool_size = 128M
 Remove leading # to turn on a very important data integrity option: logging
 changes to the binary log between backups.
 log_bin
 port = .....
 server_id = .....
 socket = .....
 The server defaults are faster for transactions and fast SELECTs.
 Adjust sizes as needed, experiment to find the optimal values.
 join_buffer_size = 128M
 sort_buffer_size = 2M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 
时间: 2024-08-26 06:17:02

正则表达式之去除空行的相关文章

jsp去除空行的web.xml配置

在jsp中我们引入的标签,例如jstl的标签,循环遍历等等,可能会产生很多空行,其实也没什么,不会影响展示,但是空行多多少少会影响性能,这是我们只需要在web.xml中配置一下我们就可以很简单的去掉,配置如下: <jsp-config> <jsp-property-group> <url-pattern>*.jsp</url-pattern> <trim-directive-whitespaces>true</trim-directive-

shell 去除空行

最近要查看的日志文件提取后有很多空行,不利于以前的文件可以进行比较了,为了向下兼容,只能取得时候把空行删除掉.自己google了一下,用了grep方法,效率还是挺快的,25000+行中73行空行,瞬间搞定,应该可以接手. 方法一:(我就是用的这个) grep -v "^$" file 去除匹配的空行另外在排查找错的时候也利用grep 查看那几行是空行,从而从原来的日志文件查看那几行为什么没有该信息,加参数 -n grep -n "^$" file 即可找到空行再去原

[转]正则表达式,去除所有HTML标签

protected string str = "<table><tr><td>sdasasdsdd</td></tr></table><br><p>sds</p><img id='img1' src='http://www.baidu.com/img/baidu_logo.gif' width='100' height='50' alt=''>aaassss<br>

DataTable 整行为空时,去除空行,常用于Excel导入,转换为DataTable时出现

protected void RemoveEmpty(DataTable dt) { List<DataRow> removelist = new List<DataRow>(); for (int i = 0; i < dt.Rows.Count; i++) { bool IsNull = true; for (int j = 0; j < dt.Columns.Count; j++) { if (!string.IsNullOrEmpty(dt.Rows[i][j]

Shell去除空格和空行的方法

一.去除空行的方法 有时我们在处理和查看文件时,经常会有很多空行,为了美观或是有需要时,就有必要把这些除行去掉了,方法如下: 1)用tr命令 复制代码 代码如下: cat 文件名 |tr -s '\n' 2)用sed命令 复制代码 代码如下: cat 文件名 |sed '/^$/d' 3)用awk命令 复制代码 代码如下: cat 文件名 |awk '{if($0!="")print}'cat 文件名 |awk '{if(length !=0) print $0}' 4)用grep命令

去除文本多余空行

1.读取文件: OpenFileDialog dialog=new OpenFileDialog(); dialog.InitialDirectory = Application.StartupPath; dialog.Filter = "All Files|*.*|text file(*.txt)|*.txt"; dialog.RestoreDirectory = true; if (dialog.ShowDialog() == DialogResult.OK) { string f

linux命令(44):去掉 所有文件中的空行

方法一:利用grep grep -v '^\s*$' test.txt 注:-v表示将匹配的结果进行反转,正则表达式匹配空行.(空行可包括空格符制表符等空白字符) 方法二:利用sed sed '/^\s*$/d' test.txt 注:d代表删除该行 方法三:利用awk awk NF test.txt 注:NF代表当前行的字段数,空行的话字段数为0,被awk解释为假,因此不进行输出. 以上三种方式均可处理包含空白字符(空格符,制表符等)的空行. 方法四:若空行均由'\n'造成,则还可以利用tr命

linux正则表达式

简单理解就是: 正则表达式就是一套处理字符串的规则和方法,以行为单位对字符串进行处理,通过特殊的符号的辅助,我们可以快速的过滤,替换某些特定字符. 例如:grep(egrep),sed,awk命令都需要正则表达式的配合.提高效率. 运维工作中,会有大量访问日志,错误日志,大数据.都需要正则表达式.(快速过滤需要的内容) 正则表达式一些字符,赋予了它特定的含义: 基础正则表达式:BRE 1)^can 表示该行以can开头. 2)can$ 表示该行以can结尾.             “^$”表示

JavaScript使用正则表达式

2.0 简介 正则表达式是可以用来查找与给定模式匹配的文本的搜索模式.例如,在上一章中,我们在一个较长的字符串中查找子字符串Cookbook: var testValue = "This is the Cookbook's test string"; var subsValue = "Cookbook"; var iValue = testValue.indexOf(subsValue); //返回值12,即子字符串的索引 这段代码有效,因为我们要查找一个严格的匹配