Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the sed command on Linux system

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "Linux system sed command how to use", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "Linux system sed command how to use" this article.

Sed is a stream editing tool for filtering and replacing text, especially when you want to make unified changes to dozens of configuration files, you will feel the charm of Sed.

Options, commands, replacement tags for sed

Command format

Sed command format: sed [options] 'command' file (s); sed script format: sed [options]-f scriptfile file (s); option

-e: edit sed actions directly on the command line mode, which is the default option;-f: write sed actions in a file, and use-f filename to perform sed actions in filename;-I: directly modify the contents of the file;-n: print only lines that match patterns;-r: support extended expressions;-h or-- help: show help;-V or-- version: display version information. Parameters.

File: specifies a list of text files to be processed.

Sed common commands

A\ insert text below the current line; I\ insert text above the current line; c\ change the selected line to new text; d delete, delete the selected line; D delete the first line of the template block; s replace the specified character; h copy the contents of the template block to the buffer in memory; H append the contents of the template block to the buffer in memory G gets the contents of the memory buffer and replaces the text in the current template plate; G gets the contents of the memory buffer and appends it to the text of the current module block; l the list cannot print a list of characters; n reads the next input line and processes the new line with the next command instead of the first command; N appends the next input line to the template plate and embeds a new line between the two to change the current line number P print the lines of the template plate. P (uppercase) prints the first line of the template block; Q exits the Sed;b lable branch to the marked place in the script, or to the end of the script if the branch does not exist; r file reads lines from file; t label if branch, starting from the last line, once the condition is met or the TMagar command will result in the branch to the labeled command, or to the end of the script T label error branch, starting from the last line, will result in a branch to the labeled command or to the end of the script in the event of an error or T file command; w file writes and appends the template block to the end of file; W file writes and appends the first line of the template block to the end of file;! Indicates that the following command works on all lines that are not selected; = prints the current line number; # extends the comment before the next newline character; sed replaces the tag

G for full inline replacement; p for print line; w for writing lines to a file; x for text in the interchange module plate and text in the buffer; y for translation of one character into another character (but not for regular expressions);\ 1 substring matching tag; & matched string tag; sed metacharacter set

^ matching lines begin, such as: / ^ sed/ matches all lines that begin with sed; $matches lines ending, such as: / sed$/ matches all lines ending with sed;. Matches any character that is not a newline character, such as: / s.d/ matches s followed by any character, and finally dash * matches 0 or more characters, such as: / * sed/ matches all templates with one or more spaces followed by lines of sed; [] matches characters in a specified range, such as / [ss] ed/ matches sed and Sed [^] matches a character that is not within the specified range, such as: / [^ A-RT-Z] ed/ matches the beginning of a letter that does not contain Amurr and Tmurz, following the line of ed;\ (..\) matches the substring and saves the matching characters, such as s /\ (love\) able/\ 1rsrecoverable is replaced with lovers Save the search characters to replace other characters, such as the end of * * love**;\ matching words, such as / love\ > / matching lines containing words ending in love; x\ {m\} repeating characters xm times, e.g. / 0\ {5\} / matching lines with 5 zeros X\ {m,\} repeat character x at least m times, for example: / 0\ {{5,\} / match lines with at least 5 zeros; x\ {mforce n\} repeat character x, at least m times, no more than n times, such as: / 0\ {5c10\} / lines matching 5cm 10\}; sed user instance

Replace operation: s command

Replace the string in the text:

The sed's _ option with the p command means that only those lines that are replaced are printed:

Sed-n's file file TestUnip direct edit file option-I, will match the first book of each line in the file file and replace it with books

Sed-I's file full replacement mark g

Using the suffix / g tag replaces all matches in each line:

When you need to start the replacement from the N match, you can use / Ng: sed's Ng file.

Echo sksksksksksk | sed 's/sk/SK/2g'skSKSKSKSKSKecho sksksksksksk | sed' s/sk/SK/3g'skskSKSKSKSK echo sksksksksksk | sed 's/sk/SK/4g'skskskSKSKSK delimiter

The character / in the above command is used as a delimiter in sed, or any delimiter can be used

Sed 's:test:TEXT:g'sed 's | test | TEXT | g' delimiter appears inside the style, it needs to be escaped:

Sed's /\ / bin/\ / usr\ / local\ / bin/g' delete operation: d command

Delete a blank line:

Sed'/ ^ $/ d'file deletes line 2 of the file:

Sed '2d' file deletes all lines from line 2 to the end of the file:

Sed'2 file deletes the last line of the file:

Sed'$d' file deletes all lines in the file that begin with test:

Sed'/ ^ test/'d file has matched the string token &

The regular expression\ w + matches each word and replaces it with [&], which corresponds to the word that was previously matched:

Echo this is a test line | sed's /\ w\ + / [&] / g'[this] [is] [a] [test] [line] all lines starting with 192.168.0.1 will be replaced with their own localhost:

Sed's / ^ 192.168.0.1 / & localhost/' file 192.168.0.1localhost substring matching tag\ 1

Matches a portion of a given style:

Echo this is digit 7 in a number | digit 7 in the sed 's/digit\ ([0-9]\) /\ 1/'this is 7 in a number command, replaced with 7. The substring to which the style matches is 7, (..) is used to match the substring, and the first substring to be matched is marked as\ 1, and so on the second result is\ 2, for example:

Echo aaa BBB | sed's /\ ([Amurz]\ +\)\ ([Amurz]\ +\) /\ 2\ 1/'BBB aaalove is marked as 1, and all loveable will be replaced with lovers and printed out:

Sed-n's /\ (love\) able/\ 1rsPlacer p'file combines multiple expressions

Sed 'expression' | sed 'expression' is equivalent to: sed 'expression; expression' reference

Sed expressions can be referenced in single quotes, but if the expression contains a variable string, you need to use double quotes.

Test=helloecho hello WORLD | sed "s/$test/HELLO" HELLO WORLD range of selected lines:, (comma)

All lines within the range determined by the templates test and check are printed:

Sed-n'/ test/,/check/p' file prints all lines from line 5 to the first line that contains lines starting with test:

Sed-n'5J / ^ test/p' file for the lines between template test and west, the end of each line is replaced with the string aaa bbb:

Sed'/ test/,/west/s/$/aaa bbb/' file multipoint editing: e command

The-e option allows multiple commands to be executed on the same line:

The first command of the sed expression above sed-e '1check 5d'-e's file removes lines 1 to 5, and the second command replaces test with TestCharger. The order in which the commands are executed affects the results. If both commands are replacement commands, the first replacement command affects the result of the second replacement command.

The command equivalent to-e is-expression:

Sed-- expression='s/test/check/'-- expression='/love/d' file reads from a file: r command

The contents of the file are read in and displayed after the lines that match the test. If multiple lines are matched, the contents of the file will be displayed under all the matching lines:

Sed'/ test/r file' filename writes to the file: the w command writes all lines that contain test in example to file:

Sed-n'/ test/w file' example append (below the line): a\ command

Append this is a test line to the line that begins with test:

Sed'/ ^ test/a\ this is a test line' file inserts this is a test line after line 2 of the test.conf file:

Sed-I'2a\ this is a test line' test.conf insert (on line):

The I\ command appends this is a test line to the line that begins with test:

Sed'/ ^ test/i\ this is a test line' file inserts this is a test line before line 5 of the test.conf file:

Sed-I'5i\ this is a test line' test.conf next: n command

If the test is matched, move to the next line of the matching line, replace the aa for that line, change to bb, print the line, and then continue:

Sed'/ test/ {n; sqqaqqb;} 'file morphing: y command

Convert all abcde in lines 1 through 10 to uppercase. Note that regular expression metacharacters cannot use this command:

Sed '1Magazure 10yCompact ABCDE file exits: Q command

After printing line 10, exit sed sed '10q' file keep and get: h command and G command when sed processes the file, each line is saved in a temporary buffer called mode space, unless the line is deleted or the output is canceled, otherwise all processed lines will be printed on the screen. The pattern space is then emptied and stored on a new line to be processed.

Sed-e'/ test/h'-e'$G' file in this example, when a line matching test is found, it is stored in the pattern space, and the h command copies it and stores it in a special buffer called the hold cache. The second statement means that when the last line is reached, the G command takes out the line that holds the buffer, puts it back in the pattern space, and appends it to the end of the line that already exists in the pattern space. In this case, it is appended to the last line. Simply put, any line that contains test is copied and appended to the end of the file.

Keep and swap: h command and x command

Swap the mode space and keep the contents of the buffer. That is, swap lines containing test and check:

Sed-e'/ test/h'-e'/ check/x' file script scriptfile

The sed script is a list of sed commands that boot the script file name with the-f option when Sed is started. Sed is very picky about the commands entered in the script. There can be no white space or text at the end of the command, and if there are multiple commands on one line, separate them with semicolons. Behaviors that begin with # comment lines and cannot span lines.

Sed [options]-f scriptfile file (s) prints odd or even lines

Method 1:

Sed-n'ptern 'test.txt # Odd-numbered line sed-n'nten p'test.txt # even-numbered line method 2:

Sed-n '2p' test.txt # Odd Line sed-n' 2p' test.txt # even Line prints the next line of the matching string

Grep-A 1 SCC URFILEsed-n'/ SCC/ {ntransferp} 'URFILEawk' / SCC/ {getline; print} 'URFILE is all the contents of this article entitled "how to use sed commands in Linux system". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report