Get the App
SLTechnology News&Howtos  ›  Servers  › 

5Perfect Linux three Musketeers sed

Shulou Source: shulou.com Published: 2022-06-03 05:43:51 09月17日 Update

First, the use of the sed command: (good at text editing, good at manipulating lines)

1. Add information

two。 Delete information

3. Modify information

4. Filter information (grep is stronger)

2. View information

1) View the information according to the specified line number

Filter single line information to display the second line of information-n means to filter invalid information

Filter continuous multiple lines of information

Filter discontiguous multiple lines of information

3. View the information according to the specified content

Filter single line information

Filter continuous multiple lines of information

Filter discontiguous multiple lines of information

Summary: commands using the sed parameter

-n means to cancel the default output information

P for display information

4. Add information

1) add information according to the specified line

Indicates that information is inserted above line 2

Indicates the insertion of multiple rows,\ nindicates the branch

Indicates that information is inserted below line 2

Summary:

I means above the specified line

A means above the specified line

Similarly: according to the specified character increase information, change the number of lines to / character /, you can increase the information according to the character.

5. Delete the information:

1) delete the information according to the specified line

2) delete the information according to the specified character

Summary: all sed additions and modifications are made in memory and are only temporarily displayed on the screen. To write to the hard disk, add the parameter I.

But using the sed command to modify the contents of the file is best to make a backup first.

6. Modify the information:

Format: sed "s#xxx#xxx#g"

1) modify the information by the specified line

7. Exercises:

Exercise 1: create multiple users at once and set passwords at the same time

The first course: create a single user and set a password

Useradd stu01;echo '123456' | passwd-- stdin stu01

The second course: create multiple users and set passwords

Echo stu {01.. 10} | xargs-N1 | sed-r "s # (. *) # useradd\ 1bot echo '123456' | passwd-- stdin\ 1millig" | bash |

Exercise 2:

Change all .txt files in the oldboy01 directory to .jpg files

Note: be careful when using the sed command to share the-n and-I parameters, because-n means to cancel the default output

That often only shows the results you want to match when using-I to write to the hard disk or often the contents of the file only use the information you match, and all the other content is gone.

Exercise 3: get the ip address method:

Practice 4:sed to create users in batches, use dynamic passwords and save

Tags: Information multiline content command character password file user parameter course multiple method hard disk output one-time usage memory branch dynamic single Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux macOS Xiaomi MariaDB Shulou Technology