English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The Linux sed command is used to process text files with scripts.
Sed can process and edit text files according to the instructions of the script.
Sed is mainly used to automatically edit one or more files, simplify repeated operations on files, and write conversion programs, etc.
sed [-hnV][-e<script>][-f<script file>][text file]
Parameter description:
-e<script> or --expression=<script> Use the script specified in the option to process the input text file.
-f<script file> or --file=<script file> Use the script file specified in the option to process the input text file.
-h or --help Display help.
-n or --quiet or --silent Only display the results after script processing.
-V or --version Display version information.
Action description:
a : Add, the string after a can be connected, and these strings will appear in a new line (the next line of the current)~
c : Replace, the string after c can be connected, and these strings can replace the lines between n1 and n2!
d : Delete, because it is to delete, so nothing is usually connected after d;
i : Insert, the string after i can be connected, and these strings will appear in a new line (the previous line of the current);
p : Print, that is, print out some selected data. Usually p runs with the parameter sed -n~
s : Replace, you can directly perform the replacement work! Usually this s action can be combined with regular expressions! For example, 1,20s/old/new/g is it!
To add a line after the fourth line of the testfile file and output the result to standard output, enter the following command at the command prompt:
sed -e 4a\newLine testfile
First, view the content of testfile as follows:
$ cat testfile #View the content of testfile HELLO LINUX! Linux is a free unix-type operating system. This is a linux testfile! Linux test
The output result after using the sed command is as follows:
$ sed -e 4a\newline testfile #Use sed to add a new string after the fourth line HELLO LINUX! #testfile file original content Linux is a free unix-type operating system. This is a linux testfile! Linux test newline
محتوای /etc/passwd را لیست کرده و اعداد سطر را چاپ کنید و سطرهای 2 تا 5 را حذف کنید!
[root@www ~]# nl /etc/passwd | sed '2,5d' 1 root:x:0:0:root:/root:/bin/bash 6 sync:x:5:0:sync:/sbin:/bin/sync 7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown .....(بعد از این حذف شده).....
عملکرد sed '2,5d' است، d به معنای حذف است! زیرا 2-5 سطر برای حذف داده شدهاند، بنابراین دادههای نمایش داده شده شامل سطرهای 2-5 نیستند. همچنین، توجه داشته باشید که باید از -e استفاده کنید، اگر -e نباشد نیز مشکلی ندارد! همچنین توجه داشته باشید که باید عملکرد sed با ' ' دو تایپ اضافه شود!
فقط سطر دوم را حذف کنید
nl /etc/passwd | sed '2d'
برای حذف سطرهای 3 تا آخر
nl /etc/passwd | sed '3,$d'
در پشت سطر دوم (یعنی در سطر سوم) واژه 'نوشیدن چای؟' اضافه شود!
[root@www ~]# nl /etc/passwd | sed '2a نوشیدن چای' 1 root:x:0:0:root:/root:/bin/bash 2 bin:x:1:1:bin:/bin:/sbin/nologin نوشیدن چای 3 daemon:x:2:2:daemon:/sbin:/sbin/nologin .....(بعد از این حذف شده).....
اگر میخواهید در پشت سطر دوم
nl /etc/passwd | sed '2i نوشیدن چای'
اگر میخواهید دو سطر یا بیشتر اضافه کنید، دو سطر را در پشت سطر دوم اضافه کنید، به عنوان مثال نوشیدن چای یا ...... و نوشیدن آبجو؟
[root@www ~]# nl /etc/passwd | sed '2a Drink tea or ......\' > نوشیدن آبجو؟? 1 root:x:0:0:root:/root:/bin/bash 2 bin:x:1:1:bin:/bin:/sbin/nologin نوشیدن چای یا ...... نوشیدن آبجو؟ 3 daemon:x:2:2:daemon:/sbin:/sbin/nologin .....(بعد از این حذف شده).....
هر سطر باید با استفاده از بارکدعکس ' \ ' به سطر جدید اضافه شود! بنابراین، در مثال بالا، میتوانید مشاهده کنید که در انتهای سطر اول بارکدعکس وجود دارد.
آیا میخواهید محتوای سطرهای 2-5 را با 'No 2-5 number' جایگزین کنید؟
[root@www ~]# nl /etc/passwd | sed '2,5c No 2-5 number' 1 root:x:0:0:root:/root:/bin/bash No 2-5 عدد 6 sync:x:5:0:sync:/sbin:/bin/sync .....(بعد از این حذف شده).....
با این روش ما میتوانیم دادهها را به صورت خطی جایگزین کنیم!
فقط سطرهای 5-7 فایل /etc/passwd لیست میشود
[root@www ~]# nl /etc/passwd | sed -n '5,7p' 5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 6 sync:x:5:0:sync:/sbin:/bin/sync 7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
من خلال وظيفة عرض sed بناءً على السطر كوحدة، يمكن اختيار سطور معينة من ملف معين لعرضها.
البحث عن السطور التي تحتوي على كلمة root في /etc/passwd
nl /etc/passwd | sed '/root/p' 1 root:x:0:0:root:/root:/bin/bash 1 root:x:0:0:root:/root:/bin/bash 2 daemon:x:1:1:daemon:/usr/sbin:/bin/sh 3 bin:x:2:2:bin:/bin:/bin/sh 4 sys:x:3:3:sys:/dev:/bin/sh 5 sync:x:4:65534:sync:/bin:/bin/sync ....تخطي
إذا تم العثور على root، بالإضافة إلى طباعة جميع السطور، سيتم أيضًا طباعة السطر المطابق.
عند استخدام -n، سيتم طباعة السطور التي تحتوي على النمط فقط.
nl /etc/passwd | sed -n '/root/p' 1 root:x:0:0:root:/root:/bin/bash
حذف جميع السطور التي تحتوي على root في /etc/passwd، والسطور الأخرى طباعة
nl /etc/passwd | sed '/root/d' 2 daemon:x:1:1:daemon:/usr/sbin:/bin/sh 3 bin:x:2:2:bin:/bin:/bin/sh ....تخطي # تم حذف التطابق في السطر الأول
البحث في /etc/passwd، العثور على السطر الم对应 لـ root، تنفيذ مجموعة الأوامر في الدوامة التالية، كل أوامر منفصلة بالفاصلة، هنا يتم استبدال bash بـ blueshell، ثم طباعة هذا السطر:
nl /etc/passwd | sed -n '/root/{s/bash/blueshell/;p;q}' 1 root:x:0:0:root:/root:/bin/blueshell
آخر q هو الخروج.
بالإضافة إلى نمط معالجة السطر الكامل، يمكن استخدام sed أيضًا كون السطر وحدة للبحث عن بيانات جزئية واستبدالها. أساسًا، البحث والإبدال في sed مشابهان جدًا لـ vi! إنه يشبه هذا:
sed 's/النصُّ الذي سيتم استبداله/النصُّ الجديد/g'
أولاً، نلاحظ المعلومات الأصلية، ونستخدم /sbin/ifconfig لاستعلام IP
[root@www ~]# /sbin/ifconfig eth0 eth0 Link encap:Ethernet HWaddr 00:90:CC:A6:34:84 inet addr:192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: fe80::290:ccff:fea6:3484/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 .....(يتم التبسيط).....
عنوان IP الخاص بالجهاز هو 192.168.1.100.
إزالة الجزء الأول من عنوان IP
[root@www ~]# /sbin/ifconfig eth0 | grep 'inet addr' | sed 's/^.*addr://g' 192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0
ثم يتم إزالة الجزء التالي أيضًا، وهو: 192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0
إزالة الجزء التالي من عنوان IP
[root@www ~]# /sbin/ifconfig eth0 | grep 'inet addr' | sed 's/^.*addr://g' | sed 's/Bcast.*$//g' 192.168.1.100
أمر sed واحد، ي�除 النص من السطر الثالث إلى النهاية من /etc/passwd ويستبدل bash بblueshell
nl /etc/passwd | sed -e '3,$d' -e 's/bash/blueshell/' 1 root:x:0:0:root:/root:/bin/blueshell 2 daemon:x:1:1:daemon:/usr/sbin:/bin/sh
-e يعني تعديل متعدد، أوامر التعديل الأولى ت�除 النص من السطر الثالث إلى النهاية من /etc/passwd، والأمر الثاني يبحث عن bash ويستبدله بblueshell.
sed يمكنه تعديل محتويات الملف مباشرة دون الحاجة إلى استخدام أوامر المجاري أو إعادة توجيه البيانات! ومع ذلك، لأن هذا الحركة ستعديل الملف الأصلي مباشرة، يرجى عدم اختبار إعدادات النظام بشكل عشوائي! دعونا نستخدم ملف regular_express.txt للتحقق من ذلك!
ملف regular_express.txt يحتوي على المحتوى التالي:
[root@www ~]# cat regular_express.txt w3codebox. google. taobao. facebook. zhihu- weibo-
استخدام sed لتبديل كل سطر في regular_express.txt الذي ينتهي بـ . إلى !
[root@www ~]# sed -i 's/\.$/\!/g' regular_express.txt [root@www ~]# cat regular_express.txt w3codebox! google! taobao! facebook! zhihu- weibo-
:q:q
استخدام sed لتحديد السطر الأخير في regular_express.txt # This is a test:
[root@www ~]# sed -i '$a # This is a test' regular_express.txt [root@www ~]# cat regular_express.txt w3codebox! google! taobao! facebook! zhihu- weibo- # This is a test
بما أن $ يمثل السطر الأخير، والفعل a هو إضافة، لذا يتم إضافة # This is a test!
يمكن استخدام خيار -i في sed لتحرير محتويات الملف مباشرة، هذا الميزة مفيدة للغاية! على سبيل المثال، إذا كان لديك ملف يحتوي على 100 مليون سطر، وتريد إضافة بعض النصوص في السطر الـ 100، قد يصبح الأمر محبطًا باستخدام vim لأن الملف كبير جدًا! ماذا تفعل؟ استخدم sed! من خلال ميزة تعديل/استبدال sed مباشرة، لا تحتاج حتى إلى استخدام vim للتعديل!