"리눅스 문자열 매칭 행번호 확인"의 두 판 사이의 차이

 
(사용자 4명의 중간 판 25개는 보이지 않습니다)
1번째 줄: 1번째 줄:
;match string and get line number
;match string and get line number
;grep 행번호 확인
;리눅스 문자열 매칭 행번호 확인
;get line number of first matching
;get line number of first matching
;리눅스 첫번째 매칭 행번호 확인
;리눅스 첫번째 매칭 행번호 확인
;get line number of last matching
;get line number of last matching
;리눅스 마지막 매칭 행번호 확인
;리눅스 마지막 매칭 행번호 확인
==여러 행==
{{소스헤더|명령어}}
<syntaxhighlight lang='bash'>
grep -n 문자열 파일
grep -n 문자열 파일 | cut -d: -f1
grep -n 문자열 파일 | awk '{print $1}' FS=":"
</syntaxhighlight>
{{소스헤더|실행예시}}
<syntaxhighlight lang='console'>
[root@zetawiki ~]# grep -n logvol /root/anaconda-ks.cfg
22:#logvol /home --fstype=ext4 --name=lv_home --vgname=VolGroup --grow --size=100
23:#logvol / --fstype=ext4 --name=lv_root --vgname=VolGroup --grow --size=1024 --maxsize=51200
24:#logvol swap --name=lv_swap --vgname=VolGroup --grow --size=1024 --maxsize=6064
</syntaxhighlight>
<syntaxhighlight lang='console'>
[root@zetawiki ~]# grep -n logvol /root/anaconda-ks.cfg | cut -d: -f1
22
23
24
</syntaxhighlight>


==첫번째 매칭==
==첫번째 매칭==
<source lang='bash'>
<syntaxhighlight lang='console'>
grep -n 문자열 파일 | head -1 | cut -d ":" -f 1
[root@zetawiki ~]# grep -n logvol /root/anaconda-ks.cfg | cut -d: -f1 | head -1
</source>
22
</syntaxhighlight>


==마지막 매칭==
==마지막 매칭==
<source lang='bash'>
<syntaxhighlight lang='console'>
grep -n 문자열 파일 | tail -1 | cut -d ":" -f 1
[root@zetawiki ~]# grep -n logvol /root/anaconda-ks.cfg | cut -d: -f1 | tail -1
</source>
24
</syntaxhighlight>


==같이 보기==
==같이 보기==
*[[리눅스 파일 N번째 행 보기]]
*[[리눅스 파일 N번째 행 보기]]
*[[grep 구간 보기]]
*[[grep 파일명 보기]]
*[[리눅스 cat 행번호 보기]]
*[[행 번호]]


==참고 자료==
==참고==
*http://stackoverflow.com/questions/7103531/how-to-get-the-part-of-file-after-the-line-that-matches-grep-expression-first
*http://stackoverflow.com/questions/7103531/how-to-get-the-part-of-file-after-the-line-that-matches-grep-expression-first


[[분류: 리눅스]]
[[분류: 리눅스]]
[[분류: grep]]

2020년 11월 27일 (금) 15:43 기준 최신판

match string and get line number
grep 행번호 확인
리눅스 문자열 매칭 행번호 확인
get line number of first matching
리눅스 첫번째 매칭 행번호 확인
get line number of last matching
리눅스 마지막 매칭 행번호 확인

1 여러 행[ | ]

명령어
grep -n 문자열 파일
grep -n 문자열 파일 | cut -d: -f1
grep -n 문자열 파일 | awk '{print $1}' FS=":"
실행예시
[root@zetawiki ~]# grep -n logvol /root/anaconda-ks.cfg
22:#logvol /home --fstype=ext4 --name=lv_home --vgname=VolGroup --grow --size=100
23:#logvol / --fstype=ext4 --name=lv_root --vgname=VolGroup --grow --size=1024 --maxsize=51200
24:#logvol swap --name=lv_swap --vgname=VolGroup --grow --size=1024 --maxsize=6064
[root@zetawiki ~]# grep -n logvol /root/anaconda-ks.cfg | cut -d: -f1
22
23
24

2 첫번째 매칭[ | ]

[root@zetawiki ~]# grep -n logvol /root/anaconda-ks.cfg | cut -d: -f1 | head -1
22

3 마지막 매칭[ | ]

[root@zetawiki ~]# grep -n logvol /root/anaconda-ks.cfg | cut -d: -f1 | tail -1
24

4 같이 보기[ | ]

5 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}