Learn sed together (3)

First look at the following piece of messy Python code

 4. import random

 5.

 6.

 7. def rabinMiller(num):

 8.     # Returns True if num is a prime number.

 9.

10.     s = num - 1

11.     t = 0

12.     while s % 2 == 0:

This is the beginning of a piece of python code copied from the Internet. The format is very confusing, there are line numbers, there are too many blank lines and spaces, this kind of direct copy code cannot be run directly, you need to manually put these useless symbols Delete. For hundreds or thousands of lines of code, if you manually delete too much trouble, even if you use some text editor replacement functions, it may not be able to achieve your goal well. This is the time for sed to show its talents.

  • Delete blank lines (for a Python code block, blank lines will produce errors)
  • Delete the line number to align the code to the left
  • Delete notes (lines starting with # sign)
sed -e 's/^...//g' -e '/^$/d' -e '/^\s*#/d' -e 's/^.//g' tt.txt

The above code specifies 4 sed commands through the -e parameter, which respectively delete the first three characters (line number) at the beginning of each line, delete blank lines, delete remark lines beginning with # sign (\s represents a space character), delete every The first character of the line is left-justified. Then we got the standard Python code format.

import random
def rabinMiller(num):
    s = num - 1
    t = 0
    while s % 2 == 0:

Learn sed together (3)

{}: Nested sed commands

For more complex sed commands, you can use braces to group different commands, such as the Python code above. If you want to organize the code between 4-100 lines, you can first specify the line range, and then use the braces to group Different sed commands are combined.

sed -n '
    4,100{
        s/^...//g
        /^$/d
        /^\s*#/d
        s/^.//g
        p
    }
`

Learn sed together (3)

r: read file

Here we need two files for demonstration, create another text file example1.txt, as follows

The other test file.
ID 0000124
Test END!

If we want to insert the file exemple1.txt at the second line of the example.txt file

sed '2r example1.txt' example.txt
# This is a test file.
# It is the last day of 2018.
# The other test file.
# ID 0000124
# Test END!
# Hope all you success!
# HAPPY NEW YEAR!

If you want to insert in the last line of the example.txt file

sed '$r example1.txt' example.txt

If you want to insert the file after the number 8 line

sed '/8/r example1.txt' example.txt
# This is a test file.
# It is the last day of 2018.
# The other test file.
# ID 0000124
# Test END!
# Hope all you success!
# HAPPY NEW YEAR!

Learn sed together (3)

a or i: add string line

For example, we want to add a line "THIS is the middle of the file" after (a) or before (i) the second line of the example.txt file

sed '2a THIS is the middle of the file' example.txt
# This is a test file.
# It is the last day of 2018.
# THIS is the middle of the file
# Hope all you success!
# HAPPY NEW YEAR!

sed '2i THIS is the middle of the file' example.txt
# This is a test file.
# THIS is the middle of the file
# It is the last day of 2018.
# Hope all you success!
# HAPPY NEW YEAR!

Both a and i can add rows, the difference is that a (append) is added after the specified row, and i (insert) is added before the specified row.

Learn sed together (3)

c: modify the line

For example, if we want to modify the second line of the example.txt file to "It is the first day of 2019", we can use the c(change) parameter to modify it.

sed '2c It is the first day of 2019' example.txt
# This is a test file.
# It is the first day of 2019
# Hope all you success!
# HAPPY NEW YEAR!

Learn sed together (3)

"=": output line number

I learned awk before, combined with "wc -l", you can output the number of lines in a file.

wc -l example.txt | awk '{print $1}' 
# 4

Of course, a simpler way is to use "=" to output the line number in sed. The usage is as follows:

sed -n '$=' example.txt
# 4

We can also filter out rows that meet certain conditions, such as outputting the number of rows starting with "H"

sed -n '/^H/=' example.txt
# 3
# 4

Learn sed together (3)

y: conversion character

For example, to convert all "a" to "A":

sed 'y/a/A/' example.txt
# This is A test file.
# It is the lAst dAy of 2018.
# HopeAll you success!
# HAPPY NEW YEAR!

Learn sed together (3)

l: display control character

It is especially useful when looking for bugs, displaying text control characters that are not normally displayed.

sed -n 'l' example.txt
# This is a test file.$
# It is the last day of 2018.$
# Hope all you success!$
# HAPPY NEW YEAR!$

A "$" is added at the end of each line to indicate the end of the line.

A hundred reading is worse than one practice

===== THE END ====

Reference materials: http://www.grymoire.com/Unix/Sed.html#uh-30

Learn sed together (3)

Guess you like

Origin blog.51cto.com/15069450/2577350