SEDおよび/またはAWKを使用してラインをパターンに置き換える[閉じる]

SEDおよび/またはAWKを使用してラインをパターンに置き換える[閉じる]

入力ファイルです

def test():
    print('do something')
    #>#> this does something
    return 0

def new_test(arg):
    # >#>this too does something
    x = 0
    y = 1

    return x+y

def main():
    test()
    z = 'hello'
    # ># this should not work
    return null

if __name__ == '__main__':
    # >#> main call
    main()

出力は次のようになります。

def test():
    print('do something')
    some_fn("this does something")
    return 0

def new_test(arg):
    some_fn("this too does something")
    x = 0
    y = 1

    return x+y

def main():
    test()
    z = 'hello'
    # ># this should not work
    return null

if __name__ == '__main__':
    some_fn("main call")
    main()

コメント文字列を抽出する方法が見つかりません。

ベストアンサー1

標準を使用すると、sed次のようになります。

sed  's/# *>#> *\(.*\)/some_fn("\1")/' file
  • あなたの#>#>パターンが余分なスペースを許可しているようで、これを# *>#> *パターンとして使用しました。他のスペースが許可されている場合は調整してください。
  • .*残りの行と一致します。\(\)交換時に参照できるように内部に入れました。\1

おすすめ記事