본문 바로가기

Study/class note

리눅스 / 리눅스 명령어

change directory 명령어로 디렉토리를 이동하는 명령어

오른쪽마우스 터미널 열기

 

# whoami : 접속한 내가 누구인지 확인하는 명령어

# pwd : 현재 내가 있는 디렉토리를 확인하는 명령어(print working directory)의 약자)

[root@localhost ~]# whoami
root
[root@localhost ~]# pwd
/root

# ls : list 명령어로 현재 디렉토리에 있는 폴더와 파일을 확인하는 명령어

# cd .. : 상위 디렉토리로 이동하기

# cd media : media 디렉토리로 이동

 

문제2. pwd했을때 현재 디렉토리가 아래의 디렉토리가 되게 하시오.

# cd home

# pwd

[root@localhost /]# cd home
[root@localhost home]# pwd

 

문제3. pwd했을 때 현재 디렉토리가 아래의 디렉토리가 되게 하시오.

# cd ..

# ls

# cd media

# pwd

[root@localhost /]# cd home
[root@localhost home]# pwd

 

ㅇ경로에는 크게 2가지 경로가 있음.

1. 절대경로 : cd "내가 가고자하는 위치"

[root@localhost media]#pwd

/media

[root@localhost media]# cd /root  <- 내가 이동하고자 하는 위치

[root@localhost media]# cd /root
[root@localhost ~]# pwd
/root

2. 상대경로 : 나의 현재 위치를 상대로 이동하겠다

[root@localhost media]# cd /root
[root@localhost ~]# pwd
/root

나의 현재위치를 상대로 상위 디렉토리로 이동하겠다는 것

 

문제4. pwd했을때 아래의 디렉토리가 되게 하시오.

# pwd

/home/oracle

답 : 

# cd / home / oracle

절대경로를 써서 바로가게함.

#상대경로
[root@localhost /]# cd home
[root@localhost home]# pwd
/home
[root@localhost home]# ls
oracle
[root@localhost home]# cd oracle
[root@localhost oracle]# pwd
/home/oracle

#절대경로
[root@localhost home]# cd /home/oracle
[root@localhost oracle]# pwd
/home/oracle

 

문제5. 지금 현재 상태에서 나의 집으로 돌아가고 싶다면?

#whoami  : 내가 누군지 확인하는 것

자기 이름으로 된 폴더가 바로 자기 집

#cd 

#pwd

cd하고 바로 엔터치면 자기 집으로 바로 이동

[root@localhost home]# whoami
root
[root@localhost home]# cd
[root@localhost ~]# pwd
/root

 

문제6. /home/oracle밑으로 이동하세요.

# cd / home / oracle

[root@localhost ~]# cd /home/oracle
[root@localhost oracle]# pwd
/home/oracle

 

문제7. 자기 집으로 돌아오세요.

#cd

#pwd

[root@localhost oracle]# cd
[root@localhost ~]# pwd
/root

 

문제8. 나의 집에서 test01이라는 디렉토리를 생성하시오.

#mkdir tes01  = make directory

#ls

[root@localhost ~]# mkdir test01
[root@localhost ~]# ls
anaconda-ks.cfg  emp.txt               test01  다운로드  바탕화면  사진  음악
dept2.txt        initial-setup-ks.cfg  공개    문서      비디오    서식
[root@localhost ~]#

 

문제9. 지금 생성한 test01 디렉토리로 이동하시오.

#cd test01

#pwd

[root@localhost ~]# cd test01
[root@localhost test01]# pwd
/root/test01

 

2  touch 명령어

"파일의 용량이 0인 파일을 생성하는 명령어"

[root@localhost ~]# cd
[root@localhost ~]# touch a1.txt
[root@localhost ~]# ls
a1.txt           dept2.txt  initial-setup-ks.cfg  공개      문서      비디오  서식
anaconda-ks.cfg  emp.txt    test01                다운로드  바탕화면  사진    음악

 

문제10. 나의 집에서 아래의 파일들을 크기가 0으로 생성하시오.

[root@localhost ~]# mkdir a.txt b.txt c.txt e.txt f.txt
[root@localhost ~]# ls
a.txt            b.txt      e.txt    initial-setup-ks.cfg  다운로드  비디오  음악
a1.txt           c.txt      emp.txt  test01                문서      사진
anaconda-ks.cfg  dept2.txt  f.txt    공개                  바탕화면  서식

# ls -l *.txt   <-- 확장자가 .txt로 끝나는 파일들을 보여줌

-l 은 list의 정보를 보여달라는 옵션임.

[root@localhost ~]# ls -l *.txt
-rw-r--r--. 1 root root    0  3월 10 13:38 a1.txt
-rwxrwx---. 1 root root   80  3월 10 12:10 dept2.txt
-rwxrwx---. 1 root root 1036  3월 10 11:29 emp.txt

a.txt:
합계 0

b.txt:
합계 0

c.txt:
합계 0

e.txt:
합계 0

f.txt:
합계 0

 

 

3  mkdir 명령어

"디렉토리를 만드는 명령어"

[root@localhost ~]# cd
[root@localhost ~]# mkdir bigdata
[root@localhost ~]# ls
a.txt            bigdata           e.txt     initial-setup-ks.cfg  test01         문서      서식
a1.txt           c.txt             emp.txt   jobs.txt              test_list.txt  바탕화면  음악
anaconda-ks.cfg  dept2.txt         emp2.txt  jobs_tail.txt         공개           비디오
b.txt            dept2_backup.txt  f.txt     notsalesman.txt       다운로드       사진

whatis 명령어 : 명령어에 대한 설명

[root@localhost ~]# whatis mkdir
mkdir (1)            - 경로 만들기
mkdir (2)            - 디렉토리를 만든다.
mkdir (1p)           - make directories
mkdir (3p)           - make a directory

man 명령어 : 명령어에 대한 메뉴얼 설명 ( 빠져나오고 싶을 때는 q)

[root@localhost ~]# man mkdir

 

문제11. /root/bigdata 밑에 test01이라는 디렉토리를 생성하시오.

[root@localhost ~]# cd /root/bigdata
[root@localhost bigdata]# mkdir test01
[root@localhost bigdata]# ls
test01

문제12. /root/bigdata 밑에 /test01/test02/test03/test04/test05 디렉토리를 만드시오

[root@localhost bigdata]# cd test01
[root@localhost test01]# pwd
/root/bigdata/test01
[root@localhost test01]# mkdir test02
[root@localhost test01]# pwd
/root/bigdata/test01
[root@localhost test01]# ls
test02
[root@localhost test01]# cd test02
[root@localhost test02]# pwd
/root/bigdata/test01/test02
[root@localhost test02]# mkdir test03
[root@localhost test02]# cd test03
[root@localhost test03]# mkdir test04
[root@localhost test03]# cd test04
[root@localhost test04]# mkdir test05
[root@localhost test04]# pwd
/root/bigdata/test01/test02/test03/test04
[root@localhost test04]# cd test05
[root@localhost test05]# pwd
/root/bigdata/test01/test02/test03/test04/test05

 

문제13. 이제 다시 자기집으로 가시오.

[root@localhost test05]# cd
[root@localhost ~]# pwd
/root

 

문제14. /root/bigdata2/test01/test02/test03/test04/test05 디렉토리를 생성하시오.

[root@localhost ~]# mkdir -p bigdata2/test01/test02/test03/test04/test05
[root@localhost ~]# 
[root@localhost ~]# cd bigdata2
[root@localhost bigdata2]# cd test01
[root@localhost test01]# cd test02
[root@localhost test02]# cd test03
[root@localhost test03]# cd test04
[root@localhost test04]# cd test05
[root@localhost test05]# pwd
/root/bigdata2/test01/test02/test03/test04/test05

-p 옵션은 디렉토리를 만들면서 한번에 하위 디렉토리를 생성하는 옵션

 

 

4  rm명령어

"파일이나 디렉토리에 삭제하는 명령어"

※주의사항

리눅스나 유닉스는 윈도우와 같이 휴지통이 있기는 한데 명령어로 삭제할때는 휴지통에 삭제된 파일이 안들어가므로 삭제할 때 주의해야함.

 

#cd : 집으로 가서

#touch bbb.txt : 파일생성

#ls : 확인

#rm bbb.txt  : 파일삭제 >  물어보면 y 선택

#ls : 확인

[root@localhost ~]# cd
[root@localhost ~]# touch bbb.txt
[root@localhost ~]# ls
a.txt            bigdata2          deptno_30.csv         jobs.txt         공개      서식
a1.txt           c.txt             e.txt                 jobs_tail.txt    다운로드  음악
anaconda-ks.cfg  dept2.txt         emp.txt               notsalesman.txt  문서
b.txt            dept2_backup.txt  emp2.txt              result44.txt     바탕화면
bbb.txt          deptno_10.csv     f.txt                 test01           비디오
bigdata          deptno_20.csv     initial-setup-ks.cfg  test_list.txt    사진
[root@localhost ~]# rm bbb.txt
rm: remove 일반 빈 파일 `bbb.txt'? y

 

문제15. root에 있는 a.txt와 b.txt를 rm으로 지우시오.

#cd

#rm a.txt 

#rm b.txt

#ls

 

※ rm -rf * : 현재 디렉토리 밑에 있는 모든 파일과 디렉토리를 다 삭제하겠음.

-r 옵션 : 현재 디렉토리 밑에 있는 모든 파일과 디렉토리를 삭제하겠음

-f 옵션 : 삭제할 때 원래 삭제할까요? 라고 물어보는데 물어보지 않고 그냥 강제로 다 삭제해라~

* 은 모두 다 라는 뜻

 

 

5  rmdir 명령어

"디렉토리를 삭제하는 명령어"

예제: 

#rmdir 디렉토리명

[root@localhost ~]# cd
[root@localhost ~]# mkdir ddd
[root@localhost ~]# ls
a1.txt           ddd               deptno_30.csv  initial-setup-ks.cfg  test01         바탕화면
anaconda-ks.cfg  dept2.txt         e.txt          jobs.txt              test_list.txt  비디오
bigdata          dept2_backup.txt  emp.txt        jobs_tail.txt         공개           사진
bigdata2         deptno_10.csv     emp2.txt       notsalesman.txt       다운로드       서식
c.txt            deptno_20.csv     f.txt          result44.txt          문서           음악
[root@localhost ~]# rmdir ddd
[root@localhost ~]# ls

 

문제16. 집에 있는 bigdata라는 디렉토리를 삭제하시오

#rmdir bigdata : 디렉토리가 비어있지 않아서 삭제되지 않음.

[root@localhost ~]# rmdir bigdata
rmdir: failed to remove `bigdata': 디렉터리가 비어있지 않음

#rm - rf bigdata : 싹 지워짐

[root@localhost ~]# rm -rf bigdata

 

문제17. 윈도우에 있는 리눅스 실습용 emp.txt를 리눅스 시스템에 넣으시오.

위에 메뉴에 장치에 드래그앤드롭 양방향으로 하고 붙여넣기 하시오.

혹은 공유폴더 설정해서 윈도우의 emp.txt를 리눅스 시스템에 넣습니다.

 

ㅇ공유폴더 설정 방법

: 설정 --> 공유폴더 --> 공유폴더 추가 --> 리눅스 바탕화면에 share 라는 디스크 생김

 

 

6  alias 명령어

"자주 수행하는 명령어들을 쉽게 사용할 수 있도록 설정하는 명령어"

ex) 파이썬으로 접속하세요~

> alias 이용해서 파이썬 접속하기

[root@localhost ~]# alias p="python"
[root@localhost ~]# p
Python 2.7.5 (default, Oct 14 2020, 14:45:30) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

 

문제18. ls -l 명령어의 alias를 l로 생성하시오.

[root@localhost ~]# alias l="ls -l"
[root@localhost ~]# l

 

문제19. alias로 등록된 명령어들을 전부 출력하시오

[root@localhost ~]# alias

 

문제20. p alias를 지우고 싶다면?

[root@localhost ~]# unalias p
[root@localhost ~]# p
bash: p: 명령을 찾을 수 없습니다...

 

문제21. l alias도 지우세요.

[root@localhost ~]# unalias l
[root@localhost ~]# l
bash: l: 명령을 찾을 수 없습니다...

 

 

7  cat명령어

"파일의 내용을 화면에 출력하는 명령어"

[root@localhost ~]# cat emp.txt
7839     KING     PRESIDENT     0     1981-11-17     5000     0     10
7698     BLAKE     MANAGER     7839     1981-05-01     2850     0     30
7782     CLARK     MANAGER     7839     1981-05-09     2450     0     10
7566     JONES     MANAGER     7839     1981-04-01     2975     0     20
7654     MARTIN     SALESMAN     7698     1981-09-10     1250     1400     30
7499     ALLEN     SALESMAN     7698     1981-02-11     1600     300     30
7844     TURNER     SALESMAN     7698     1981-08-21     1500     0     30
7900     JAMES     CLERK     7698     1981-12-11     950     0     30
7521     WARD     SALESMAN     7698     1981-02-23     1250     500     30
7902     FORD     ANALYST     7566     1981-12-11     3000     0     20
7369     SMITH     CLERK     7902     1980-12-09     800     0     20
7788     SCOTT     ANALYST     7566     1982-12-22     3000     0     20
7876     ADAMS     CLERK     7788     1983-01-15     1100     0     20
7934     MILLER     CLERK     7782     1982-01-11     1300     0     10

 

 

문제22. (점심시간 문제) dept2.txt를 리눅스 시스템에 넣고  cat으로 dept2.txt를 출력하시오. 출력화면을 캡쳐해서 올려주세요.

[root@localhost ~]# cat dept2.txt
10,ACCOUNTING,NEW YORK
20,RESEARCH,DALLAS
30,SALES,CHICAGO
40,OPERATIONS,BOSTON

 

 

8  redirection 명령어

"화면에 출력되는 결과를 파일로 저장하는 명령어"

>> : 기존에 같은 파일의 이름이 없으면 파일을 생성하고 있으면 기존 파일 뒤에 덧붙이겠다.

> : 파일 생성. 기존에 똑같은 이름의 파일이 있으면 덮어쓰겠다

[root@localhost ~]# cat emp.txt >> emp2.txt

emp.txt를 화면에 출력하는데 출력되는 결과를 emp2.txt로 저장해라라는 뜻

[root@localhost ~]# ls emp2.txt
emp2.txt   # 생성확인

[root@localhost ~]# cat emp.txt >> emp2.txt  # 한번 더 붙여짐

: 텍스트 파일을 백업할 때 많이 씀

 

문제23. dept2.txt를 cat으로 출력하는 결과를 dept2_backup.txt로 생성하시오.

[root@localhost ~]# cd
[root@localhost ~]# cat dept2.txt >> dept2_backup.txt
[root@localhost ~]# ls dept2_backup.txt
dept2_backup.txt
[root@localhost ~]# ls -l dept2_backup.txt
-rw-r--r--. 1 root root 80  3월 10 13:51 dept2_backup.txt

확장자가 .txt로 끝나는 파일들의 리스트를 보여달라.

[root@localhost ~]# ls -l *.txt
-rwxrwx---. 1 root root    80  3월 10 12:10 dept2.txt
-rw-r--r--. 1 root root    80  3월 10 13:51 dept2_backup.txt
-rwxrwx---. 1 root root  1036  3월 10 11:29 emp.txt
-rw-r--r--. 1 root root  2072  3월 10 13:49 emp2.txt
-rwxrwx---. 1 root root 12176 11월 23 11:58 jobs.txt
-rw-r--r--. 1 root root   651  3월 10 14:10 jobs_tail.txt
-rw-r--r--. 1 root root   194  3월 10 15:25 notsalesman.txt
-rw-r--r--. 1 root root    33  3월 10 16:13 result44.txt
-rw-r--r--. 1 root root    66  3월 10 16:48 result49.txt
-rw-r--r--. 1 root root   505  3월 10 13:55 test_list.txt

문제 ls -l *.txt로 출력되는 결과를 text_list.txt로 생성하시오.

[root@localhost ~]# ls -l *.txt >> test_list.txt
[root@localhost ~]# cat test_list.txt
a1.txt
dept2.txt
dept2_backup.txt
emp.txt
emp2.txt

a.txt:

b.txt:

c.txt:

e.txt:

f.txt:
-rw-r--r--. 1 root root    0  3월 10 13:38 a1.txt
-rwxrwx---. 1 root root   80  3월 10 12:10 dept2.txt
-rw-r--r--. 1 root root   80  3월 10 13:51 dept2_backup.txt
-rwxrwx---. 1 root root 1036  3월 10 11:29 emp.txt
-rw-r--r--. 1 root root 2072  3월 10 13:49 emp2.txt
-rw-r--r--. 1 root root   91  3월 10 13:55 test_list.txt

a.txt:
합계 0

b.txt:
합계 0

c.txt:
합계 0

e.txt:
합계 0

f.txt:
합계 0
[root@localhost ~]# cd
[root@localhost ~]# ls -l test_list.txt
-rw-r--r--. 1 root root 505  3월 10 13:55 test_list.txt

 

 

9  more 명령어

1페이지가 넘는 문서의 내용을 화면에 출력할 떄 페이지 단위로 볼 수 있는 명령어

먼저 스티브 잡스 연선문인 jobs.txt를 리눅스 시스템에 넣으시오.

 

예제. cat으로 볼 경우 그냥 스크립트 한바닥 가득 채워서 나옴.

[root@localhost ~]# ls  jobs.txt
jobs.txt
[root@localhost ~]# cat jobs.txt
steve jobs'2005 stanford commencement address
I am honored to be with you today at your commencement from one of the finest universities in the world. 
I never graduated from college. 
Truth be told, this is the closest I've ever gotten to a college graduation. 
Today I want to tell you three stories from my life. 
That's it. 
No big deal. 
Just three stories. 
The first story is about connecting the dots. 
I dropped out of Reed College after the first 6 months, but then stayed around as a drop-in for another 18 months or so before I really quit. 
So why did I drop out? 
It started before I was born. 
My biological mother was a young, unwed college graduate student, and she decided to put me up for adoption. 
She felt very strongly that I should be adopted by college graduates, so everything was all set for me to be adopted at birth by a lawyer and his wife. 
Except that when I popped out they decided at the last minute that they really wanted a girl. 
So my parents, who were on a waiting list, got a call in the middle of the night asking: 
"We have an unexpected baby boy;
[root@localhost ~]# more jobs.txt

전진 키 : 스페이스

후진 키 : b

페이지 단위로 내려가는 거 : f

 

 

10  head 명렁어

" 문서의 처음 몇 줄을 화면에 출력하는 명령어"

 

예제.

#head 출력줄수 파일명

[root@localhost ~]# head -10 jobs.txt
steve jobs'2005 stanford commencement address
I am honored to be with you today at your commencement from one of the finest universities in the world. 
I never graduated from college. 
Truth be told, this is the closest I've ever gotten to a college graduation. 
Today I want to tell you three stories from my life. 
That's it. 
No big deal. 
Just three stories. 
The first story is about connecting the dots. 
I dropped out of Reed College after the first 6 months, but then stayed around as a drop-in for another 18 months or so before I really quit. 
[root@localhost ~]#

 

문제25. emp.txt의 위의 3줄을 출력하시오.

[root@localhost ~]# head -3 emp.txt
7839     KING     PRESIDENT     0     1981-11-17     5000     0     10
7698     BLAKE     MANAGER     7839     1981-05-01     2850     0     30
7782     CLARK     MANAGER     7839     1981-05-09     2450     0     10
[root@localhost ~]#

 

문제26. emp.txt의 뒤의 3줄을 출력하시오.

[root@localhost ~]# tail -3 emp.txt
7788     SCOTT     ANALYST     7566     1982-12-22     3000     0     20
7876     ADAMS     CLERK     7788     1983-01-15     1100     0     20
7934     MILLER     CLERK     7782     1982-01-11     1300     0     10
[root@localhost ~]#

 

문제27. jobs.txt의 뒤의 10줄을 jobs_tail.txt로 저장하고 jobs_tail.txt를 화면에 출력하시오.

[root@localhost ~]# tail -10 jobs.txt >> jobs_tail.txt
[root@localhost ~]# cat jobs_tail.txt
Stewart and his team put out several issues of The Whole Earth Catalog, and then when it had run its course, they put out a final issue. It was the mid-1970s, and I was your age. 
On the back cover of their final issue was a photograph of an early morning country road, the kind you might find yourself hitchhiking on if you were so adventurous. 
Beneath it were the words: 
"Stay Hungry. Stay Foolish." 
It was their farewell message as they signed off. 
Stay Hungry. Stay Foolish. 
And I have always wished that for myself. 
And now, as you graduate to begin anew, I wish that for you. 
Stay Hungry. Stay Foolish. 
Thank you all very much. [root@localhost ~]#

 

 

11  wc 명령어

"파일 안의 단어의 갯수 또는 라인수를 출력하는 명령어"

[root@localhost ~]# wc jobs.txt
  143  2260 12176 jobs.txt

라인수 143

단어갯수 2260

문자수(철자의 갯수) 12176

[root@localhost ~]# wc -l jobs.txt   #라인수
143 jobs.txt
[root@localhost ~]# wc -w jobs.txt   #단어수
2260 jobs.txt
[root@localhost ~]# wc -c jobs.txt   #철자의 개수
12176 jobs.txt

 

문제28. emp.txt의 전체 라인수를 출력하시오.

[root@localhost ~]# wc -l emp.txt
14 emp.txt

 

 

12  grep 명령어

"파일 안에 포함된 특정 단어나 구문을 검색하는 명령어"

[root@localhost ~]# grep 'SCOTT' emp.txt
7788     SCOTT     ANALYST     7566     1982-12-22     3000     0     20

emp.txt에서 SCOTT이라는 단어가 포함된 행을 보여줌

 

[root@localhost ~]# grep -i 'scott' emp.txt
7788     SCOTT     ANALYST     7566     1982-12-22     3000     0     20

-i 옵션은 대소문자를 구분하지 않겠다는 뜻

 

문제29. 직업이 salesman인 사원들의 모든 행을 출력하시오.

[root@localhost ~]# grep -i 'salesman' emp.txt
7654     MARTIN     SALESMAN     7698     1981-09-10     1250     1400     30
7499     ALLEN     SALESMAN     7698     1981-02-11     1600     300     30
7844     TURNER     SALESMAN     7698     1981-08-21     1500     0     30
7521     WARD     SALESMAN     7698     1981-02-23     1250     500     30

 

문제30. 직업이 salesman인 사원들의 이름과 월급을 출력하시오.

[root@localhost ~]# grep -i 'salesman' emp.txt | awk '{ print $2, $6 }'
MARTIN 1250
ALLEN 1600
TURNER 1500
WARD 1250

 

ㅇpipe 명령어 : 앞의 명령어 | 뒤의 명령어

앞의 명령어의 출력을 뒤의 명령어의 입력으로 보냄으로서 실행 결과를 다음 명령어로 전달하는 기능

 

ㅇ awk '{ print $2, $6 }' : awk는 컬럼을 선택하는 명령어 $2, $6 (2번째 컬럼과 6번째 컬럼)을 출력해라~

[root@localhost ~]# awk '{print $2, $6}' emp.txt

 

문제31. 이름이 scott인 사원의 이름과 월급과 직업을 출력하시오.

[root@localhost ~]# grep -i 'scott' emp.txt | awk '{print $2, $3, $6 }'
SCOTT ANALYST 3000

 

문제32. 직업이 analyst인 사원들의 이름, 직업, 부서번호를 출력하시오.

[root@localhost ~]# grep -i 'analyst' emp.txt | awk '{print $2, $3, $8}'
FORD ANALYST 20
SCOTT ANALYST 20

 

리눅스 명령어로 이렇게 txt파일의 내용을 검색할 때의 장점?

데이터를 검색하려면 오라클 데이터베이스에 데이터를 입력하거나 아니면 판다스로 해야하는데 만약 데이터가 몇 억건이나 되는 대용량 파일이면 오라클 db에 넣는 준비작업만해도 오래걸림. 테이블 생성, insert 해야하고, 데이터 베이스에 데이터를 저장할 공간도 해야함. 

따라서 리눅스 명령어로 바로 검색하면 훨씬 빠르고 간편함.

 

문제33. 부서번호가 10번인 사원들의 이름과 월급을 출력하시오.

[root@localhost ~]# awk '$8 == 10 {print $2, $6}' emp.txt
KING 5000
CLARK 2450
MILLER 1300

$8이 8번째 컬럼을 뜻함. $8 == 10 은 8번째 컬럼의 데이터가 10이면~ 이라는 조건을 뜻함.

 

+) -w는 단어별 검색할때 사용

 

ㅇ리눅스의 연산자 3가지

1. 산술연산자 : +, -, *, / 

2. 비교연산자 : <, >, >=, <=, ==, !=

3. 논리연산자 : &&, ||, !

 

문제34. 직업이 salesman인 사원들이 전부 몇명인지 출력하시오.

[root@localhost ~]# grep -i 'salesman' emp.txt | wc -l
4

 

문제35. 부서번호가 10번인 사원들이 전부 몇명인지 출력하시오.

[root@localhost ~]# awk '$8 == 10' emp.txt | wc -l
3

[root@localhost ~]# awk '$8 == 10 {print $0}' emp.txt     
7839     KING     PRESIDENT     0     1981-11-17     5000     0     10
7782     CLARK     MANAGER     7839     1981-05-09     2450     0     10
7934     MILLER     CLERK     7782     1982-01-11     1300     0     10

{print $0} : 모든 컬럼이 다 출력됨

 

 

13  awk 명령어

"특정 컬럼을 출력하고자 할 때 사용하는 명령어"

[root@localhost ~]# awk '$3 == "SALESMAN" {print $2, $3}' emp.txt
MARTIN SALESMAN
ALLEN SALESMAN
TURNER SALESMAN
WARD SALESMAN

 

문제36. 월급이 3000 이상인 사원들의 이름과 월급을 출력하시오.

[root@localhost ~]# awk '$6 >= 3000 {print $2, $6}' emp.txt
KING 5000
FORD 3000
SCOTT 3000

 

문제37. 직업이 salesman이 아닌 사원들의 이름과 직업을 출력하시오.

[root@localhost ~]# awk '$3 != 'SALESMAN' {print $2, $3}' emp.txt
KING PRESIDENT
BLAKE MANAGER
CLARK MANAGER
JONES MANAGER
MARTIN SALESMAN
ALLEN SALESMAN
TURNER SALESMAN
JAMES CLERK
WARD SALESMAN
FORD ANALYST
SMITH CLERK
SCOTT ANALYST
ADAMS CLERK
MILLER CLERK

 

문제38. 지금 위에서 출력된 결과를 notsalesman.txt로 저장하시오.

[root@localhost ~]# awk '$3 != 'SALESMAN' {print $2, $3}' emp.txt >> notsalesman.txt
[root@localhost ~]# ls notsalesman.txt
notsalesman.txt

[root@localhost ~]# cat notsalesman.txt 
KING PRESIDENT
BLAKE MANAGER
CLARK MANAGER
JONES MANAGER
MARTIN SALESMAN
ALLEN SALESMAN
TURNER SALESMAN
JAMES CLERK
WARD SALESMAN
FORD ANALYST
SMITH CLERK
SCOTT ANALYST
ADAMS CLERK
MILLER CLERK

 

문제39. 직업이 salesman이고 월급이 1200 이상인 사원들의 이름과 월급과 직업을 출력하시오.

[root@localhost ~]# awk '$3 == "SALESMAN" && $6 >= 1200 {print $2, $6, $3}' emp.txt
MARTIN 1250 SALESMAN
ALLEN 1600 SALESMAN
TURNER 1500 SALESMAN
WARD 1250 SALESMAN

 

문제40. 1981년도에 입사한 사원들의 이름과 입사일을 출력하시오.

[root@localhost ~]# awk 'substr($5, 1,4) == "1981" {print $2, $5}' emp.txt
KING 1981-11-17
BLAKE 1981-05-01
CLARK 1981-05-09
JONES 1981-04-01
MARTIN 1981-09-10
ALLEN 1981-02-11
TURNER 1981-08-21
JAMES 1981-12-11
WARD 1981-02-23
FORD 1981-12-11

substr(문자열, 시작자릿수, 끝자릿수)

 

문제41. 이름의 첫번째 철자가 A로 시작하는 사원들의 이름과 월급을 출력하시오.

[root@localhost ~]# awk 'substr($2,1,1) == "A" {print $2, $6}' emp.txt
ALLEN 1600
ADAMS 1100

 

 

15  sort 명령어

"data를 특정 컬럼을 기준으로 정렬하는 명령어"

예제 : sort 옵션 파일명

-k : ascending

-rk : descending

-nk : 숫자 ascending

-nrk : 숫자 ascending

 

월급이 낮은 사원부터 높은 사원순으로 출력해라. (ascending)

월급이 높은 사원부터 낮은 사원순으로 출력해라. (descending)

[root@localhost ~]# sort -nk 6 emp.txt
7369     SMITH     CLERK     7902     1980-12-09     800     0     20
7900     JAMES     CLERK     7698     1981-12-11     950     0     30
7876     ADAMS     CLERK     7788     1983-01-15     1100     0     20
7521     WARD     SALESMAN     7698     1981-02-23     1250     500     30
7654     MARTIN     SALESMAN     7698     1981-09-10     1250     1400     30
7934     MILLER     CLERK     7782     1982-01-11     1300     0     10
7844     TURNER     SALESMAN     7698     1981-08-21     1500     0     30
7499     ALLEN     SALESMAN     7698     1981-02-11     1600     300     30
7782     CLARK     MANAGER     7839     1981-05-09     2450     0     10
7698     BLAKE     MANAGER     7839     1981-05-01     2850     0     30
7566     JONES     MANAGER     7839     1981-04-01     2975     0     20
7788     SCOTT     ANALYST     7566     1982-12-22     3000     0     20
7902     FORD     ANALYST     7566     1981-12-11     3000     0     20
7839     KING     PRESIDENT     0     1981-11-17     5000     0     10


[root@localhost ~]# sort -nrk 6 emp.txt
7839     KING     PRESIDENT     0     1981-11-17     5000     0     10
7902     FORD     ANALYST     7566     1981-12-11     3000     0     20
7788     SCOTT     ANALYST     7566     1982-12-22     3000     0     20
7566     JONES     MANAGER     7839     1981-04-01     2975     0     20
7698     BLAKE     MANAGER     7839     1981-05-01     2850     0     30
7782     CLARK     MANAGER     7839     1981-05-09     2450     0     10
7499     ALLEN     SALESMAN     7698     1981-02-11     1600     300     30
7844     TURNER     SALESMAN     7698     1981-08-21     1500     0     30
7934     MILLER     CLERK     7782     1982-01-11     1300     0     10
7654     MARTIN     SALESMAN     7698     1981-09-10     1250     1400     30
7521     WARD     SALESMAN     7698     1981-02-23     1250     500     30
7876     ADAMS     CLERK     7788     1983-01-15     1100     0     20
7900     JAMES     CLERK     7698     1981-12-11     950     0     30
7369     SMITH     CLERK     7902     1980-12-09     800     0     20

 

 

문제42. 직업이 SALESMAN인 사원들의 이름과 월급을 출력하는데 월급이 높은 사원부터 출력하시오.

[root@localhost ~]# awk '$3 == "SALESMAN" {print $2, $6}' emp.txt | sort -nrk 2
ALLEN 1600
TURNER 1500
WARD 1250
MARTIN 1250

이미 첫번째 컬럼에서 데이터가 분류됨 > 파이프라인을 통해 정렬할 때 컬럼순서가 재지정되므로 순서 유의해야함. 이름이 1번컬럼, 월급이 2번컬럼.

 

문제43. 월급이 1200 이상인 사원들의 이름과 입사일을 출력하는데 최근에 입사한 사원부터 출력하시오.

[root@localhost ~]# awk '$6 >= 1200 {print $2, $5}' emp.txt | sort -rk 2
SCOTT 1982-12-22
MILLER 1982-01-11
FORD 1981-12-11
KING 1981-11-17
MARTIN 1981-09-10
TURNER 1981-08-21
CLARK 1981-05-09
BLAKE 1981-05-01
JONES 1981-04-01
WARD 1981-02-23
ALLEN 1981-02-11

 

문제44. 부서번호가 20번이고 직업이 CLERK인 사원들의 이름과 월급과 직업을 출력하는데 월급이 높은 사원부터 출력되게 하시오.

[root@localhost ~]# awk '$8 == 20 && $3 == "CLERK" {print $2, $6, $3}' emp.txt | sort -nrk 2
ADAMS 1100 CLERK
SMITH 800 CLERK

 

문제45. 위에서 출력된 결과를 result44.txt로 저장하시오.

[root@localhost ~]# awk '$8 == 20 && $3 == "CLERK" {print $2, $6, $3}' emp.txt | sort -nrk 2 >> result44.txt
[root@localhost ~]# ls result44.txt
result44.txt

[root@localhost ~]# cat result44.txt
ADAMS 1100 CLERK
SMITH 800 CLERK

 

 

16  uniq 명령어

"중복된 라인을 제거하는 명령어"

예제. $uniq 옵션 파일명

 

직업을 중복제거해서 출력하시오.

예제1. emp.txt에서 직업만 출력하시오.

[root@localhost ~]# awk '{print $3}' emp.txt
PRESIDENT
MANAGER
MANAGER
MANAGER
SALESMAN
SALESMAN
SALESMAN
CLERK
SALESMAN
ANALYST
CLERK
ANALYST
CLERK
CLERK

예제2. 위의 결과를 abcd순으로 정렬해서 출력하시오.

[root@localhost ~]# awk '{print $3}' emp.txt | sort -k 1
ANALYST
ANALYST
CLERK
CLERK
CLERK
CLERK
MANAGER
MANAGER
MANAGER
PRESIDENT
SALESMAN
SALESMAN
SALESMAN
SALESMAN

 

예제3. 위의 결과를 출력할 때 중복을 제거해서 출력하시오.

[root@localhost ~]# awk '{print $3}' emp.txt | sort -k 1 | uniq
ANALYST
CLERK
MANAGER
PRESIDENT
SALESMAN

 

문제46. 부서번호를 출력하는데 중복을 제거해서 출력하시오.

[root@localhost ~]# awk '{print $8}' emp.txt | sort -nk 1 | uniq
10
20
30

 

문제47. 부서번호가 20번인 사원들의 직업을 출력하는데 중복을 제거해서 출력하시오.

[root@localhost ~]# awk '$8 == 20 {print $3}' emp.txt | sort -k 1 | uniq
ANALYST
CLERK
MANAGER

 

문제48. 고객.csv 파일을 연령대별로 각각 분리해서 판다스 데이터 프레임을 만들려고 합니다. 그래서 고객이 20대면 20.csv, 30대 30.csv, 40대 40.csv, 50대 50.csv 로 생성함. 이처럼 부서번호가 10번인 사원들은 deptno_10.csv로 생성하고 20번인 사원들은 deptno_20.csv로 생성, 부서번호가 30번인 사원들은 deptno_30.csv로 생성하시오.

[root@localhost ~]# awk '$8 == 10 {print $0}' emp.txt >> deptno_10.csv
[root@localhost ~]# cat deptno_10.csv
7839     KING     PRESIDENT     0     1981-11-17     5000     0     10
7782     CLARK     MANAGER     7839     1981-05-09     2450     0     10
7934     MILLER     CLERK     7782     1982-01-11     1300     0     10
[root@localhost ~]# awk '$8 == 20 {print $0}' emp.txt >> deptno_20.csv
[root@localhost ~]# awk '$8 == 30 {print $0}' emp.txt >> deptno_30.csv

 

문제49. (오늘의 마지막 문제) 직업이 MANAGER인 사원들의 이름과 월급과 입사일을 출력하는데 월급이 높은 사원부터 출력되게 하고 그 출력된 결과를 result49.txt로 생성되게 하시오.

[root@localhost ~]# awk '$3 == "MANAGER" {print $2, $6, $5}' emp.txt | sort -nrk 2 >> result49.txt
[root@localhost ~]# cat result49.txt

 

반응형

'Study > class note' 카테고리의 다른 글

리눅스 / 리눅스 명령어3  (0) 2022.03.14
리눅스 / putty로 서버 접속, 리눅스 명령어2  (0) 2022.03.11
리눅스 설치  (0) 2022.03.08
빅분기 실기를 위한 예제  (0) 2022.03.08
R / 소리 시각화  (0) 2022.03.08