sed 패턴 일치 및 파일 삽입

sed 패턴 일치 및 파일 삽입

sed를 사용하여 파일을 편집하려고 하는데 setup.py일치해야 하는 패턴은 다음과 같습니다.

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import fnmatch
import os
import re
import sys

from setuptools import Command
from setuptools import find_packages
from setuptools import setup
from setuptools.command.install import install as InstallCommandBase
from setuptools.dist import Distribution

DOCLINES = __doc__.split('\n')

_VERSION = '1.0.0'

REQUIRED_PACKAGES = [
    'absl-py >= 0.7.0',
    'astunparse == 1.6.3',
    'backports.weakref >= 1.0rc1;python_version<"3.4"',
    'scipy == 1.2.2;python_version<"3"',
]

일치 후 end 앞에 행을 삽입하고 싶습니다 REQUIRED_PACKAGES = [ .* ].'test == 1.1.0',]

그래서 결국 다음과 같이 보입니다.

REQUIRED_PACKAGES = [
    'absl-py >= 0.7.0',
    'astunparse == 1.6.3',
    'backports.weakref >= 1.0rc1;python_version<"3.4"',
    'scipy == 1.2.2;python_version<"3"',    
    'test == 1.1.0',
]

Python을 사용해 보았지만 더 깨끗하고 간단한 옵션을 찾고 있었습니다.

import re


SEARCH_DICT = {
    'required_packages': re.compile(
        r'REQUIRED_PACKAGES = (?P<required_packages>.*)\n')
}

TEST_LIBRARY = '\t\t\'test==1.0.0\'\n'


def _parse_line(line):
    """
    Do a regex search against all defined regexes and
    return the key and match result of the first matching regex

    """

    for key, rx in SEARCH_DICT.items():
        match = rx.search(line)
        if match:
            return key, match
    # if there are no matches
    return None, None


def parse_file(filepath):
    """
    Parse text at given filepath

    Parameters
    ----------
    filepath : str
        Filepath for file_object to be parsed

    Returns
    -------
    data : file contents
    """

    data = []  # create an empty list to collect the data
    line_index = -1 
    # open the file and read through it line by line

    with open(filepath, 'r+') as file_object:
        line = file_object.readline()
        line_index+=1
        while line:
            # at each line check for a match with a regex
            key, match = _parse_line(line)
            if key == 'required_packages':
                required_packages_start = match.group('required_packages')
                if required_packages_start == '[':                    
                    print('Found REQUIRED_PACKAGES')
                    while line.strip():                        
                        library = line.rstrip()
                        if library == ']': # End of required packages
                            return line_index
                        line = file_object.readline()
                        line_index+=1                     

            line = file_object.readline()  
            line_index+=1          
        file_object.readline()
        line_index+=1

    return line_index


line_index = parse_file('test.test')
lines = None
if line_index:
    with open('test.test', 'r') as file_handler:
        lines = file_handler.readlines()
    lines.insert(line_index, TEST_LIBRARY)
    with open('test.out', 'w') as file_handler:
        file_handler.writelines(lines)

답변1

sed행을 삽입 할 수 있습니다 i.

sed "/REQUIRED_PACKAGES = \[/, /]/ {/]/i\    'test == 1.1.0',
;}" FILE

답변2

sed행 기반 도구입니다. 나는 를 사용하는 것이 더 좋다고 믿습니다 awk. 원하는 블록을 더 쉽게 감지하고 줄을 추가할 것입니다. 더 우아한 방법이 있을 수 있지만 이는 원하는 대로 수행됩니다.

awk '$0=="REQUIRED_PACKAGES = [" {found=1}
    found==1 && $0=="]" {print "    \047test == 1.1.0\047"; found=0}
    {print}' setup.py

이해하기 매우 쉽습니다. 파일의 모든 줄을 인쇄하지만 블록의 닫는 괄호를 인쇄하기 전에 REQUIRED_PACKAGES추가하는 줄을 인쇄합니다.

답변3

$ cat file
_VERSION = '1.0.0'

REQUIRED_PACKAGES = [
    'absl-py >= 0.7.0',
    'astunparse == 1.6.3',
    'backports.weakref >= 1.0rc1;python_version<"3.4"',
    'scipy == 1.2.2;python_version<"3"',
]

.

$ cat tst.awk
/REQUIRED_PACKAGES = \[/ { inBlock=1 }
inBlock {
    if ( /]/ ) {
        sub(/[^[:space:]].*/,"",prev)
        print prev "\047test == 1.1.0\047,"
        inBlock = prev = ""
    }
    prev = $0
}
{ print }

.

$ awk -f tst.awk file
_VERSION = '1.0.0'

REQUIRED_PACKAGES = [
    'absl-py >= 0.7.0',
    'astunparse == 1.6.3',
    'backports.weakref >= 1.0rc1;python_version<"3.4"',
    'scipy == 1.2.2;python_version<"3"',
    'test == 1.1.0',
]

위의 코드는 이전 줄에서 사용된 들여쓰기를 사용하여 새 텍스트를 삽입합니다.

관련 정보