Unix / Linux操作系统,删除线注释 [英] Unix/Linux, Delete comments from lines

查看:399
本文介绍了Unix / Linux操作系统,删除线注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要删除/删除用户输入行注释而不删除任何codeS。因此,例如:

I need to delete/remove comments from a user-input line without deleting any codes. So for example:

mail -s 'text' brown < text #comments

我如何删除评论并留下code完好?
我可以删除以#开头的行,但如果它在线路中间的某个位置开始没有。

How do I remove the comments and leave the code intact? I can delete lines that begin with #, but not if it begins somewhere in the middle of the lines.

我试过:

echo $line | sed -e 's/\

,但它不工作。任何想法,我做错了什么?

but it does not work. Any idea what I'm doing wrong?

另外,如何检测其中#不用于开始评论例?
对于因为它们不是注释以#结束例如报价#和code系列。

Also, how to detect cases in which # is not used to begin a comment? For example quoted # and line of code that ends with # since they are not comments.

echo $line | sed -e  '/^#/d'

在此行中,#,不使用作为注释,但作为code的一部分。我弄清楚,我需要检测,如果是#引号内或不具备前的#空白字符。我怎么离开的输出,因为它是?

In this line, the # is not used as a comment, but as part of code. I figure out that I need to detect that if # is within quotes or does not have a whitespace character before the #. How do I leave the output as it is?

推荐答案

您可以从使用该 awk的<删除所有到行尾/ code>

You can remove all from # to end of line using this awk

awk '{sub(/#.*$/,"")}1' file

但是,如果你的文件是这样的:

But if you have file like this:

#!/bin/bash
pidof tail #See if tail is running
if [ $? -ne 0 ] ; then  #start loop
   awk '{print " # "$8}' file >tmp # this is my code
fi # end of loop
awk -F# '{for (i=1;i<=NF;i++) print $i}' file > tmp2
a=a+1 # increment a

有没有方法可以自动删除评论不破坏一些。

There are no way you can remove the comment automatically without destroying some.

这篇关于Unix / Linux操作系统,删除线注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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