"리눅스 파일 특정 행 보기"의 두 판 사이의 차이

40번째 줄: 40번째 줄:


==방법 2: head & tail==
==방법 2: head & tail==
{{소스헤더|42번행}}
{{소스헤더|10번행}}
<source lang='bash'>
<source lang='bash'>
출력명령어 | head -42 | tail -1
출력명령어 | head -10 | tail -1
</source>
</source>
<source lang='bash'>
<source lang='bash'>
head -42 파일명 | tail -1
head -10 파일명 | tail -1
</source>
</source>
<source lang='console'>
<source lang='console'>
root@localhost:~# for i in {1..100}; do echo "i = $i"; done | head -42 | tail -1
root@localhost:~# for i in {1..100}; do echo "i = $i"; done | head -10 | tail -1
i = 42
i = 10
root@localhost:~# cat /etc/passwd | head -6 | tail -1
root@localhost:~# cat /etc/passwd | head -10 | tail -1
games:x:5:60:games:/usr/games:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
root@localhost:~# head -6 /etc/passwd | tail -1
root@localhost:~# head -10 /etc/passwd | tail -1
games:x:5:60:games:/usr/games:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
</source>
</source>


64번째 줄: 64번째 줄:
</source>
</source>
<source lang='console'>
<source lang='console'>
root@localhost:~# N=6
root@localhost:~# N=10
root@localhost:~# for i in {1..100}; do echo "i = $i"; done | head -$N | tail -1
root@localhost:~# for i in {1..100}; do echo "i = $i"; done | head -$N | tail -1
i = 6
i = 10
root@localhost:~# cat /etc/passwd | head -$N | tail -1
root@localhost:~# cat /etc/passwd | head -$N | tail -1
games:x:5:60:games:/usr/games:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
root@localhost:~# head -"$N" /etc/passwd | tail -1
root@localhost:~# head -"$N" /etc/passwd | tail -1
games:x:5:60:games:/usr/games:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
</source>
</source>



2020년 2월 7일 (금) 19:01 판

리눅스 파일 특정행 보기, N행 보기
리눅스 파일에서 N번째 행 출력
리눅스 파일에서 N번째 행 보여주기
리눅스 파일에서 N번째 행 가져오기
리눅스 N번째 행 grep[1]

1 방법 1: sed

10번행
출력명령어 | sed -n 10p
sed -n 10p < 파일명
root@localhost:~# for i in {1..100}; do echo "i = $i"; done | sed -n 10p
i = 10
root@localhost:~# cat /etc/passwd | sed -n 10p
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
root@localhost:~# sed -n 10p < /etc/passwd
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
변수 N번행
출력명령어 | sed -n "$N"p
sed -n "$N"p < 파일명
root@localhost:~# N=10
root@localhost:~# for i in {1..100}; do echo "i = $i"; done | sed -n "$N"p
i = 10
root@localhost:~# cat /etc/passwd | sed -n "$N"p
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
root@localhost:~# sed -n "$N"p < /etc/passwd
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin

2 방법 2: head & tail

10번행
출력명령어 | head -10 | tail -1
head -10 파일명 | tail -1
root@localhost:~# for i in {1..100}; do echo "i = $i"; done | head -10 | tail -1
i = 10
root@localhost:~# cat /etc/passwd | head -10 | tail -1
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
root@localhost:~# head -10 /etc/passwd | tail -1
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
변수 N번행
출력명령어 | head -$N | tail -1
head -$N 파일명 | tail -1
root@localhost:~# N=10
root@localhost:~# for i in {1..100}; do echo "i = $i"; done | head -$N | tail -1
i = 10
root@localhost:~# cat /etc/passwd | head -$N | tail -1
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
root@localhost:~# head -"$N" /etc/passwd | tail -1
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin

3 같이 보기

4 참고

  1. 의미상 grep이긴 한데, grep 명령어를 사용하는 것은 아님
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}