![sed 명령 결합 도움말(구문)](https://linux55.com/image/178544/sed%20%EB%AA%85%EB%A0%B9%20%EA%B2%B0%ED%95%A9%20%EB%8F%84%EC%9B%80%EB%A7%90(%EA%B5%AC%EB%AC%B8).png)
이렇게 많은 줄이 포함된 파일이 있습니다.
, foo = $true
, foo = $false
foo = $true, <--- single space after comma
foo = $false, <-- single space after comma
이 문자열을 교체하려면 할 수 있습니다
#!/bin/bash
sed -i 's/, foo = $true/bar/g' file
sed -i 's/, foo = $false/bar/g' file
sed -i 's/foo = $true, /bar/g' file
sed -i 's/foo = $false, /bar/g' file
아니면 묶는 것도 마찬가지로 나쁘다
sed -i -e 's/, foo = $true/bar/g' -e 's/, foo = $false/bar/g' -e 's/foo = $true, /bar/g' ... file
하지만 단일 sed 명령을 사용하여 모든 반복을 수행하는 방법이 있습니까?
sed -i 's/[,\ ].*foo = [$false\|$true][,\ ]/bar/g' file
답변1
sed -Ei 's/,{,1} {,1}foo = \$(true|false),{,1} {,1}/bar/g' file
sed -Ei 's/(, ){,1}foo = \$(true|false)(, ){,1}/bar/g' file
-E
플래그는 확장 정규 표현식을 활성화하여 (){}|
이스케이프 없이 메타 문자를 사용할 수 있도록 합니다.
(, ){,1}
0(묵시적)을 1회 발생으로 일치시킵니다,
.\$(true|false)
일치$true
또는$false
.
기억해주세요
sed -i -e 's/, foo = $true/bar/g' -e 's/, foo = $false/bar/g' -e 's/foo = $true, /bar/g' ... file
sed 명령이지만 다양한 스크립트( -e
각 플래그마다 하나씩)가 있습니다.