Linux: Three ways of file replacement sed, awk, perl

background

Replacement of linux file content. I read about these three kinds of sed, awk, and perl on the Internet. Let’s use them one by one today to see how they are.

sed

grammar

The Linux sed command uses scripts to process text files. detailed documentation

sed [-hnV][-e<script>][-f<script文件>][文本文件]

With find you can search and replace folders:
find ./ -name "*.js" | xargs sed -i '' 's/aaa/hhh/g'

question

Generally, this command can take effect on linux.

sed -i 's/aaa/hhh/g' index.js

But I get an error when I try it command i expects \ followed by text, and the reason is that on a Mac, sed comes from BSD, which is slightly different than the sed on a typical Linux box.
insert image description here
There are two ways to solve this problem:

1. Add an empty string

insert image description here

sed -i '' 's/aaa/hhh/g' index.js

On mac, you need to add a string.
The empty string here means the suffix of the backup file. For example, you fill in

sed -i '.source' 's/aaa/hhh/g' index.js

insert image description here

2. Download gnu-sed

brew install gnu-sed

awk

AWK is a language for manipulating text files and is a powerful text analysis tool.

grammar

awk [选项参数] 'script' var=value file(s)

detailed documentation

example

awk '{ sub(/aaa/,"hhh"); print $0 }' index.js

insert image description here
It can be seen that the aaa has indeed been changed to hhh, but it is only a printout without modifying the source file, so how to change it? use>

In linux, > means to write the content, >> means to append the content, you can read this article

awk '{ sub(/aaa/,"hhh"); print $0 > "index.js" }' index.js

insert image description here
This can be replaced normally

perl

perl is a programming language that comes with linux. Detailed Documentation
Perl borrows features from C, sed, awk, shell scripts, and many other programming languages. The syntax is somewhat similar to these languages, but it also has its own features.
insert image description here

grammar

  • -p tells Perl to traverse the file name parameters added later, which is roughly similar to the effect of this code in sed
  • -i is roughly used for backup operations when reading files.
  • -e tells Perl to follow the perl statement
  • -p and -i can be combined -pi, -p -e can also be combined, but -i, -e cannot be combined to write -ie.

example

perl -pi -e 's|aaa|hhh|g' index.js

insert image description here
Replace and make a backup

perl -pi.source -e 's|aaa|hhh|g' index.js

insert image description here

find ./ -name "*.js" | xargs perl -pi -e 's|aaa|hhh|g'

insert image description here

Summarize

  • sed - When you need to do simple text transformations on files.
  • awk - when you just need to simply format and summarize or transform data.
  • perl - Almost any task, but especially when the task requires complex regular expressions.
  • python - For the same tasks you can use Perl.

sed is an editor (command line)
awk is a text processing tool (command line)
shell, perl, python are programming languages

The above / and | can be used, such as 's/aaa/hhh/g', 's|aaa|hhh|g'

Guess you like

Origin blog.csdn.net/weixin_43972437/article/details/130604845