
지정된 줄 번호에 있고 패턴과 일치하는 경우에만 줄을 삭제하려면 어떻게 해야 합니까?
예를 들어:
- 나는하고 싶다삭제(
d
); - 그만큼제삼선 (
3
); - 그렇다면공백(
^$
);
다음 구문은 다음과 같습니다.
cat file | sed '3 /^$/d'
다음 오류를 반환합니다.
sed: -e expression #1, char 3: unknown command: `/'
답변1
이렇게 해보세요:
sed '3{/^$/d;}' file
중괄호에 주목하세요.
답변2
user000001이 답변한 것처럼 sed '3{/^$/d;}' file
충분하지만 해당 출력만 표시됩니다. 파일을 수정하려는 경우 sed
GNU를 sed
사용할 수 있습니다 sed -i '3{/^$/d}' file
(GNU의 경우 여기에서 이전 부분 sed
도 생략할 수 있음).;
}
`-i[SUFFIX]' `--in-place[=SUFFIX]' This option specifies that files are to be edited in-place. GNU `sed' does this by creating a temporary file and sending output to this file rather than to the standard output.(1).
FreeBSD/OS/X sed
에서는 sed -i '' '3{/^$/d;}' file
.