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

The usage of sed command of three Musketeers in text processing

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

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

Introduction to 1.sed

Sed is a character stream-oriented "non-interactive" editor. Most of the manual operations on the text in vim compilation can be done through the sed command, so it is convenient for the compiler under linux to automatically process files in batches, such as analyzing log files, modifying configuration files, and so on.

2.sed command usage

Usage:

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

Common options:

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

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

~] # sed-e's @ ^ UUID. * @ @ g'-e'/ ^ [[: space:]] * $/ d' / etc/fstab

-f script-file,-- file=script-file one edit command per line

-r,-- regexp-extended: regular expressions that support using extensions

-I [SUFFIX],-- in-place [= SUFFIX]: edit the source file directly

This operation is risky, please use it with caution!

Script: address delimiting

(1) empty address: process the full text

(2) single address:

#: specify a line

/ pattern/: each line matched by this pattern

(3) address range:

#, #: lines from # to

#, + #:

#, / pat1/

/ pat1/,/pat2/

$: last line

(4) step by step: ~

1x 2: all odd lines

2 # 2: all even lines

Edit commands:

D: delete the contents of the schema space

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" before the line. Multiple line appends are supported.

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

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

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

!: the condition is reversed

Format: address demarcation! Editing command

R\ PATH/TO/SOMEFILE: reads the contents of the specified file to the line to which the current file is matched by the pattern, which is generally used for file merging

The delimiter can be specified by oneself. The commonly used ones include the following: finding and replacing, etc.

Replace the tag:

G: global replacement

P: show the rows that have been replaced successfully

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

Usage example:

1. Add comment information before the line of UUID in the / etc/fstab file

~] # sed'/ ^ UUID/i\ this is UUID' / etc/fstab

two。 Delete all empty reds of a file

~] # sed'/ ^ $/ d' FILE

3. Delete all white space characters at the beginning of all lines that begin with white space characters in the / boot/grub2/grub.cfg file

~] # sed's @ ^ [[: space:]]\ + @ @ g'grub.cfg

4. Append information to the end of the line in the / etc/fstab file

~] # sed'$a\ sed command end' / etc/fstab

3. Advanced editing command

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 contents of the pattern space with the retention 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

Usage example:

~] # sed-n'npolitics p' FILE: display even lines, ";" used to separate multiple commands ~] # sed '1displaying Gentleman characters d'FILE: display the contents of the file in reverse order ~] # sed'! $d'FILE: take out the last line ~] # sed'$! n tinctorials D'FILE: two lines after taking out the file ~] # sed'/ ^ $/ d G'FILE: delete all the original blank lines, then add a blank line after all the non-blank lines ~] # sed'n FILE: show odd lines ~] # sed'G' FILE: add a blank line after each original line

4. Actual combat training

To back up all the files in the modified / etc/ directory once a week

~] # vim / backupsUniverse bf.bf.shanzhao binds to Backup bashcards dirt trees etc`date +% Fafu% Hafu% Mmurf% S`mkdir-p / backups/$dirfor file in `find / etc/*-mtime-7`; do cp-r $file / backups/$dir & > / dev/nulldonecd / backups & & tar-Jcf ${dir} .tar.xz $dir & & rm-rf / backups/$dir~] # crontab-e0 * * 1 / bin/bash / backups/bf.sh

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