Linux-operation 1 (replace text content)

Linux-sed

Whole row operation

Operations outside quotation marks:
-r Regular (regular in script)
-n Cancel automatic printing mode
-i Directly modify file content ( sed -i "2,3 s/('15133/3/g" test1.txt )
Operations within quotation marks:
-a Add new (when the next line of a line)
-c Replace and replace (whole line replacement)
-d delete
-i insert (a line above the current line) ()
-p print
-s replace and replace (referring to the replacement of strings in the line, which can be used with regular expressions)

view function

sed -n '1,4 p' test1.txt‘

insert image description here

find replace

sed -i 's/str1/str2/g'  filename
str1: 旧字符
str2: 新字符

It does not distinguish whether the replacement string escapes
the old string, which can be represented by regular expressions
-i changes the original file

will be ',replaced with,

sed -i "s/',/,/g" test1.txt 

insert image description here
-iComparison between inside and outside quotation marks
insert image description here

'Replace lines 2 and 3 with-

sed -n "2,3 s/'/-/p" test1.txt

insert image description here

sed -i "2,3 s/('15133/3/g" test1.txt (更改文本内容))
sed -n "2,3 s/('15133/3/p" test1.txt (不更改文本内容)

insert image description here

Linux-perl

-e executes the command, (parameters that must be written)
-p automatic loop + output
-pe can read each line of the file and process it according to the given command.
No changes to the original file
will 'be replaced with.

perl -pe "s/'/./g" test1.txt 

insert image description here

vim replacement

替换命令的完整语法
:[range]s/str/str2/[flags]

[flags]
By default, operate on the first matching item in the specified range
-g for all operations
-c ask the user to confirm before replacing
-e ignore errors during execution
[range]
-$ indicates the last line
-% indicates all lines Equivalent to -1, $
-. indicates the current line
-n, m indicates the line from n to m
-. +1 indicates the next line of the current line
example

:s/str1/str2/ 替换当前行第一个str1为str2
:s/str1/str2/g 替换当前行所有str1为str2
:$s/str1/str2/g  替换最后一行的所有str1为str2
:n,$s/str1/str2/ 替换第 n 行开始到最后一行中每一行的第一个str1为str2
:n,$s/str1/str2/g 替换第 n 行开始到最后一行中每一行所有str1为str2
(n 为数字,若 n 为 .,表示从当前行开始到最后一行)
:%s/str1/str2/(等同于 :g/str1/s//str2/) 替换每一行的第一个str1为str2
:%s/str1/str2/g(等同于 :g/str1/s//str2/g) 替换每一行中所有str1为str2

Guess you like

Origin blog.csdn.net/weixin_44964850/article/details/125487157