<!DOCTYPE html>
    <html lang="vi" xmlns="http://www.w3.org/1999/xhtml" prefix="og: http://ogp.me/ns#">
    <head>
<title>Cách sử dụng lệnh sed trong Linux</title>
<meta name="description" content="Cách sử dụng lệnh sed 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;cach-su-dung-lenh-sed-trong-linux-7842.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="Cách sử dụng lệnh sed 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;cach-su-dung-lenh-sed-trong-linux-7842.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/cach-su-dung-lenh-sed-trong-linux-7842.html">
<link rel="shortcut icon" href="https://nguoicodonvn2008.info/favicon.ico">
<link rel="canonical" href="https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/cach-su-dung-lenh-sed-trong-linux-7842.html">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/" title="Tin Tức" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/karaoke-dual/" title="Tin Tức - Karaoke Dual" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/nhac-tre/" title="Tin Tức - Nhạc trẻ" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/tru-tinh/" title="Tin Tức - Trữ tình" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/nuoc-ngoai/" title="Tin Tức - Nước ngoài" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/remix/" title="Tin Tức - Remix" type="application/rss+xml">
<link rel="alternate" href="https://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://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://nguoicodonvn2008.info/vi/news/rss/cuoc-song/" title="Tin Tức - Cuộc sống" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/phan-mem/" title="Tin Tức - Phần mềm" type="application/rss+xml">
<link rel="alternate" href="https://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://nguoicodonvn2008.info/vi/news/rss/hoc-tap/" title="Tin Tức - Học tập" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/tai-lieu/" title="Tin Tức - Tài liệu" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/de-thi/" title="Tin Tức - Đề thi" type="application/rss+xml">
<link rel="preload" as="style" href="https://nguoicodonvn2008.info/assets/css/font-awesome.min.css" type="text/css">
<link rel="preload" as="style" href="https://nguoicodonvn2008.info/themes/default/css/bootstrap.non-responsive.css" type="text/css">
<link rel="preload" as="style" href="https://nguoicodonvn2008.info/themes/default/css/style.css" type="text/css">
<link rel="preload" as="style" href="https://nguoicodonvn2008.info/themes/default/css/style.non-responsive.css" type="text/css">
<link rel="preload" as="style" href="https://nguoicodonvn2008.info/themes/default/css/news.css" type="text/css">
<link rel="preload" as="style" href="https://nguoicodonvn2008.info/themes/default/css/custom.css" type="text/css">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/assets/js/jquery/jquery.min.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/assets/js/language/vi.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/assets/js/DOMPurify/purify3.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/assets/js/global.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/assets/js/site.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/themes/default/js/news.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/themes/default/js/main.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/themes/default/js/custom.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/themes/default/js/bootstrap.min.js" type="text/javascript">
<link rel="stylesheet" href="https://nguoicodonvn2008.info/assets/css/font-awesome.min.css">
<link rel="stylesheet" href="https://nguoicodonvn2008.info/themes/default/css/bootstrap.non-responsive.css">
<link rel="stylesheet" href="https://nguoicodonvn2008.info/themes/default/css/style.css">
<link rel="stylesheet" href="https://nguoicodonvn2008.info/themes/default/css/style.non-responsive.css">
<link rel="StyleSheet" href="https://nguoicodonvn2008.info/themes/default/css/news.css">
<link rel="stylesheet" href="https://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://nguoicodonvn2008.info/">https://nguoicodonvn2008.info</a></p>
	</div>
	<div class="clear"></div>
	<hr />
	<div id="content">
		<h1>Cách sử dụng lệnh sed trong Linux</h1>
		<ul class="list-inline">
			<li>Thứ hai - 11/12/2023 09:24</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 class="imghome">
			<img alt="Cách sử dụng lệnh sed trong Linux" src="https://st.quantrimang.com/photos/image/2018/04/05/cai-Ubuntu-tren-Windows-logo200-size-80x80-znd.png" width="460" class="img-thumbnail" />
		</div>
		<div class="clear"></div>
		<div id="bodytext" class="clearfix">
			<p>Sed là viết tắt của “Stream Editor” và đây là một tiện ích mạnh mẽ cho phép bạn phân tích cú pháp và chuyển đổi văn bản ngay từ dòng lệnh. Cho dù bạn đang xử lý các file cấu hình, script hay thậm chí là plain text, sed là công cụ hỗ trợ bạn thao tác văn bản nhanh chóng và hiệu quả.</p>

<p>Công dụng chính của sed là tìm kiếm các mẫu văn bản cụ thể và thay thế chúng bằng một nội dung khác. Nó cũng có thể xóa hoặc chèn dòng và thực hiện các chuyển đổi văn bản khác. Nó đặc biệt hữu ích cho việc chỉnh sửa hàng loạt file hoặc làm việc trong các shell script để tự động hóa những tác vụ khác nhau.</p>

<p>Mặc dù bản thân sed cực kỳ linh hoạt nhưng nó thường được sử dụng kết hợp với các lệnh Linux khác như&nbsp;<strong>awk</strong>&nbsp;để xử lý văn bản,&nbsp;<strong>grep</strong>&nbsp;để tìm kiếm mẫu và&nbsp;<strong>cat</strong>&nbsp;để hiển thị nội dung file. Cùng với nhau, những công cụ này tạo thành một bộ công cụ mạnh mẽ để xử lý văn bản trong môi trường&nbsp;Linux.</p>

<p>Cú pháp chung cho lệnh sed:</p>

<pre id="pre0">
<code>$ sed &#91;OPTIONS&#93; &#91;FILE&#93;...</code></pre>

<h2>1. Thay thế văn bản</h2>

<pre id="pre1">
<code>echo &quot;Text&quot; | sed &#039;s/Replaceable_Word/The_Word_That_Replaces/&#039;</code></pre>

<p>Sử dụng lệnh sed để tìm kiếm và thay thế bất kỳ phần nào của văn bản.&nbsp;<strong>&#039;s&#039;</strong>&nbsp;biểu thị tác vụ tìm kiếm và thay thế.</p>

<p>Ví dụ, giả sử bạn có một chuỗi<em>&nbsp;“I love CSS”</em>&nbsp;và bạn muốn thay thế&nbsp;<em>“CSS”</em>&nbsp;bằng&nbsp;<em>“CSS Libraries“.</em></p>

<pre id="pre2">
<code>echo &quot;I love CSS&quot; | sed &#039;s/CSS/CSS Libraries/&#039;</code></pre>

<pre id="pre3">
<code>I love CSS Libraries</code></pre>

<p>Trong ví dụ này, lệnh echo xuất ra&nbsp;<em>“I love CSS“</em>, sau đó sed thay thế&nbsp;<em>“CSS”</em>&nbsp;bằng&nbsp;<em>“CSS Libraries“</em>. Kết quả cuối cùng là&nbsp;<em>“I love CSS Libraries”</em>.</p>

<h2>2. Thay thế văn bản trên một dòng cụ thể trong file</h2>

<pre id="pre4">
<code>sed &#039;&#91;line&#93; s/harder/easier/g&#039; &#91;file&#93;</code></pre>
<iframe allowtransparency="true" data-google-container-id="a!2" data-google-query-id="CIXQ8vvKh4MDFYtRDwId-ZcCnQ" data-load-complete="true" frameborder="0" height="0" hspace="0" id="aswift_1" marginheight="0" marginwidth="0" name="aswift_1" sandbox="" scrolling="no" vspace="0" width="694"></iframe>

<p>Tùy chọn&nbsp;<strong>&#039;g&#039;</strong>&nbsp;của lệnh sed được sử dụng để thay thế bất kỳ thứ gì phù hợp với mẫu.</p>

<p>Ví dụ, giả sử bạn có một file văn bản có tên example.txt với nội dung sau:</p>

<pre id="pre5">
<code>Life is hard.
Working harder is the key to success.
The harder you work, the luckier you get.</code></pre>

<p>Để thay thế tất cả các lần xuất hiện của từ&nbsp;<em>“harder”</em>&nbsp;bằng&nbsp;<em>“easier”</em>&nbsp;trên dòng 2 của example.txt, bạn sẽ chạy:</p>

<pre id="pre6">
<code>sed &#039;2 s/harder/easier/g&#039; example.txt</code></pre>

<p>Sau khi chạy lệnh, kết quả hiển thị trên terminal sẽ là:</p>

<pre id="pre7">
<code>Life is hard.
Working easier is the key to success.
The harder you work, the luckier you get.</code></pre>

<p>Lưu ý rằng từ&nbsp;<em>“harder”</em>&nbsp;chỉ được thay thế bằng&nbsp;<em>“easier”</em>&nbsp;ở dòng 2.</p>

<p>Nếu muốn lưu lại những thay đổi này vào file, bạn có thể sử dụng tùy chọn<strong>&nbsp;-i</strong>:</p>

<pre id="pre8">
<code>sed -i &#039;2 s/harder/easier/g&#039; example.txt</code></pre>

<p>Sau khi chạy lệnh này, nội dung của example.txt sẽ được thay đổi vĩnh viễn thành:</p>

<pre id="pre9">
<code>Life is hard.
Working easier is the key to success.
The harder you work, the luckier you get.</code></pre>

<h2>3. Thay thế kết quả khớp đầu tiên bằng văn bản mới</h2>

<pre id="pre10">
<code>sed &#039;s/harder/easier/&#039; &#91;file&#93;</code></pre>

<p>Lệnh này chỉ thay thế kết quả khớp đầu tiên của mẫu tìm kiếm.</p>

<p>Ví dụ, giả sử bạn có một file văn bản có tên example.txt với nội dung sau:</p>

<pre id="pre11">
<code>Life is harder than we think.
Working harder is the key to success.
No pain, no gain. Work harder.</code></pre>

<p>Bạn có thể sử dụng lệnh sed để thay thế từ&nbsp;<em>“harder”</em>&nbsp;bằng&nbsp;<em>“easier”</em>&nbsp;ở mỗi dòng:</p>

<pre id="pre12">
<code>sed &#039;s/harder/easier/&#039; example.txt</code></pre>

<p>Sau khi chạy lệnh, kết quả sẽ là:</p>

<pre id="pre13">
<code>Life is easier than we think.
Working easier is the key to success.
No pain, no gain. Work easier.</code></pre>

<h2>4. Xóa các dòng trùng khớp</h2>

<pre id="pre14">
<code>sed &#039;/Something/d&#039; example.txt</code></pre>

<p>Sử dụng tùy chọn&nbsp;<strong>d</strong>&nbsp;của lệnh sed để xóa bất kỳ dòng nào khỏi file.</p>

<p>Ví dụ, giả sử bạn có một file có tên example.txt với nội dung sau:</p>

<pre id="pre15">
<code>Hello World
Something is here
Another line
Yet another line
Something else</code></pre>

<p>Chạy lệnh&nbsp;<code>sed &#039;/Something/d&#039; example.txt</code>&nbsp;sẽ xuất ra:</p>

<pre id="pre16">
<code>Hello World
Another line
Yet another line</code></pre>

<h2>5. Tìm kiếm từ không phân biệt chữ hoa chữ thường + xóa nó</h2>

<pre id="pre17">
<code>sed &#039;/Sample/Id&#039; example.txt</code></pre>

<p>Tùy chọn<strong>&nbsp;I</strong>&nbsp;của lệnh sed được sử dụng để tìm kiếm mẫu phù hợp theo cách không phân biệt chữ hoa chữ thường.</p>
<iframe allowtransparency="true" data-google-container-id="a!3" data-google-query-id="CKaEp4fLh4MDFTkDtwAdbA8KXw" data-load-complete="true" frameborder="0" height="0" hspace="0" id="aswift_2" marginheight="0" marginwidth="0" name="aswift_2" sandbox="" scrolling="no" vspace="0" width="694"></iframe>

<p>Ví dụ, giả sử bạn có một file có tên exampleb.txt với nội dung sau:</p>

<pre id="pre18">
<code>This is a Sample line.
Another line.
Yet another Sample line.
Final line.</code></pre>

<p>Chạy lệnh&nbsp;<code>sed &#039;/Sample/Id&#039; example.txt</code>&nbsp;sẽ tạo ra kết quả đầu ra sau:</p>

<pre id="pre19">
<code>Another line.
Final line.</code></pre>

<h2>6. Thay thế các từ bằng chữ viết hoa</h2>

<pre id="pre20">
<code>sed &#039;s/\(libraries\)/\U\1/Ig&#039; example.txt</code></pre>

<p>Sử dụng tùy chọn<strong>&nbsp;U</strong>&nbsp;của lệnh sed để chuyển đổi bất kỳ văn bản nào thành chữ in hoa.</p>

<p>Ví dụ, giả sử bạn có một file có tên example.txt với nội dung sau:</p>

<pre id="pre21">
<code>I love libraries.
libraries are great.
You can find many books in libraries.</code></pre>

<p>Sau khi chạy lệnh sed, kết quả sẽ là:</p>

<pre id="pre22">
<code>I love LIBRARIES.
LIBRARIES are great.
You can find many books in LIBRARIES.</code></pre>

<h2>7. Thay thế các từ bằng chữ viết thường</h2>

<pre id="pre23">
<code>sed &#039;s/\(libraries\)/\L\1/Ig&#039; example.txt</code></pre>

<p>Tùy chọn<strong>&nbsp;L</strong>&nbsp;của lệnh sed được sử dụng để chuyển đổi bất kỳ văn bản nào thành chữ thường.</p>

<p>Ví dụ, giả sử bạn có một file có tên example.txt với nội dung sau:</p>

<pre id="pre24">
<code>Libraries are essential for research.
libraries help in many ways.
I love LIBRARIES!</code></pre>

<p>Sau khi chạy lệnh sed, kết quả sẽ là:</p>

<pre id="pre25">
<code>libraries are essential for research.
libraries help in many ways.
I love libraries!</code></pre>

<h2>8. Chèn dòng trống vào file</h2>

<pre id="pre26">
<code>sed G &#91;file&#93;</code></pre>

<p>Sử dụng tùy chọn&nbsp;<strong>G</strong>&nbsp;của lệnh sed để chèn dòng trống sau mỗi dòng của file.</p>

<p>Ví dụ, giả sử bạn có một file có tên example.txt với nội dung sau:</p>

<pre id="pre27">
<code>Hello
World
This
Is
A
Test</code></pre>

<p>Bạn có thể chạy lệnh sau để thêm một dòng mới vào cuối mỗi dòng:</p>

<pre id="pre28">
<code>sed G example.txt</code></pre>

<p>Sau khi chạy lệnh, kết quả sẽ là:</p>

<pre id="pre29">
<code>Hello

World

This

Is

A

Test</code></pre>

<h2>9. In số dòng của file</h2>

<pre id="pre30">
<code>sed &#039;=&#039; &#91;file&#93;</code></pre>

<p>Dấu&nbsp;<strong>=&nbsp;</strong>được sử dụng để in số dòng trước mỗi dòng văn bản trong một file.</p>

<p>Ví dụ, giả sử bạn có một file có tên example.txt với nội dung sau:</p>

<pre id="pre31">
<code>Hello
World
This
Is
A
Test</code></pre>

<p>Bạn có thể chạy lệnh sau để in số dòng trước mỗi dòng:</p>

<pre id="pre32">
<code>sed &#039;=&#039; example.txt</code></pre>

<pre id="pre33">
<code>1
Hello
2
World
3
This
4
Is
5
A
6
Test</code></pre>
		</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/cach-su-dung-lenh-sed-trong-linux-7842.html" title="Cách sử dụng lệnh sed trong Linux">https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/cach-su-dung-lenh-sed-trong-linux-7842.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://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://nguoicodonvn2008.info/themes/default/images/car.png" alt=""></span>
</div>
<script src="https://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://nguoicodonvn2008.info/assets/js/language/vi.js"></script>
<script src="https://nguoicodonvn2008.info/assets/js/DOMPurify/purify3.js"></script>
<script src="https://nguoicodonvn2008.info/assets/js/global.js"></script>
<script src="https://nguoicodonvn2008.info/assets/js/site.js"></script>
<script src="https://nguoicodonvn2008.info/themes/default/js/news.js"></script>
<script src="https://nguoicodonvn2008.info/themes/default/js/main.js"></script>
<script src="https://nguoicodonvn2008.info/themes/default/js/custom.js"></script>
<script type="application/ld+json">
        {
            "@context": "https://schema.org",
            "@type": "Organization",
            "url": "https://nguoicodonvn2008.info",
            "logo": "https://nguoicodonvn2008.info/uploads/angel.gif"
        }
        </script>
<script src="https://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>