Bash: 조건에서 중괄호 삭제

Bash: 조건에서 중괄호 삭제

아래 문자열에서 중괄호를 제거하고 싶지만 * 그 사이에 중괄호가 있는 경우에만 가능합니다.

이 주제에 대해 많은 답변을 찾았지만 제 시나리오에서는 조건이 충족되는 중괄호만 삭제하고 싶습니다 {*} --> *.

name,apple,price,{50 70 80 80},color,{*}
name,orange,price,{*},color,{80 30 40}

예상 출력:

name,apple,price,{50 70 80 80},color,*
name,orange,price,*,color,{80 30 40}

도와주세요. 미리 감사드립니다.

답변1

sed의 명령 사용 s(대체로) – 에는 중괄호를 포함 regexp하지만 :에는 포함하지 않습니다 replacement.

sed 's/{\*}/*/g'

답변2

bash만으로

line='name,orange,price,{*},color,{80 30 40}'
s='{*}'
echo "${line//"$s"/*}"
name,orange,price,*,color,{80 30 40}

변수가 필요하지 않도록 이스케이프를 수행하는 방법을 알 수 없었습니다 s.

큰따옴표가 필요합니다. 그렇지 않으면 다음과 같은 결과가 나타납니다.

$ echo "${line//$s/*}"
name,orange,price,*

답변3

명령

sed "/{\*}/s/{\*}/*/g" filename

산출

name,apple,price,{50 70 80 80},color,*
name,orange,price,*,color,{80 30 40}

방법2

awk '$0 ~ "{*}" {gsub (/{\*}/,"*",$0);print }'  filename

산출

name,apple,price,{50 70 80 80},color,*
name,orange,price,*,color,{80 30 40}

관련 정보