如果行不以 # 开头,则重复删除标记 [英] remove token repeatedly if line does not start with #

查看:45
本文介绍了如果行不以 # 开头,则重复删除标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的文本文件中删除所有逗号,除非一行以 # 开头

I want to remove all commas from my text file unless a line starts with #

例如:

a, b, c
#a, b, c

应该转向:

a b c
#a, b, c

我不介意双重扫描文件,但我想用 sed 来做到这一点

I don't mind double scan the file but I want to do that with sed

推荐答案

你可以试试下面的 sed 命令,

You could try the below sed command,

$ sed '/^ *#/!s/,//g' file
a b c
#a, b, c

^ 断言我们在开始.所以上面的命令将匹配以零个或多个空格和 # 符号开头的行.然后下面的 ! 使 sed 反转选择,即它强制 sed 在不匹配的行上进行替换.s/,//g 用空字符串替换所有逗号.

^ asserts that we are at the start. So the above command will match the lines which starts with zero or more spaces and a # symbol. Then the following ! makes the sed to inverse the selections ie, it forces the sed to do the replacement on the lines which are not matched. s/,//g replaces all the commas with an empty string .

通过awk,

$ awk '!/^ *#/{gsub(/,/,"")}1' file
a b c
#a, b, c

! 在开头否定模式.同样,它只会在开头没有 # 的行上进行替换.

! at the start negates the patten. Likewise , it will do the replacement only on the lines which don't have # at the start.

这篇关于如果行不以 # 开头,则重复删除标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆