Linux Shell脚本去掉几类常见文件中的注释
? ? Linux操作系統(tǒng)中去掉各類文件中的注釋這個(gè)功能比較常用,通常用在查看一個(gè)較長(zhǎng)的文件,又不想看注釋的情況。通常這些文件包括C語(yǔ)言編寫的*.c、*.h文件、cpp文件、*.xml文件、*.sh shell腳本文件、*.ini *.conf配置文件、*.php *.py *.pl等編程語(yǔ)言編寫的文件以及無(wú)擴(kuò)展名的一些可執(zhí)行文件等。
????實(shí)現(xiàn)這個(gè)功能并不復(fù)雜,通常注釋風(fēng)格就那么幾種,在編寫腳本過(guò)程中只需要編寫出合適的正則表達(dá)式以及運(yùn)用適當(dāng)?shù)奈谋咎幚砉ぞ?#xff08;grep、sed等)即可。
????針對(duì)幾種常見(jiàn)的注釋風(fēng)格編寫一個(gè)腳本文件代替cat更會(huì)省力一些。
腳本如下:
此腳本可以從GitHub上獲取,歡迎issue、fork、star:https://github.com/DingGuodong/LinuxBashShellScriptForOps/blob/master/functions/string/noComment2.sh
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | #!/bin/bash #?delete?all?spaces?and?comments?of?specialized?file,?using?with?$@?filename DEBUG=false if?${DEBUG}?;?then ????old_PS4=$PS4??#?system?builtin?variable?does?not?need?'${var}'?expression #????export?PS4='+${BASH_SOURCE}:${LINENO}:${FUNCNAME[0]}:?' ????export?PS4='+${LINENO}:?${FUNCNAME[0]}:?'?#?if?there?is?only?one?bash?script,?do?not?display?${BASH_SOURCE} ????_XTRACE_FUNCTIONS=$(set?+o?|?grep?xtrace) ????set?-o?xtrace fi function?is_file_exist(){ ????test?-f?$1?||?echo?"ls:?cannot?access?$file:?No?such?file?or?directory"?&&?exit?1 } function?dos2unix_text_file_format_converter(){ ????if?cat?-A?${file}?|?grep?'\^M\\$'?>/dev/null?||?file?${file}?|?grep?"with?CRLF?line?terminators"?>/dev/null?;?then ????????which?dos2unix?>/dev/null?2>&1?||?yum?-q?-y?install?dos2unix?||?apt-get?-qq?-y?install?dos2unix ????????dos2unix?${file}?>/dev/null ????fi } function?del_comment_in_c_cpp_file(){ ????tmp_file=/tmp/.noComment_$(date?+%Y%m%d%H%M%S%N$RANDOM) ????cp?${file}?${tmp_file} ????#delete?the?comment?line?begin?with?'//comment' ????sed?-i?"/^[?\t]*\/\//d"?${tmp_file} ????#delete?the?comment?line?end?with?'//comment' ????sed?-i?"s/\/\/[^\"]*//"?${tmp_file} ????#delete?the?comment?only?occupied?one?line?'/*?comment?*/' ????sed?-i?"s/\/\*.*\*\///"?${tmp_file} ????#delete?the?comment?that?occupied?many?lines?'/*comment ????#??????????????????????????????????????????????*comment ????#??????????????????????????????????????????????*/ ????sed?-i?"/^[?\t]*\/\*/,/.*\*\//d"?${tmp_file} ????grep?-v?^$?${tmp_file} ????\rm?-f?${tmp_file} } function?del_comment_in_sh_conf_file(){ ????#ignore?the?comment?line?end?with?'#?comment' ????grep?-v?"^[?\t]*\#"?${file}?|?grep?-v?"^$" } function?del_comment_in_xml_file(){ ????if?test?-f?${file}?&&?file?${file}?|?grep?"XML"?>/dev/null;?then ????????which?tidy?>/dev/null?2>&1?||?yum?-q?-y?install?tidy?>/dev/null?2>&1?||?apt-get?-qq?-y?install?tidy?>/dev/null?2>&1 ????????tidy?-quiet?-asxml?-xml?-indent?-wrap?1024?--hide-comments?1?${file} ????else ????????which?tidy?>/dev/null?2>&1?||?yum?-q?-y?install?tidy?>/dev/null?2>&1?||?apt-get?-qq?-y?install?tidy?>/dev/null?2>&1 ????????tidy?-quiet?-asxml?-xml?-indent?-wrap?1024?--hide-comments?1?${file} ????fi } function?del_comment_in_general_file(){ ????#ignore?the?comment?line?end?with?'#?comment' ????grep?-v?"^[?\t]*\#"?${file}?|?grep?-v?"^[?\t]*\;"?|grep?-v?"^$" } function?del_comment(){ ????case?${file}?in ????????*.c|*.cpp|*.h) ????????????del_comment_in_c_cpp_file ????????????;; ????????*.sh|*.conf) ????????????del_comment_in_sh_conf_file ????????????;; ????????*.xml) ????????????del_comment_in_xml_file ????????????;; ????????*) ????????????del_comment_in_general_file ????????????;; ????esac } file=$1 if?[[?-f?${file}?]];?then ????del_comment else ????echo?"ls:?cannot?access?$file:?No?such?file?or?directory"?&&?exit?1 fi if?${DEBUG}?;?then ????export?PS4=${old_PS4} ????${_XTRACE_FUNCTIONS} fi |
tag:刪除注釋,不查看注釋,去掉注釋
--end--
本文轉(zhuǎn)自 urey_pp 51CTO博客,原文鏈接:http://blog.51cto.com/dgd2010/1886595,如需轉(zhuǎn)載請(qǐng)自行聯(lián)系原作者
總結(jié)
以上是生活随笔為你收集整理的Linux Shell脚本去掉几类常见文件中的注释的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 《Linux From Scratch》
- 下一篇: 用 Win7,硬件也有新天地