Home >Operation and Maintenance >Linux Operation and Maintenance >How to replace the sed command in Linux

How to replace the sed command in Linux

WBOY
WBOYOriginal
2022-02-25 16:24:2415444browse

Method: 1. Set the parameter "c", c can be followed by strings, these strings can replace the specified lines, the syntax is "sed 'line number c content after replacement'"; 2. Set The parameter "s" can be used for substitution directly, and the syntax is "sed 's/string to be replaced/new string/g'".

How to replace the sed command in Linux

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

How to replace the sed command in Linux

The Linux sed command uses scripts to process text files.

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, write conversion programs, etc.

Syntax

sed [-hnV][-e<script>][-f<script文件>][文本文件]

Parameter description:

  • -e3f1c4e4b6b16bbbd69b2ee476dc4f83a or --expression=3f1c4e4b6b16bbbd69b2ee476dc4f83a with the script specified in the option Process input text files.

  • -f8d9e20cf7980a42776913d042440355f or --file=8d9e20cf7980a42776913d042440355f Process the input text file with the script file specified in the option.

  • -h or --help displays help.

  • -n or --quiet or --silent only displays the results after script processing.

  • -V or --version displays version information.

Action description:

  • a: Newly added, a can be followed by strings, and these strings will appear on a new line (The current next line)~

  • c: Replacement, c can be followed by strings, and these strings can replace the lines between n1 and n2!

  • d: Deletion, because it is deletion, so d is usually not followed by anything;

  • i: Insertion, after i Strings can be connected, and these strings will appear on a new line (the current previous line);

  • p: Print, that is, print out a selected data. Usually p will be run together with the parameter sed -n~

  • s: Replacement, you can directly perform the replacement work! Usually this s action can be paired with regular notation! For example, 1,20s/old/new/g is it!

The example is as follows:

How about replacing the contents of lines 2-5 with "No 2-5 number"?

[root@www ~]# nl /etc/passwd | sed &#39;2,5c No 2-5 number&#39;
1 root:x:0:0:root:/root:/bin/bash
No 2-5 number
6 sync:x:5:0:sync:/sbin:/bin/sync
.....(后面省略).....

In addition to the entire line processing mode, sed can also search and replace partial data in behavioral units. Basically sed's searches and substitutions are quite similar to those of vi! It is a bit like this:

sed &#39;s/要被取代的字串/新的字串/g&#39;

First observe the original information and use /sbin/ifconfig to query the 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
.....(以下省略).....

The IP of this machine is 192.168.1.100.

Delete the first part of the IP

[root@www ~]# /sbin/ifconfig eth0 | grep &#39;inet addr&#39; | sed &#39;s/^.*addr://g&#39;
192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0

Then delete the subsequent part, that is: 192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0

Delete the part behind the IP

[root@www ~]# /sbin/ifconfig eth0 | grep &#39;inet addr&#39; | sed &#39;s/^.*addr://g&#39; | sed &#39;s/Bcast.*$//g&#39;
192.168.1.100

Related recommendations: "Linux Video Tutorial"

The above is the detailed content of How to replace the sed command in Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn