2007. 11. 3. 20:47 Unix

여러파일 조작하기

여러 파일 조작하기
 
동시에 여러 파일에 대해 조작을 할 필요성을 느낀적이 있는가???
 
여기 그에 대한 해답(솔루션)이 있다.
 
예를 들어, 하나의 파일에 대해서 뿐만 아니라 일련의 파일에 대해서도
(grep을 이용하여) 문자를 찾고, awk 또는 펄스크립트등을 실행하는 등의
여러가지 작업을 수행할 필요가 있다면 unix 프롬프트상에서 다음의
명령어를 사용하라.
 
 $<: foreach i (<file_list>)
 ? echo $i
 ? grep <search_pattern> $i > tmp
 ? awk -f awk_script tmp >> report
 ? ....
 ? ....
 ? end
$<:
 
꺽쇄괄호안의 파일리스트는 다음의 두 종류가 올 수 있다.
 
* 특정한 이름을 지정
 
* 파일 이름의 목록을 포함하는 유닉스 변수. 예를 들면 "p"라는 변수는
다음에 보는 것처럼 "data"라는 문자로 시작하는 모든 파일을 지정할 수
있다.
 
   set p = (data*)
 
다른 예제를 들자면
 
   set g = `grep -l "Startpoint" * `
 
또는
 
   set all = *
(위의 내용은 현재 디렉토리의 모든 파일명을 "all"이라는 변수에 할당한다.
 
그리고 변수명을 "foreach"라는 명령어와 함께 사용한다면 다음과 같이
될 것이다.
 
$<:  foreach i ($all)
  .....
  .....
end
 
-----------------<원문>-------------------
OPERATING ON MULTIPLE FILES
 
Have you ever felt the need to perform a
set of operations on multiple files
simultaneously???
 
Here is a solution for that.
 
For instance, if it is required to perform
multiple operations like searching a string
(using grep), and executing an awk or perl
script etc, etc. on not just one file but a
set of files, use the following commands at
the unix prompt:
 
 $<: foreach i (<file_list>)
 ? echo $i
 ? grep <search_pattern> $i > tmp
 ? awk -f awk_script tmp >> report
 ? ....
 ? ....
 ? end
$<:
 
The files list in the brackets can be either
 
* Specifically mentioned
 
* A unix variable which contains a list of
file names.  For instance, the variable
"p" can be assigned all the files starting
with string "data" as follows:
   set p = (data*)
Other examples are:
   set g = `grep -l "Startpoint" * `
or
   set all = *
(This assigns all file names in the current
directory to the variable "all" )
 
And its usage with the "foreach" command will
be as follows:
 
$<:  foreach i ($all)
  .....
  .....
end
반응형
Posted by She쥐포s
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

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
                             UNIX GURU UNIVERSE
                                UNIX HOT TIP

                       Unix Tip 2744 - July  8, 2007

                   
http://www.ugu.com/sui/ugu/show?tip.today
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

출력결과 읽기

vi에서 다음을 입력하라:

:r !my_command

위 명령은 편집하고 있는 다음 라인에 입력한 명령의 출력결과가 입력될 것이다.

@@@@@@@@@@@@@@@[원문]@@@@@@@@@@@@@@@@

READ IN THE OUTPUT

While in VI, if you enter:

:r !my_command

Then you will insert the output of
the command you've entered on the
next line of the file that you're editing.

반응형
Posted by She쥐포s

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

달력

 « |  » 2024.5
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 31

최근에 올라온 글

최근에 달린 댓글

글 보관함