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

Case Analysis using sed Command

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the relevant knowledge of "sed command use case analysis". The editor shows you the operation process through the actual case, and the operation method is simple, fast and practical. I hope this "sed command use case analysis" article can help you solve the problem.

Sed is a "non-interactive" character stream-oriented editor. Can handle the contents of multiple files and multiple lines at the same time, you can input the whole file to the screen without changing the original file, and you can input the content that only matches the pattern to the screen. You can also make changes to the original file, but the results are not returned on the screen.

Command format of sed: sed [options] 'command' file (s); script format of sed: 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: modify file contents directly;-n: print only lines that match the mode -r: support extension expressions;-h or-- help: display 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 has an effect on all lines that are not selected; = print the current line number; # extend the comment before the next newline character; sed replacement mark g for full in-line 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 to another (but not for regular expressions) \ 1 substring matching tag; & matched string tag; sed metacharacter set ^ matching lines start, 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:

Sed's _

The-n option is used with the p command to print only those lines that are replaced:

Sed-n's Universe TestUniverse p 'file

The direct edit file option-I will match the first book of each line in the file file and replace it with books

Sed-I'sUniverse bookCharger booksAccording to G' file

Full replacement tag g

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

Sed's _

When you need to start with the N match, you can use / Ng:

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'

When the 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

Delete line 2 of the file:

Sed '2d' file

Delete all lines from line 2 to the end of the file:

Sed '2d` file

Delete the last line of the file:

Sed'$d' file

Delete all lines in the file that begin with test:

Sed'/ ^ test/'d file

Matched 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 are 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 | sed 's/digit\ ([0-9]\) /\ 1/'this is 7 in a number

In the command, digit 7 is replaced with 7. The substring matched by the style is 7, (..) For matching substrings, the first matching substring is marked\ 1, and so on the second matching result is\ 2, for example:

Echo aaa BBB | sed's /\ ([Amurz]\ +\)\ ([Amurz]\ +\) /\ 2\ 1/'BBB aaa

Love is marked as 1, and all loveable is replaced with lovers and printed out:

Sed-n's /\ (love\) able/\ 1rsUnip 'file

Combine multiple expressions

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

Quote

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

Print all lines from line 5 to the first line that contains lines starting with test:

Sed-n '5Jing / ^ test/p' file

For 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:

Sed-e '1cr 5d'-e's Cash TestChargeCash' file

The first command of the above sed expression deletes lines 1 to 5, and the second command replaces test with check. 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

Read from 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

Write file: the w command writes all lines that contain test in example to file:

Sed-n'/ test/w file' example

Append (below 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

Insert 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

Insert 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; s _ A _ b _ r _ b;} 'file

Morph: 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 Universe' file

Exit: 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 row that matches 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)

Print odd or even lines

Method 1:

Sed-n'ptern 'test.txt # Odd line sed-n'nten p'test.txt # even line

Method 2:

Sed-n'1x 2p 'test.txt # Odd Line sed-n' 2p 'test.txt # even Line

Print the next line of the matching string

Grep-A 1 SCC URFILEsed-n'/ SCC/ {ntransferp} 'URFILEawk' / SCC/ {getline; print} 'URFILE, this is the end of the introduction of "case Analysis of sed Command usage". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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