SED를 사용하여 "{"to "}에서"{ "에서"}에서 시작하는 파일에서 모든 줄을 제거하십시오.

StackOverflow https://stackoverflow.com//questions/10649516

  •  11-12-2019
  •  | 
  •  

문제

콘텐츠가 다음과 같습니다.

control_data {
some data dadadadf 
some data fsfsfs
some more data
.
.
.
}
more data below 
{
.
.
}
.

"control_data"에서 첫 번째 "}"로 데이터를 삭제하고 싶습니다

명령을 시도했습니다

sed "s/control_data\([^}]*\)}//g"
.

그러나 이것은 예를 들어 여러 줄이 없으면 작동합니다.이 명령은 데이터가있을 때 다음과 같이 작동합니다.

control_data {some data dadadadf some data fsfsfs some more data...}more data
.

다음을 제공합니다 :

more data {....} 
.

이 명령을 여러 줄에 데이터가있을 때이 명령을 작동시키는 방법은 무엇입니까? 나는 쉘 스크립팅에 대한 새로운 설명을 새로운 설명으로 돕는 데 도움이 될 것입니다.

미리 감사드립니다.

도움이 되었습니까?

해결책

이 하나는 이상적인 것일 수는 없지만이 사용 된 SED (SED 코드)

cat ip_file.txt | tr '\n' ':' | sed "s/control_data\([^}]*\)}//g" | tr ':' '\n'
.

logic : 모든 새로운 줄을 다음과 같이 변환하고 SED 코드를 사용하여 모두 변환 한 다음 뉴라인으로 변환합니다. 참고 : 내 가정은 다음과 같습니다. 파일에 파일에 존재할 수 없습니다.

다른 팁

My Sed-Fu는 약합니다. 그러나 여기서 내가 일하는 일이 있습니다 (이 답변 가이드로) :

[me@home]$ sed -n '/^control_data {/{:a;n;/^}/!ba;n};p' input_file.txt
more data below 
{
.
.
}
.

여기에 명령을 분석 한

sed -n "             # "-n" = suppress automatic printing of pattern space
/^control_data {/ {  # if (line matches control data)
   :a;                 #   mark this spot with label a
   n;                  #   get next line
   /^}/!ba             #   if (doesn't start with "}") go back to label a
   n;                  #   get next line before leaving this control block
};                   # end if
p;                   # print everything else not affected by previous block
" input_file.txt
.

이렇게 해제 할 수 있습니다 :

sed '/^control_data {.*}$/d;/^control_data {/,/^}$/d' file
.

  • 첫 번째 명령은 모든 단일 줄을 제거합니다. /^control_data {.*}$/d
  • 두 번째 명령은 모든 블록을 제거합니다.
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top