当前位置:网站首页>Regular and extended expressions, sed text processor and awk tool, changing IP address with script
Regular and extended expressions, sed text processor and awk tool, changing IP address with script
2022-07-19 02:47:00 【For whom do the stars change】
1、 Regular expressions (grep)
effect :
Use a single string to describe it
Matches a series of strings that conform to a syntax rule
form :
Ordinary characters and metacharacters
Metacharacters :
[ ] : Any character in the middle bracket
[^] : Except for any character in the middle bracket
^ : Find the beginning of a line , What does it start with
$ : Find end of line , What does it end with
. : Find any character
* : Look for duplicate characters
{ } : Find range of consecutive characters
(1) Find specific characters -n Indicates display line number ,-i Indicates case insensitive ,-v Indicates reverse selection
grep -n 'the' test.txt
grep -in 'the' test.txt (2) Using brackets “[ ]” To find set characters
Find a match “i” perhaps “o” The characters of
grep -n 'sh[io]rt' test.txtlookup “oo” The front is not “w” String
grep -n '[^w]oo' test.txtlookup “oo” Lines with lowercase letters are not allowed in front
grep -n '[^a-z]oo' test.txtFind rows that contain numbers
grep -n '[0-9]' test.txt(3) Find the beginning of a line “^” And end of line characters “$”
Find lines that start with lowercase letters
grep -n '^[a-z]' test.txtFind lines that do not start with letters
grep -n '^[^a-zA-Z]' test.txtLooking to “.” The line at the end
grep -n '\.$' test.txt (4) Find any character “.” And repeating characters “*”
“.” Represents any character
grep -n 'w..d' test.txtThe query contains at least two o The characters above
grep -n 'ooo*' test.txtQuery to w start d ending , At least one in between o String
grep -n 'woo*d' test.txtQuery to w start d ending , The middle character can have a string of optional characters
grep -n 'w.*d' test.txtFind the line of any number
grep -n '[0-9][0-9]*' test.txt(5) Find range of consecutive characters “{ }”
Check two o The characters of
grep -n 'o\{2\}' test.txtQuery to w start d ending , The middle contains 2-5 individual o String
grep -n 'wo\{2,5\}d' test.txtQuery to w start d ending , The middle contains 2 More than o String
grep -n 'wo\{2,\}d' test.txt2、 Extended regular expression (egrep)
Duplicate query o
egrep -n 'wo+d' test.txtInquire about ”tast" perhaps "test" character string
egrep -n 't(a|e)st' test.txtThe blank line and the beginning of the line in the query file are “#” Outside the line
egrep -v '^$|^#' test.txt3、Sed File processor
-e: Represents a text file that uses a specified command or script to process input
-f: Indicates that the input text file is processed with the specified script file
-n: Show only processed results
-i: Edit text file directly
a: increase , Add a line below the current line to specify
c: Replace , Replace the selected row with the specified content
d: Delete . Delete selected rows
i: Insert . Insert a row above the selected row to specify
(1) Output eligible text p Indicates normal output
Output everything
sed -n 'p' test.txtOutput No 3 That's ok
sed -n '3p' test.txtOutput all odd rows ,n Read in the next line
sed -n 'p;n' test.txtOutput No 1-5 Odd rows between rows
sed -n '1,5{p;n}' test.txtOutput No 10 Even lines from line to end of file
sed -n '10,${n;p}' test.txtThe output contains the The line of
sed -n '/the/p' test.txtOutput to PI Beginning line
sed -n '/PI/p' test.txtOutput lines ending in numbers
sed -n '/[0-9]$/p' test.txtOutput contains words wood The line of
sed -n '/\<wood\>/p' test.txt(2) Delete eligible text (d)
Delete the first 3 That's ok
nl test.txt | sed '3d'Delete the first 3-5 That's ok
nl test.txt | sed '3,5d'Delete include cross The line of
nl test.txt | sed '/cross/d'Delete lines that start with lowercase letters
sed '/^[a-z]/d' test.txtDelete with “.” The line at the end
sed '/\.$/d' test.txtDelete all blank lines
sed '/^$/d' test.txt(3) Replace eligible text
First in each line the Replace with THE
sed 's/the/THE/' test.txt
Put all in each line the Replace with THE
sed 's/the/THE/g' test.txt Insert at the beginning of each line # Number
sed 's/^/#/' test.txtInsert a string at the end of each line EOF
sed 's/$/EOF/' test.txt Will include the Of all rows o Replace with O
sed '/the/s/o/O/g' test.txt (4) Migrate eligible text
In the 3 Insert a new row after row , The content is New
sed '3aNew' test.txt Include in the Insert a new row after each row of , The content is New
sed '/the/aNew' test.txt In the 3 Insert multiline content after row , In the middle of the \n Means line break
sed '3aNew1\nNew2' test.txt(5)sed Direct operation of documents
Write a script , Close the firewall and selinux, And configuration IP by 192.168.1.10
vim ip.sh
#!/bin/bash
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i '/dhcp/d' /etc/sysconfig/network-scripts/ifcfg-ens33
sed -i '/UUID/d' /etc/sysconfig/network-scripts/ifcfg-ens33
sed -i '$aIPADDR=192.168.1.10' /etc/sysconfig/network-scripts/ifcfg-ens33
systemctl restart network
chmod +x ip.sh
./ip.sh
Used to adjust vsftpd Service configuration : No anonymous users , But allow local users to ( It is also allowed to write )
#!/bin/bash
SAMPLE="/usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE/vsftpd.conf"
CONFIG="/etc/vsftpd/vsftpd.conf"
[ ! -e "$CONFIG.bak" ] && cp $CONFIG $CONFIG.bak
sed -e '/^anonymous_enable/s/YSE/NO/g' $SAMPLE > $CONFIG
sed -i -e '/^local_enable/s/NO/YES/g' -e '/^write_enable/s/NO/YES/g' $CONFIG
grep "listen" $CONFIG || sed -i '$alisten=YES' $CONFIG
sed -i '$apam_service_name=vsftpd' $CONFIG
sed -i '/^one/s/^/#' $CONFIG
systemctl restart vsftpd
systemctl enable vsftpd
4、awk Tools
(1) Output text by line
Output No 1~3 Row content
awk 'NR==1,NR==3{print}' test.txtOutput No 1 That's ok , The first 3 Row content
awk 'NR==1||NR==3{print}' test.txt// Output to root Beginning line
awk '/^root/{print}' /etc/passwd(2) Output text by field
Output in each line ( Space off ) Of the 1,3 A field
awk '{print $1,$3}' test.txt边栏推荐
- Find() (if the name is used by too many people, I will add words)
- JMeter response time test component & multi interface concurrency
- String Full Permutation Problem
- BeanShell script gets the current time
- Bladex - a well-designed microservice architecture
- [hsjframework] unity time management timemanger timer
- Uni app wechat applet ordering system [another order] page Jump
- Common English business mail phrases
- Leetcode buckle classic question - 42 Connect rainwater
- Shell脚本整数值比较、逻辑测试、if语句、提取性能监控指标
猜你喜欢
随机推荐
String Full Permutation Problem
Dirty reading, unreal reading, non repeatable reading
High quality subroutine
多层数据包结构及TCP三次握手
ENSP静态路由实验
安装.NET提示“无法建立到信任根颁发机构的证书链”(方法简单有下载地址)
Uniapp wechat applet login (authorize wechat first and then mobile phone number) - (1)
Server knowledge (details)
数组、冒泡的认识
描述DNS解析的工作过程
Find() (if the name is used by too many people, I will add words)
MySQL备份和恢复
Make a simple record and check the set
Post man JSON script to JMX script of JMeter
For solopi app performance test
Experience in using flow playback tool Gor
Flask template injection
Understanding: what is interface and the concept of interface
Conditional statement of shell script
Plant a seed and grow into a towering b+ tree ten years later








