bash 如何替换文件中的哈希字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18649938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 06:29:08  来源:igfitidea点击:

How to Replace a hash character in file?

regexstringbashgrep

提问by run_time_rookie

How can I remove '#' character from file

如何从文件中删除“#”字符

 File Name: /etc/ironman.d

 #dir /usr/comicon
 #dir2 /usr/individuality
 #dir3 /usr/land

How can I remove '#' character before dir2. There is no way to maintain line number. I tried

如何删除 dir2 之前的“#”字符。没有办法维护行号。我试过

 dirpath='#dir2 /usr/individuality'
 ${dirpath#/#}

But it only removes in $dirpath but not in actual file, if I use above method to remove '#' and add then, I get a duplicate of $dirpath without '#', but I just want single entry without '#' in the file. How do I remove # in place (before dir2)

但它只在 $dirpath 中删除而不在实际文件中删除,如果我使用上述方法删除 '#' 然后添加,我会得到一个没有 '#' 的 $dirpath 副本,但我只想要一个没有 '#' 的条目文件。我如何删除 # 到位(在 dir2 之前)

回答by Carl Norum

You can use sed(1)to edit files:

您可以使用sed(1)来编辑文件:

sed -e 's/#dir2/dir2/' /etc/ironman.d

回答by svante

An awk solution :

一个 awk 解决方案:

awk -F'#' '{print }' /etc/ironman.d

回答by svante

Replace all leading #'s :

替换所有前导 # :

sed -e 's/^#*//' /etc/ironman.d