숙명여대 창병모 2011 가을 1. 파일 조작 filtering, sorting, comparing, searching for...

25
숙숙숙숙 숙숙숙 2011 숙숙 1

Transcript of 숙명여대 창병모 2011 가을 1. 파일 조작 filtering, sorting, comparing, searching for...

숙명여대 창병모2011 가을

1

파일 조작 filtering, sorting, comparing, searching for files

명령어 스케줄링 cron, at

텍스트 처리 프로그램 AWK

파일 보관 및 압축 archiving tar, gzip

2

3

filter all lines containing a specified pattern

4

grep -hilnvw pattern {fileName}*

fgrep -hilnvwx string {fileName}*

egrep-hilnvw pattern {fileName}*

Utility Kind of pattern

fgrep fixed string only

grep regular expression

egrep Extended regular expression

%grep -wn the grepfile

%grep -wnv the grepfile

%grep -w x *.c

%grep -wl x *.c

%grep “.nd” grepfile

%grep “sm.*ng” grepfile

%grep “[a-m]nd” grepfile

5

Sorting files

sort -tc -r {sortField -bfMn}* {fileName}*

- sorts files in ascending or descending order

- based on sort field(s)

Comparing files

cmp -ls fileName1 fileName2

testing for the sameness

diff -i -dflag fileName1 fileName2

file differences

6

%sort -r sortfile

%sort +0 -1 sortfile

%sort +0 -1 -bM sortfile

%cmp lady1 lady2

%cmp -l lady1 lady2

%diff lady1 lady2

7

find pathList expression

recursively descends through pathList and applies expression to every file.

expression-name pattern

-user userId

-atime count

-print

-ls

-exec command

8

%find . -name '*.c' -print

%find . -user chang -ls

%find . -mtime 14 -ls

%find . -mtime 14 -ls -exec rm {} \;

9

10

11

What is cron system in UNIX ?

- schedule a series of jobs to be executed on a periodic

basis using crontab (crontable)

crontab crontabNamecrontab -ler [userName]

1) crontab is the user interface to the cron system

2) when used without any options, the crontab file called

crontabName is registered and

3) its commands are executed according to the specified

timing rules

12

Crontab file

minute hour day month

weekday command

0-59 0-23 1-31 1-12 1-7(1=Mon) UNIX command

example : a crontab file called crontab.cron

0 8 * * 1 echo Happy Monday Moring

* * * * * echo One Minute Passed >

/dev/tty1

30 14 1 * * mail users % Jan Meeting At 3 pm

13

Unix 에서 처음 개발된 일반 스크립트 언어

텍스트 형태로 되어있는 데이터를 필드로 구분하여 처리하는 기능을 기본으로 한다 .

Alfred Aho, Peter Weinberger, Brian Kernighan

14

awk -Fc [-f fileName] program {fileName}*Scans the lines and performs actions on every line that matches a condition

awk program is a list of commands of the form[ condition] [ { action } ]

Examples

awk ‘{ print NF, $0}’ float

awk ‘{ print $1 $3 $NF}’ float

awk ‘NR > 1 && NR <4 { print NR, $1, $3, $NF}’ float

15

Condition the special token BEGIN or END an expression involving logical operators, relational

operators, and/or regular expressions

16

Action

if (conditional) statement [else statement]

while (conditional) statement

for (expression; conditional; expression)

statement

break

continue

variable=expression

17

Action

print [list of expressions]

printf format [, list of expressions]

next skips the remaining patterns of the current line of input)

exit skips the rest of the current line)

{list of statements}

18

awk2 BEGIN { print “Start of file:”, FILENAME }

{ print $1 $3 $NF }

END { print “End of file” }

awk -f awk2 float awk4

BEGIN { print “Scanning file” }

{ printf “line %d: %s\n”, NR, $0;

lineCount++;

wordCount + = NF

}

19

awk5 {for (I = NF; I >= 1; I--)

printf “%s “, $I

printf “\n”

}

awk6/t.*e/ {print $0}

awk7/strong/, /clear/ { print $0}

20

21

three utilities to archive files save files to a disk or tape

tar, cpio, dump

tar maintain an archive of files on a magnetic tape

create and access special tar-format archive files

a file can be added on the end of tar-format file

tar -cfrtuvx [tarFileName] fileList

22

% tar -cvf tarfile .

% tar -xvf tarfile

% tar -rvf tarfile reverse.c

% tar -cvf /dev/rst0 *

23

compress -cv {fileName}+replaces a file by its compressed version(.Z)-c option sends the compressed version to standard output

uncompress -cv {fileName}+re-creating the original file from its compressed file

%compress -v a.c b.c

%uncompress *.Z

24

gzip {fileName}+replaces a file by its compressed version(.gz)-c option sends the compressed version to standard output

gzip -d {fileName}+decompress the original files from its compressed file

gzip -l {fileName}+list compressed file contents

%gzip –v a.c b.c%gzip –d *.gz

25