一致するパターンからコメント文字をキャプチャ

一致するパターンからコメント文字をキャプチャ

ファイルから一致する行を選択したいと思います。

# Mode: org

#ファイルプログラミング言語のコメント文字にすることができます。

また、inはbashスクリプトを使用して前にスペースを追加することでorの1つに#することもできます。;!

はい

# Mode: org

   ;; Mode: org

!! Mode: org

ファイル拡張子で言語を見つけることができます

.el    emacs
.c     c
.cc    c++
.sh    bash
.rc    bash
.f     fortran
.F     fortran
.f90   fortran
.texi  texinfo
.tex   tex

これは私が現在使用しているbash機能です。

capture ()
{
 local efile="$1"

 local begorg endorg charcl

 impl="1"
 if [ "$impl" = "1" ]; then 
   charcl='^[[:space:]]*(#|;|!)+[[:space:]]*' 
 elif [ "$impl" = "2" ]; then
   charcl='^[[:space:]]*(//|@c)[[:space:]]*' 
 fi 
 
 begorg="${charcl}"'Mode: org$'
 endorg="${charcl}"'# End of org$'
 
 awk -v ccls="$charcl" -v bego="$begorg" -v endo="$endorg" \
   '$0 ~ bego { found=1; next } 
    $0 ~ endo { found=0; } 
    found { sub(/ccls/,""); print }' "$efile"
}

これは良いことですが、awkパターンを含める方法に問題があります。

/<pattern>/ { set variable to A }
/other pattern/ { set variable to B }
... use variable in sub

ベストアンサー1

おすすめ記事