sed를 이용한 공백지우기

다음의 sed 명령어를 사용하여 파일에서 빈라인과 공백만을 포함한 라인을 제거할 수 있다.
 
sed -e '/^[     ]*$/d' InputFile >OutputFile

단일인용부호(') 사이에 있는 앞의 슬래쉬(/)는 sed에 의해 해석될 정규표현식의 범위를
지정한다.
닫는 단일인용부호 앞의 "d"는 sed에게 정규표현식에 해당하는 라인을 지우도록 한다.
 
정규표현식사이에서 캐럿(^)은 라인의 시작에 해당한다.
[]*는 여는 대괄호([)와 닫는 대괄호(])사이의 문자(또는 숫자)가 0번이상
나타나는 것을 나타낸다.
(위의 정규표현식에서는 대괄호 사이에 공백문자(스페이스)와 탭을 넣어야 한다)
달러기호($)는 라인의 끝에 해당된다.

세가지 정규표현식은 함께 어우러져 빈라인이나 공백이나
탭문자(어떠한 조합이든지)로만 이루어진 라인을 찾아낸다.
 
sed의 표준 작동은 표준출력으로 나타나므로 빈라인(또는 공백(스페이스, 탭)으로만
이루어진)을 제외한 모든 라인을 OutputFile로 보낼 것이다.

-----<원문입니다. 오역이 있을지도 모르니..>-----
I SED BLANK

Using sed, you can remove blank lines, and
lines that contain only whitespace, from a
file using the following:

sed -e '/^[     ]*$/d' InputFile >OutputFile

Within the single quotes ('), the forward
slashes (/) delimit the regular expression
that will be interpreted by sed.  The "d"
before the closing single quote, tells sed
to delete any lines that match the regular
expression.

Within the regular expression, the caret
(^) matches the beginning of a line.
The []* matches zero to many occurrences of
the character list between the open bracket
([) and the close bracket (]) (in the above
regular expression, you must insert a space
and a tab between the brackets).  The dollar
sign ($) matches the end of a line.

These three constructs together match any
blank line or any line that contains only
spaces and tabs (in any combination).

Since the standard operation of sed is to
echo lines to stdout, all lines except blank
lines (or lines that only contain whitespace)
will be sent to OutputFile.
반응형
Posted by She쥐포s

블로그 이미지
She쥐포s
Yesterday
Today
Total

달력

 « |  » 2024.4
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30

최근에 올라온 글

최근에 달린 댓글

글 보관함