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

Learn the sed usage of notes

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

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

Sed command usage

Sed is one of the three Musketeers of the text.

What is sed?

Sed: a line editing tool that handles a line of text. Read one line from the text at a time and put it in your own pattern space. If it can be matched by the pattern given by sed, then edit it and output it; if it cannot be matched by the pattern, then the content that cannot be matched in the pattern space will be output to the screen by default.

Note: sed is a tool that does not edit the original text, and once a line of text is read into the pattern space, it will first output the contents of the pattern space to the screen, and then determine whether the lines in the pattern space can be matched by the pattern.

What's the use of learning sed? Can better deal with the text, view the text.

Syntax:

Sed [OPTION]... 'script' [input-file]...

Script':' address delimiting editing command (address delimiting and editing commands have no spaces)

Common options: [OPTION]

-n: does not output content in mode space to the screen

-e script,-- expression=script: multipoint editing

-f / PATH/TO/SED_SCRIPT_FILE

One editing command per line; multiple editing commands form a sed script

-r,-- regexp-extended: supports the use of extended regular expressions

-I [SUFFIX],-- in-place [= SUFFIX]: edit the original file directly; (the edited results are saved directly to the original file)

'script'

Address demarcation:

(1) empty address: process the full text

(2) single address:

#: specify a line

/ pattern/: every line to which this pattern is matched

(3) address range

#, #: lines # to

#, + #: # lines and # lines below

#, / pat1/: Line # to the line to which the / Pat1/ pattern matches

Rows matched by / pat1/,/pat2/: pattern 1 to rows matched by pattern 2

$: last line

(4) step by step: ~

1x 2: all odd lines

2 # 2: all even lines

Edit commands:

D: deletin

[root@bucktan tmp] # sed'/ ^ UUID/d' fstab deletes lines that start with UUID

[root@bucktan tmp] # sed '9d' fstab delete line 9

P: display the contents of the mode space

A\ text: append the text "text" after the line, which supports the use of\ nMultiline append

I\ text: insert the text "text" in front of the line, which supports the use of\ nMultiline insertion.

C\ text: replace the matching line with the text "text" specified here

W / PATH/TO/SOMEFILE: save the lines matched by the pattern space to the specified file

[root@bucktan tmp] # sed'/ ^ # / w apple2' fstab

R / PATH/FROM/SOMEFILE: reads the contents of the specified file to the line to which the current file is matched by the pattern; file merge

[root@bucktan tmp] # sed'/ ^ # / r apple2 (# text 1) 'fstab (# text 2)

# read the contents of text 1 to the end of the line starting with the # sign in text 2

=: print the line number for the line to which the pattern matches

!: the condition is reversed

Address demarcation! Editing command

Eg: [root@bucktan tmp] # sed-n'/ ^ # /! P 'fstab

# does not display the content in the mode space to the screen, and displays the matching lines that do not begin with the # sign to the screen

The delimiter can be specified by oneself. The commonly used ones are swatches, slots, etc.

Eg: [root@bucktan tmp] # sed'/ ^ # / s fstab replace the # in the line starting with the # sign to replace the @ sign

Replace the tag:

G: global replacement; (by default only the text that matches the first occurrence is replaced)

Eg: [root@bucktan tmp] # sed'/ ^\ / dev/s@v@e@g' fstab

# replace all v with e in lines starting with / dev

W / PATH/TO/SOMEFILE: save the result of successful replacement to the specified file

P: show the rows that have been replaced successfully

Exercise 1: remove all white space characters at the beginning of all lines that begin with white space characters in the / boot/grub/grub2.cfg file

~] # sed's @ ^ [[: space:]]\ + @ @'/ etc/grub2.cfg

Exercise 2: delete the # sign at the beginning of all lines starting with # and all white space characters after # in the / etc/fstab file

~] # sed's @ ^ # [[: space:]] * @ @'/ etc/fstab

Exercise 3: output an absolute path to the sed command and take out its directory, which behaves like dirname

~] # echo "/ var/log/messages/" | sed's @ [^ /]\ + /\? $@ @'

~] # echo "/ var/log/messages" | sed-r's @ [^ /] + /? $@ @'

Advanced editing commands:

H: overwrite the contents of the schema space to the holding space

H: append the contents of the schema space to the holding space

G: overwrite the contents of the hold space to the pattern space

G: append the contents of the hold space to the pattern space

X: interchange the content in the pattern space with the content in the hold space

N: overrides the next line of the matched row read to the pattern space

N: append the next row of the matched row to the pattern space

D: delete rows in schema space

D: delete all rows in multiline mode space

Example:

Sed-n'nposition p 'FILE: displays even rows

'ntterp 'means to execute two commands (njournal p) and match the first line but save the second line to the pattern space, followed by p

Sed '1displaying Gentleman FILE: displays the contents of a file in reverse order

Analysis: 1! G: append the contents of the hold space to the pattern space if it is not the first line

H: overwrite the contents of the schema space to the holding space

$! d: delete the row in the schema space if it is not the last line

Sed'$! d 'FILE: take out the last line

Sed'$! n 'FILE: two lines after checking out the file

Sed'/ ^ $/ dash G'FILE: delete all existing blank lines, and then add a blank line after all non-blank lines

Parse: / ^ $/ d: delete if it is a blank line; G: append the contents of the hold space to the pattern space

Sed'nfield 'FILE: displays odd lines

Sed 'G' FILE: add a blank line after each original line

Write a script to perform the following functions:

Actual combat: determine the hostname, if it is not linux-magedu, change the hostname to magedu.com-IP and take effect permanently.

(hint: IP can be obtained through ifconfig and obtained after text processing)

(example: IP:172.18.100.1 hostname is local.host, modified hostname is magedu.com-172.18.100.1)

Prompt steps:

1. Acquisition of IP

2. Judgment of hostname

3. Modify hostname by command.

4. Modify / etc/hostname file through sed

Temporary method:

#! / bin/bash

# description=the function of "sed"

# date=2016.4.7

# name=sed.sh

# author=bucktan

# mail

Hostname= `ifconfig | head-2 | tail-1 | cut-d:-f2 | cut-d'- f1`

HostName= `hostname`

If ["$hostName"! = "linux-magedu"]; then

Hostname magedu.com-$Hostname

Fi

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

Network Security

Wechat

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

12
Report