UNIX 및실습 11 장보충 - sed 1
sed (1) 간결한비대화식편집기 쉘프롬프트에서편집명령과파일명을입력하면편집명령수행결과가화면에나타남 ( 원본은수정되지않음 ) 한번에한줄씩처리해서결과를화면에내보냄 sed 명령을파일에담아스크립트로만들어수행가능 정규표현식을이용하여패턴을검색하거나치환할수있음 (grep 과유사 ) sed n /RE/p filename sed n s/re/replacement_string/ filename 주소를지정하여편집하고자하는영역지정가능 sed 1,3d filename 2
명령과치환플래그 sed (2) 명령기능명령기능 a\ 현재줄에하나이상의줄추가 l 출력할수없는문자나열 c\ 현재줄의내용을새로운내용으로변경 p 줄을출력 d 현재줄삭제 n 다음입력줄을읽어서첫명령대신다음 명령을처리하고다음줄로넘어감 i\ 현재줄위에내용삽입 q sed 끝내거나종료됨 h H g G 패턴영역의내용을 holding buffer 에복사 패턴영역의내용을 holding buffer 에추가 holding buffer 에있는내용을패턴버퍼로복사, 패턴버퍼내용을덮어씀 holding buffer 에있는내용을패턴버퍼로복사, 패턴버퍼내용에추가 r 파일에서줄을읽음! 선택된줄을제외한모든줄에명령적용 s 문자열치환 3
sed (3) 명령과치환플래그 ( 계속 ) 플래그기능 옵션 플래그기능 g 각줄전체에대해치환 x Holding buffer와 pattern buffer 맞교환 p 줄을출력 y 한문자를다른문자로변환 ( 정규 표현식메타문자에 y를사용할수 없음 ) w 줄을파일에쓰기 옵션기능옵션기능 -e 다중편집기능 -f 스크립트파일지정 -n 기본출력금지 4
sed 예제 (1) datafile 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 Margot Weber 4.5.89 5 9 central CT Ann Stephens 5.7.94 5 13 1. sed /Main/d datafile - Main 이포함된모든줄삭제 2. sed /Main/!d datafile - Main 이포함되지않은모든줄삭제 5
sed 예제 (2) 출력 : p 명령 3. sed /north/p datafile north 가포함된줄두번출력 4. sed n /north/p datafile -n 옵션과동시사용하면해당줄만출력 삭제 : d 명령 5. sed 3d datafile 6. sed 3,$d datafile 7. sed /north/d datafile 6
sed 예제 (3) 치환 : s 명령 8. sed s/west/north/g datafile 9. sed n s/^west/north/p datafile 10. sed s/[0-9][0-9]$/&.5/ datafile & : 앞에서찾아진것을대표 11. sed n s/hemenway/jones/gp datafile 12. sed s#3#88#g datafile 원래는 s 다음에나오는문자는구분문자이며기본적으로 / 가사용되지만다른문자로바꿀수도있음 / 가포함된경로명과같은문자열을처리하고자하는경우반드시필요 7
sed 예제 (4) 줄선택범위지정 : 콤마 (,) 13. sed n /west/,/east/p datafile 14. sed n 5,/^northwest/p datafile 15. sed /west/,/east/s/$/**vaca**/ datafile 다중편집 : e 하나이상의편집명령이있는경우사용 명령이순서대로적용되므로명령순서에따라다른결과가나올수있음 16. sed e 1,3d e s/hemenway/jones/ datafile 8
파일에서읽기 : r newfile1 sed 예제 (5) * * * Suan has left the company * * * 17. sed /Suan/r newfile1 datafile 파일에쓰기 : w 18. sed n /north/w newfile2 datafile cat newfile2 9
sed 예제 (6) 추가 : a\ 명령 새로운텍스트가현재줄다음에추가 19. sed /^north/a\ --> The north district has moved datafile 삽입 : i\ 명령 새로운텍스트를현재줄위에추가 20. sed /^eastern/i\ New England Region\n ---------- datafile 변경 : c\ 명령 현재줄을완전히바꾸거나변경 21. sed /^western/c\ Western Region has been closed datafile 다음 : n 명령 다음줄을얻어서명령수행 22. sed /eastern/ { n; s/am/archie/; } datafile 수행할명령이여러개인경우 ; 으로구별하고 { } 로묶어준다 10
변환 : y 변환 sed 예제 (7) 23. sed 1,3y/abcdefghijkl/ABCDEFGHIJKL/ datafile a 부터 l 까지소문자를 A 부터 L 까지대문자로변환 종료 : q 명령 24. sed 5q datafile 25. sed /Lewis/ { s/lewis/joseph/; q; } datafile 11
sed 예제 (8) 26. sed 'G' datafile 빈줄추가 ( 현재 holding buffer 에내용이없으므로 ) 27. sed e /northeast/h e G datafile northeast 만나기전까지는빈줄추가 이후는 northeast 줄에있는내용출력 28. sed e /WE/{h; d;} e /CT/{ G; } datafile WE 만나서내용저장, 삭제 CT 만나면저장된내용패턴에덧붙이기 ( 출력 ) 29. sed e /Patricia/h e /Margot/x datafile Patricia 만나면저장 Margot 만나면저장된내용과교환 (Margot 줄대신 Patricia 줄출력 ) 30. sed n 1!G; h; $p datafile 역순출력 12
예제 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 31. sed f sedding1 datafile 13
실습과제 (sed 를포함한모든유틸리티사용 ) 예제파일 (databook) 1. 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 2. 처음 3 줄삭제 3. 5 번째줄에서 10 번째줄만출력 4. Lane 이포함된줄삭제 5. Fred 로시작하는줄뒤에 *** 추가 6. Jose 로시작하는줄을 Jose has retired 로치환 7. 다음을수행하는 sed 스크립트작성 (100 점 ) 첫줄에 Personal Records 제목삽입 봉급이 500 으로끝나면제거 마지막줄에 The End 추가 8. 중복된줄삭제하고, 봉급순으로정렬하여첫줄에 ***** Sorted by Salary ***** 제목삽입하고마지막에 ***** The End ***** 삽입하여출력 (200 점 ) 봉급 14