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 sed Command under linux

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces "the use of sed command under linux". In daily operation, I believe that many people have doubts about the use of sed command under linux. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "the use of sed command under linux". Next, please follow the editor to study!

# options and parameters: #-n: use silent mode. In the general use of sed, all data from STDIN is generally listed on the terminal. But if you add the-n argument, only the row (or action) that has been specially handled by sed will be listed. #-e: edit the action of sed directly on the command line mode; #-f: write the action of sed directly in a file, and-f filename can run the sed action in filename; #-r: the action of sed supports the syntax of extended normal representation. (the default is the basic regular representation syntax) #-I: directly modify the contents of the read file instead of outputting it to the terminal. # # Action description: [N1 [, N2]] function# N1, N2: select the number of lines for the operation, 2, 5function represents 2 to 5 lines, 2, + 5function represents the second line begins and goes back 5 lines #! function takes the reverse # # function:# a: new lines can be followed by strings, and these strings will appear on the new line (the current next line) # c: replace lines, c can be followed by strings These strings can replace the lines between N1 and N2! Format: # d: delete a line, d is usually not followed by a string; format: # I: insert a line, I can be followed by a string, and these strings will appear on a new line (the current previous line); # p: print a selected line. Usually p will run with the parameter sed-n ~ # s: replace, you can directly replace the work! Usually the action of this s can be matched with the formal representation! For example, the first matching string # c and s of each line is replaced without g at the end, and the difference from s is that only the matched string is replaced, and c is the line # example that replaces the matched string # example: # create a test file echo-e "you are my love\ nhello word\ nfuck coding\ nhttp://baidu.com" > test.txt # with no options The result of the operation is printed to the screen, and the test.txt # function s # you will not be modified and replaced by the an in lines 1 to 2 of he sed's YouTube test.txt # 1 to 2 with b sed'1 and 2s test.txt # function a, and insert k on the next line with the hello line. Do not add a line after a that looks better # match to the next line of hello add a line k sed'/ hello/a\ k 'test.txt # 1 to 2 lines add a line k sed' 1 test.txt # function c after each line, replace the behavior k with hello The lines that match hello can be replaced by k sed'/ hello/c\ k 'test.txt # 1 to 2 with k sed' 1 test.txt # function I, the same as a, except that this is to insert # function p,! P inverted on the previous line of the matching line. Use # with-n to print the matching line, or reverse sed-n'/ hello/p' test.txt sed-n'/ Hellobind to print 1 to 2 lines. Or reverse sed-n'1 function 2p 'test.txt sed-n' 1 test.txt 2p 'test.txt # function d # delete matching lines, or reverse sed' / hello/d' test.txt sed'/ hellodeband delete lines 1 to 2 Or reverse sed-n'1 test.txt sed 2p 'test.txt #-I option, this is more often used, this direct manipulation file uses the same, and the effect is the same as sed' / hello/d' test.txt > test.txt. # other options to figure this out, the study on "how to use the sed command under linux" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Internet Technology

Wechat

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

12
Report