UNIX 및실습 11 장보충 - sed
sed (1) 간결한비대화식편집기 쉘프롬프트에서편집명령과파일명을입력하면편집명령수행결과가화면에나타남 ( 원본은수정되지않음 ) 한번에한줄씩처리해서결과를화면에내보냄 sed 명령을파일에담아스크립트로만들어수행가능 정규표현식을이용하여패턴을검색하거나치환할수있음 (grep 과유사 ) sed n /RE/p filename sed n s/re/replacement_string/ filename 주소를지정하여편집하고자하는영역지정가능 sed 1,3d filename
명령과치환플래그 sed (2) 명령기능명령기능 a\ 현재줄에하나이상의줄추가 l 출력할수없는문자나열 c\ 현재줄의내용을새로운내용으로변경 p 줄을출력 d 현재줄삭제 n 다음입력줄을읽어서첫명령대신다음 명령을처리하고다음줄로넘어감 i\ 현재줄위에내용삽입 q sed 끝내거나종료됨 h 패턴영역의내용을 holding buffer 에복사 r 파일에서줄을읽음 H 패턴영역의내용을 holding buffer 에추가! 선택된줄을제외한모든줄에명령적용 g G 플래그기능 holding buffer 에있는내용을패턴버퍼로복사, 패턴버퍼내용을덮어씀 holding buffer 에있는내용을패턴버퍼로복사, 패턴버퍼내용에추가 s 플래그기능 문자열치환 g 각줄전체에대해치환 x Holding buffer 와 pattern buffer 맞교환 p 줄을출력 y 한문자를다른문자로변환 ( 정규표현식 메타문자에 y를사용할수없음 ) w 줄을파일에쓰기
sed (3) 옵션 옵션기능옵션기능 -e 다중편집기능 -f 스크립트파일지정 -n 기본출력금지 예 sed /Tom/d file - Tom 이포함된모든줄삭제 sed /Tom/!d file - Tom 이포함되지않은모든줄삭제
datafile 출력 : p 명령 sed 예제 (1) northwest NW Charles Main 3.0.98 3 34 western WE Sharon Gray 5.3.97 5 23 southwest SW Lewis Dalsass 2.7.8 2 18 southern SO Suan Chin 5.1.95 4 15 southeast SE Patricia Hemenway 4.0.7 4 17 eastern EA TB Savage 4.4.84 5 20 northeast NE AM Main Jr. 5.1.94 3 13 north NO Marget Weber 4.5.89 5 9 central CT Ann Stephens 5.7.94 5 13 sed /north/p datafile sed n /north/p datafile -n 옵션과동시사용하면해당줄만출력
sed 예제 (2) 삭제 : d 명령 sed 3d datafile sed 3,$d datafile sed /north/d datafile 치환 : s 명령 sed s/west/north/g datafile sed n s/^west/north/p datafile sed s/[0-9][0-9]$/&.5/ datafile & : 앞에서찾아진것을대표 sed n s/hemenway/jones/gp datafile sed s#3#88#g datafile 원래는 s 다음에나오는문자는구분문자이며기본적으로 / 가사용되지만다른문자로바꿀수도있음 / 가포함된경로명과같은문자열을처리하고자하는경우반드시필요
sed 예제 (3) 줄선택범위지정 : 콤마 (,) sed n /west/,/east/p datafile sed n 5,/^northwest/p datafile sed /west/,/east/s/$/**vaca**/ datafile 다중편집 : e 하나이상의편집명령이있는경우사용 명령이순서대로적용되므로명령순서에따라다른결과가나올수있음 sed e 1,3d e s/hemenway/jones/ datafile
파일에서읽기 : r newfile1 * * * Suan has left the company * * * sed 예제 (4) sed /Suan/r newfile1 datafile 파일에쓰기 : w sed n /north/w newfile2 datafile cat newfile2
sed 예제 (5) 추가 : a\ 명령 새로운텍스트가현재줄다음에추가 sed /^north/a\ --> The north district has moved datafile 삽입 : i\ 명령 새로운텍스트를현재줄위에추가 sed /^eastern/i\ New England Region\n ---------- datafile 변경 : c\ 명령 현재줄을완전히바꾸거나변경 sed /^western/c\ Western Region has been closed datafile 다음 : n 명령 다음줄을얻어서명령수행 sed /eastern/ { n; s/am/archie/; } datafile 수행할명령이여러개인경우 ; 으로구별하고 { } 로묶어준다
변환 : y 변환 sed 예제 (6) sed 1,3y/abcdefghijkl/ABCDEFGHIJKL/ datafile a 부터 l 까지소문자를 A 부터 L 까지대문자로변환 종료 : q 명령 sed 5q datafile sed /Lewis/ { s/lewis/joseph/; q; } datafile
sed 예제 (7) 보류와얻기 : h 와 g 명령 sed e /northeast/h e $G datafile sed e /WE/{h; d;} e /CT/{ G; } datafile 보류와교환 : h 와 x 명령 sed e /Patricia/h e /Margot/x datafile
예제 1 sedding1 sed 스크립트예제 # 으로시작하면주석 # My first sed script /Lewis/a\ Lewis is the TOP Salesperson for April\ Lewis is moving to the Southern district next month. /Margot/c\ Margot has retired. 1i\ EMPLOYEE DATABASE\ --------------------------------------- $d sed f sedding1 datafile
실습과제 (sed 를포함한모든유틸리티사용 ) 예제파일 (databook) Jon 을 Jonathan 으로변경 Steve Blenheim:238-923-7366:95 Latham Lane, Easton, PA 83755:11/12/56:20300 Betty Boop:245-836-8357:635 Cutesy Lane, Hollywood, CA 91464:6/23/23:14500 Igor Chevsky:385-375-8395:3567 Populus Place, Caldwell, NJ 23875:6/18/68:23400 Norma Corder:397-857-2735:74 Pine Street, Dearborn, MI 23874:3/28/45:245700 Jennifer Cowan:548-834-2348:583 Laurel Ave., Kingsville, TX 83745:10/1/35:58900 Jon DeLoach:408-253-3122:123 Park St., San Jose, CA 04086:7/25/53:85100 Karen Evich:284-758-2857:23 Edgecliff Place, Lincoln, NB 92743:7/25/53:85100 Karen Evich:284-758-2867:23 Edgecliff Place, Lincoln, NB 92743:11/3/35:58200 Karen Evich:284-758-2867:23 Edgecliff Place, Lincoln, NB 92743:11/3/35:58200 Fred Fardbarkle:674-843-1385:20 Parak Lane, Duluth, MN 23850:4/12/23:780900 Fred Fardbarkle:674-843-1385:20 Parak Lane, Duluth, MN 23850:4/12/23:780900 Lori Gortz:327-832-5728:3465 Mirlo Street, Peabody, MA 34756:10/2/65:35200 Paco Gutierrez:835-365-1284:454 Easy Street, Decatur, IL 75732:2/28/53:123500 Ephram Hardy:293-259-5395:235 CarltonLane, Joliet, IL 73858:8/12/20:56700 James Ikeda:834-938-8376:23445 Aster Ave., Allentown, NJ 83745:12/1/38:45000 Barbara Kertz:385-573-8326:832 Ponce Drive, Gary, IN 83756:12/1/46:268500 Lesley Kirstin:408-456-1234:4 Harvard Square, Boston, MA 02133:4/22/62:52600 William Kopf:846-836-2837:6937 Ware Road, Milton, PA 93756:9/21/46:43500 Sir Lancelot:837-835-8257:474 Camelot Boulevard, Bath, WY 28356:5/13/69:24500 Jesse Neal:408-233-8971:45 Rose Terrace, San Francisco, CA 92303:2/3/36:25000 Zippy Pinhead:834-823-8319:2356 Bizarro Ave., Farmount, IL 84357:1/1/67:89500 Arthur Putie:923-835-8745:23 Wimp Lane, Kensington, DL 38758:8/31/69:126000 Popeye Sailor:156-454-3322:945 Bluto Street, Anywhere, USA 29358:3/19/35:22350 Jose Santiago:385-898-8357:38 Fife Way, Abilene, TX 39673:1/5/58:95600 Tommy Savage:408-724-0140:1222 Oxbow Court, Sunnyvale, CA 94087:5/19/66:34200 Yukio Takeshida:387-827-1095:13 Uno Lane, Ashville, NC 23556:7/1/29:57000 Vinh Tranh:438-910-7449:8235 Maple Street, Wilmington, VM 29085:9/23/63:68900 봉급 처음 3 줄삭제 5 번째불에서 10 번째줄만출력 Lane 이포함된줄삭제 Fred 로시작하는줄뒤에 *** 추가 Jose 로시작하는줄을 Jose has retired 로치환 다음을수행하는 sed 스크립트작성 첫줄에 Personal Records 제목삽입 봉급이 500 으로끝나면제거 마지막줄에 The End 추가 중복된줄삭제하고, 봉급순으로정렬하여첫줄에 ***** Sorted by Salary ***** 제목삽입하고마지막에 ***** The End ***** 삽입하여출력
[ 실습과제 ] 실습각단계화면캡처하여 pdf 파일로정리하여과제제출 (cms.mmu.ac.kr/bear) 제출기한 : 5 월 20 일자정