.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
플래그마다 하나씩)가 있습니다.