<!DOCTYPE html>
    <html lang="vi" xmlns="http://www.w3.org/1999/xhtml" prefix="og: http://ogp.me/ns#">
    <head>
<title>10 ví dụ về lệnh Grep trong Linux</title>
<meta name="description" content="10 ví dụ về lệnh Grep trong Linux - Savefile - Tin Tức - https&#x3A;&#x002F;&#x002F;www.nguoicodonvn2008.info&#x002F;vi&#x002F;news&#x002F;savefile&#x002F;kien-thuc-may-tinh&#x002F;10-vi-du-ve-lenh-grep-trong-linux-3883.html">
<meta name="author" content=".: Nguoicodonvn2008.info - Cõi lòng người cô đơn :.">
<meta name="copyright" content=".: Nguoicodonvn2008.info - Cõi lòng người cô đơn :. [admin@nguoicodonvn2008.info]">
<meta name="robots" content="index, archive, follow, noodp">
<meta name="googlebot" content="index,archive,follow,noodp">
<meta name="msnbot" content="all,index,follow">
<meta name="generator" content="NukeViet v4.5">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta property="og:title" content="10 ví dụ về lệnh Grep trong Linux">
<meta property="og:type" content="website">
<meta property="og:description" content="Savefile - Tin Tức - https&#x3A;&#x002F;&#x002F;www.nguoicodonvn2008.info&#x002F;vi&#x002F;news&#x002F;savefile&#x002F;kien-thuc-may-tinh&#x002F;10-vi-du-ve-lenh-grep-trong-linux-3883.html">
<meta property="og:site_name" content=".&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;.">
<meta property="og:url" content="https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/10-vi-du-ve-lenh-grep-trong-linux-3883.html">
<link rel="shortcut icon" href="https://www.nguoicodonvn2008.info/favicon.ico">
<link rel="canonical" href="https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/10-vi-du-ve-lenh-grep-trong-linux-3883.html">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/" title="Tin Tức" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/karaoke-dual/" title="Tin Tức - Karaoke Dual" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/nhac-tre/" title="Tin Tức - Nhạc trẻ" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/tru-tinh/" title="Tin Tức - Trữ tình" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/nuoc-ngoai/" title="Tin Tức - Nước ngoài" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/remix/" title="Tin Tức - Remix" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/tam-su-tinh-yeu/" title="Tin Tức - Tâm sự tình yêu" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/tho-suu-tam/" title="Tin Tức - Thơ sưu tầm" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/cuoc-song/" title="Tin Tức - Cuộc sống" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/phan-mem/" title="Tin Tức - Phần mềm" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/kien-thuc-may-tinh/" title="Tin Tức - Kiến thức máy tính" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/hoc-tap/" title="Tin Tức - Học tập" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/tai-lieu/" title="Tin Tức - Tài liệu" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/de-thi/" title="Tin Tức - Đề thi" type="application/rss+xml">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/assets/css/font-awesome.min.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/bootstrap.non-responsive.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/style.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/style.non-responsive.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/news.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/custom.css" type="text/css">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/jquery/jquery.min.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/language/vi.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/DOMPurify/purify3.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/global.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/site.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/themes/default/js/news.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/themes/default/js/main.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/themes/default/js/custom.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/themes/default/js/bootstrap.min.js" type="text/javascript">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/assets/css/font-awesome.min.css">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/themes/default/css/bootstrap.non-responsive.css">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/themes/default/css/style.css">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/themes/default/css/style.non-responsive.css">
<link rel="StyleSheet" href="https://www.nguoicodonvn2008.info/themes/default/css/news.css">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/themes/default/css/custom.css">
<style type="text/css">
	body{background: #fff;}
</style>
    </head>
    <body>
<div id="print">
	<div id="hd_print">
		<h2 class="pull-left">.&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;.</h2>
		<p class="pull-right"><a title=".&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;." href="https://www.nguoicodonvn2008.info/">https://www.nguoicodonvn2008.info</a></p>
	</div>
	<div class="clear"></div>
	<hr />
	<div id="content">
		<h1>10 ví dụ về lệnh Grep trong Linux</h1>
		<ul class="list-inline">
			<li>Thứ bảy - 03/04/2021 00:59</li>
			<li class="hidden-print txtrequired"><em class="fa fa-print">&nbsp;</em><a title="In ra" href="javascript:;" onclick="window.print()">In ra</a></li>
			<li class="hidden-print txtrequired"><em class="fa fa-power-off">&nbsp;</em><a title="Đóng cửa sổ này" href="javascript:;" onclick="window.close()">Đóng cửa sổ này</a></li>
		</ul>
		<div class="clear"></div>
		<div id="hometext">
		</div>
		<div id="bodytext" class="clearfix">
			<p style="text-align: justify;">Lệnh grep cung cấp quyền truy cập vào tiện ích grep, một công cụ xử lý file mạnh mẽ được sử dụng để tìm các mẫu trong file văn bản. Lệnh này có nhiều trường hợp sử dụng thực tế và chắc chắn là một trong&nbsp;những lệnh Linux được sử dụng nhiều nhất. Hướng dẫn này minh họa một số lệnh grep Linux đơn giản nhưng hữu ích được sử dụng trong thực tế.</p>

<h2 style="text-align: justify;">File ví dụ dùng cho việc minh họa</h2>

<p style="text-align: justify;">Bài viết đã tạo một file tham khảo để giúp người đọc hiểu về grep hiệu quả hơn. Bạn có thể tạo một bản sao của file này bằng cách đưa ra lệnh shell sau trong Terminal.</p>

<pre id="pre0">
<code>cat &lt;&lt;END &gt;&gt; test-file
This is a simple text file that contains
multiple strings as well as some telephone numbers
(555) 555-1234 (567) 666-2345
and email plus web addresses
john@doe.com
https://google.com
ftp://mywebserver.com
END</code></pre>

<h2 style="text-align: justify;">1. Tìm văn bản trong file</h2>

<p style="text-align: justify;">Để tìm kiếm mẫu văn bản trong file, chỉ cần chạy grep theo sau là tên mẫu. Ngoài ra, hãy chỉ định tên của file chứa văn bản.</p>

<pre id="pre1">
<code>grep &quot;email&quot; test-file</code></pre>

<p style="text-align: justify;">Lệnh này sẽ hiển thị dòng trong&nbsp;<strong>test-file</strong>&nbsp;có chứa từ&nbsp;<strong>email</strong>. Bạn cũng có thể tìm kiếm cùng một văn bản trong nhiều file bằng grep.</p>

<pre id="pre2">
<code>grep &quot;example&quot; /usr/share/dict/american-english /usr/share/dict/british-english</code></pre>

<p style="text-align: justify;">Lệnh trên hiển thị tất cả mọi trường hợp của từ&nbsp;<strong>example</strong>&nbsp;trong các file từ điển&nbsp;<strong>american-english</strong>&nbsp;và&nbsp;<strong>british-english.</strong></p>

<h2 style="text-align: justify;">2. Tìm các từ khớp hoàn toàn</h2>

<p style="text-align: justify;">Lệnh grep của&nbsp;Linux&nbsp;được minh họa trong ví dụ trước đó cũng liệt kê các dòng có khớp một phần. Sử dụng lệnh dưới đây nếu bạn chỉ cần sự xuất hiện của một từ khớp hoàn toàn.</p>

<pre id="pre3">
<code>grep -w &quot;string&quot; test-file</code></pre>

<p style="text-align: justify;">Tùy chọn&nbsp;<strong>-w</strong>&nbsp;hoặc&nbsp;<strong>--word-regexp</strong>&nbsp;của grep chỉ giới hạn đầu ra đối với các kết quả khớp hoàn toàn. Grep bao gồm một số flag bổ sung có thể được sử dụng với lệnh mặc định.</p>

<h2 style="text-align: justify;">3. Bỏ qua phân biệt chữ hoa, chữ thường</h2>

<p style="text-align: justify;">Theo mặc định, grep tìm kiếm các mẫu có phân biệt chữ hoa chữ thường. Tuy nhiên, bạn có thể muốn tắt tính năng này nếu không biết trước mẫu đó thuộc trường hợp nào.</p>

<pre id="pre4">
<code>grep -i &quot;this&quot; test-file</code></pre>

<p style="text-align: justify;">Sử dụng tùy chọn&nbsp;<strong>-i</strong>&nbsp;hoặc&nbsp;<strong>--ignore-case</strong>&nbsp;để tắt tính năng phân biệt chữ hoa chữ thường.</p>

<h2 style="text-align: justify;">4. Đếm số lượng mẫu</h2>

<p style="text-align: justify;">Flag&nbsp;<strong>-c</strong>&nbsp;là viết tắt của&nbsp;<strong>count</strong>. Nó hiển thị số lượng kết quả phù hợp đã được tìm thấy cho một mẫu cụ thể. Quản trị viên có thể sử dụng điều này để truy xuất thông tin cụ thể về hệ thống.</p>

<p style="text-align: justify;">Bạn có thể pipe lệnh ps với grep để đếm các tiến trình thuộc về người dùng hiện tại.</p>

<pre id="pre5">
<code>ps -ef | grep -c $USER</code></pre>

<p style="text-align: justify;">Lệnh sau đây hiển thị số lượng&nbsp;file MP3&nbsp;có trong một thư mục.</p>

<pre id="pre6">
<code>ls ~/Music | grep -c .mp3</code></pre>

<h2 style="text-align: justify;">5. Hiển thị số dòng chứa các kết quả khớp</h2>

<p style="text-align: justify;">Bạn có thể muốn tìm số dòng chứa một kết quả khớp nhất định. Sử dụng tùy chọn&nbsp;<strong>-n</strong>&nbsp;hoặc&nbsp;<strong>--line-number</strong>&nbsp;của grep để đạt được điều này.</p>

<pre id="pre7">
<code>cat /etc/passwd | grep -n rubaiat</code></pre>

<p style="text-align: justify;">Tùy chọn này đặc biệt hữu ích để gỡ lỗi mã nguồn và khắc phục sự cố file nhật ký. Để hiển thị tất cả số cho các dòng trong&nbsp;<strong>~/.vimrc</strong>&nbsp;được sử dụng để cấu hình&nbsp;trình soạn thảo văn bản Vim:</p>

<pre id="pre8">
<code>grep -n &quot;set&quot; ~/.vimrc</code></pre>

<div style="text-align: justify;">&nbsp;</div>

<h2 style="text-align: justify;">6. Tìm tên file bằng tiện ích mở rộng</h2>

<p style="text-align: justify;">Để có danh sách tất cả các file MP3 trong thư mục&nbsp;<strong>~/Music</strong>:</p>

<pre id="pre9">
<code>ls ~/Music/ | grep &quot;.mp3&quot;</code></pre>

<p style="text-align: justify;">Bạn có thể thay thế .mp3 bằng bất kỳ phần mở rộng nào khác để định vị các file cụ thể. Lệnh sau liệt kê tất cả các file php có trong thư mục hiện hành.</p>

<pre id="pre10">
<code>ls | grep &quot;.php&quot;</code></pre>

<h2 style="text-align: justify;">7. Tìm mẫu trong file nén</h2>

<p style="text-align: justify;">Lệnh grep của Linux cũng có thể tìm thấy các mẫu bên trong file nén. Tuy nhiên, bạn sẽ cần sử dụng lệnh zgrep để thực hiện việc này. Trước tiên, hãy tạo một kho lưu trữ nén của&nbsp;<strong>test-file</strong>&nbsp;bằng cách nhập:</p>

<pre id="pre11">
<code>gzip test-file</code></pre>

<p style="text-align: justify;">Bây giờ, bạn có thể tìm kiếm văn bản hoặc các mẫu khác bên trong kho lưu trữ kết quả.</p>

<pre id="pre12">
<code>zgrep email test-file.gz</code></pre>

<h2 style="text-align: justify;">8. Tìm địa chỉ email</h2>

<p style="text-align: justify;">Quản trị viên cũng có thể liệt kê các địa chỉ email từ những file văn bản bằng lệnh grep Linux. Ví dụ sau thực hiện điều này bằng cách tìm kiếm một mẫu biểu thức chính quy (regular expression).</p>

<pre id="pre13">
<code>grep &#039;^&#91;a-zA-Z0-9&#93;\+@&#91;a-zA-Z0-9&#93;\+\.&#91;a-z&#93;\{2,\}&#039; test-file</code></pre>

<p style="text-align: justify;">Bạn có thể tìm thấy các biểu thức chính quy để thực hiện những công việc tương tự hoặc tự tạo nếu bạn biết cách chúng hoạt động.</p>

<h2 style="text-align: justify;">9. Tìm số điện thoại bằng Grep</h2>

<p style="text-align: justify;">Bạn có thể sử dụng biểu thức chính quy grep để lọc số điện thoại từ file văn bản. Lưu ý rằng bạn phải điều chỉnh mẫu để phù hợp với loại số điện thoại bạn cần.</p>

<pre id="pre14">
<code>grep &#039;\((&#91;0-9&#93;\{3\})\|&#91;0-9&#93;\{3\}\)&#91; -&#93;\?&#91;0-9&#93;\{3\}&#91; -&#93;\?&#91;0-9&#93;\{4\}&#039; test-file</code></pre>

<p style="text-align: justify;">Lệnh nói trên lọc ra các số điện thoại Mỹ gồm 10 chữ số.</p>

<h2 style="text-align: justify;">10. Tìm URL từ file nguồn</h2>

<p style="text-align: justify;">Bạn có thể tận dụng sức mạnh của grep để liệt kê các&nbsp;URL&nbsp;được tìm thấy trong file văn bản. Lệnh dưới đây sẽ in tất cả các URL có trong&nbsp;<strong>test-file</strong>.</p>

<pre id="pre15">
<code>grep -E &quot;^(http|https|ftp):&#91;\/&#93;{2}(&#91;a-zA-Z0-9\-\.&#93;+\.&#91;a-zA-Z&#93;{2,4})&quot; test-file</code></pre>

<p style="text-align: justify;">Ví dụ sẽ lại sử dụng tùy chọn&nbsp;<strong>-E</strong>&nbsp;cho các biểu thức chính quy mở rộng. Bạn cũng có thể sử dụng lệnh egrep để tránh thêm điều này.</p>

<pre id="pre16">
<code>egrep &quot;^(http|https|ftp):&#91;\/&#93;{2}(&#91;a-zA-Z0-9\-\.&#93;+\.&#91;a-zA-Z&#93;{2,4})&quot; test-file</code></pre>

<p style="text-align: justify;">Trên đây là một số ví dụ thực tiễn về cách sử dụng lệnh grep trong Linux.</p>

<p style="text-align: justify;">Chúc bạn áp dụng thành công!</p>
		</div>
				<div id="author">
						<p>
				<strong>Nguồn tin:</strong>
				Quantrimang.com
			</p>
		</div>
	</div>
	<div id="footer" class="clearfix">
		<div id="url">
			<strong>URL của bản tin này: </strong><a href="https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/10-vi-du-ve-lenh-grep-trong-linux-3883.html" title="10 ví dụ về lệnh Grep trong Linux">https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/10-vi-du-ve-lenh-grep-trong-linux-3883.html</a>

		</div>
		<div class="clear"></div>
		<div class="copyright">
			&copy; .&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;.
		</div>
		<div id="contact">
			<a href="mailto:admin@nguoicodonvn2008.info">admin@nguoicodonvn2008.info</a>
		</div>
	</div>
</div>
        <div id="timeoutsess" class="chromeframe">
            Bạn đã không sử dụng Site, <a onclick="timeoutsesscancel();" href="https://www.nguoicodonvn2008.info/#">Bấm vào đây để duy trì trạng thái đăng nhập</a>. Thời gian chờ: <span id="secField"> 60 </span> giây
        </div>
        <div id="openidResult" class="nv-alert" style="display:none"></div>
        <div id="openidBt" data-result="" data-redirect=""></div>
		</script>
		<div class="car-top">
  <span><img src="https://www.nguoicodonvn2008.info/themes/default/images/car.png" alt=""></span>
</div>
<script src="https://www.nguoicodonvn2008.info/assets/js/jquery/jquery.min.js"></script>
<script>var nv_base_siteurl="/",nv_lang_data="vi",nv_lang_interface="vi",nv_name_variable="nv",nv_fc_variable="op",nv_lang_variable="language",nv_module_name="news",nv_func_name="savefile",nv_is_user=0, nv_my_ofs=-4,nv_my_abbr="EDT",nv_cookie_prefix="nv4c_e856T",nv_check_pass_mstime=1738000,nv_area_admin=0,nv_safemode=0,theme_responsive=0,nv_recaptcha_ver=2,nv_recaptcha_sitekey="",nv_recaptcha_type="image",XSSsanitize=1;</script>
<script src="https://www.nguoicodonvn2008.info/assets/js/language/vi.js"></script>
<script src="https://www.nguoicodonvn2008.info/assets/js/DOMPurify/purify3.js"></script>
<script src="https://www.nguoicodonvn2008.info/assets/js/global.js"></script>
<script src="https://www.nguoicodonvn2008.info/assets/js/site.js"></script>
<script src="https://www.nguoicodonvn2008.info/themes/default/js/news.js"></script>
<script src="https://www.nguoicodonvn2008.info/themes/default/js/main.js"></script>
<script src="https://www.nguoicodonvn2008.info/themes/default/js/custom.js"></script>
<script type="application/ld+json">
        {
            "@context": "https://schema.org",
            "@type": "Organization",
            "url": "https://www.nguoicodonvn2008.info",
            "logo": "https://www.nguoicodonvn2008.info/uploads/angel.gif"
        }
        </script>
<script src="https://www.nguoicodonvn2008.info/themes/default/js/bootstrap.min.js"></script>
<script type="text/javascript">
var $scrolltop = $('.car-top');
$scrolltop.on('click', function () {
    $('html,body').animate({
        scrollTop: 0
    }, 800);
    $(this).addClass("car-run");
    setTimeout(function(){ $scrolltop.removeClass('car-run');}, 1000);
    return false;
});
$(window).on('scroll', function ()
{ 
    if($(window).scrollTop() >= 200)
    {
        $scrolltop.addClass("show");
        $scrolltop.addClass("car-down");
    }
    else
    {
       $scrolltop.removeClass("show");
       setTimeout(function(){ $scrolltop.removeClass('car-down');}, 300);
    }
});
</script>
</body>
</html>