【linux相識相知】sed命令

来源:http://www.cnblogs.com/liubinsh/archive/2017/09/13/7516402.html
-Advertisement-
Play Games

作為文本三劍客之一的sed,功能的確的強大,本文介紹一些基本的用法和高級選項,需要在今後的日常使用和工作中不斷的熟悉和鞏固。 ...


在之前的博客中我們介紹了文本三劍客中grep,本次博客就另外一名劍客——sed做出詳細的描述,sed真的是一款強大的工具。下麵讓我們來一起看一下吧!

 

概述和工作機制

SED的英文全稱為Stream EDitor,中文稱流編輯器。預設情況下,它會一行一行的讀取文件中的內容,在瞭解其工作原理之前,首先我們得先知道一下幾個概念:

1.模式空間(pattern buffer):sed從文件中讀取行首先會放到模式空間中進行執行和處理,定義的sed命令都是在這裡執行的,預設情況下會逐行的讀,逐行的處理,除非你做了行定界。

2.保持空間(hold buffer):在處理完模式空間的一些行的時候,我們有可能需要一個臨時的地方去存放模式空間中的內容,這時候就可以將模式空間中的內容放到保持空間了。

初始情況下,模式空間和保持空間都是空的。

1.預設情況下,將從文件中逐行的讀取內容至模式空間;

2.預設情況下,模式空間中的內容在處理完成後將會列印到標準輸出;

3.sed命令在模式空間中的都是按順序執行的,除非指定了行定界,否則將在所有的行上面執行;

4.修改後的行被送至標準輸出的之後,模式空間將被清空。

 

基本選項

 語法格式:

sed [OPTION]... {script-only-if-no-other-script} [input-file]...

-n:不輸出模式空間中的內容至標準輸出

#例子1
[root@localhost ~]# sed '' /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Wed Sep  6 11:16:57 2017
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/cl-root     /                       xfs     defaults        0 0
UUID=0ab24855-5180-4d3e-a61d-99ca54711c2c /boot                   xfs     defaults        0 0
/dev/mapper/cl-swap     swap                    swap    defaults        0 0
[root@localhost ~]# 
#例子2
[root@localhost ~]# sed -n '' /etc/fstab 
[root@localhost ~]#

預設情況下,sed命令會逐行的讀取文件中每一行內容至模式空間,再執行執行單引號內的命令,如例子1,單引號內沒有任何內容,所以不會對行內容作任何的處理,所以sed會逐行讀取文件的內容,然後逐行的顯示到標準輸出,每列印一行到標準輸出,模式空間就會被清空一次。在例子2中,加了-n的選項,會阻止輸出到標準輸出,所以就不會顯示任何的內容。

 -e script:指定多個命令

[root@localhost ~]# cat poetry 
1)Never give up,  
2)Never lose hope.  
3)Always have faith,  
4)It allows you to cope.  
5)Trying times will pass,  
6)As they always do.  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength you will gain
#例子3
[root@localhost ~]# sed -e '1d' -e '10d' poetry 
2)Never lose hope.  
3)Always have faith,  
4)It allows you to cope.  
5)Trying times will pass,  
6)As they always do.  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
11)Know it will pass,  
12)And strength you will gain 

使用-e選項,可以同時對行作出多個模式匹配,d的意思是刪除。這裡的例子3,文件poetry是輸入,sed讀取第一行的內容至模式1空間,然後判斷是否是第一行,判斷成功是第一行,則刪除第一行(從模式空中清除,也不會列印至標準輸出),讀取第二行,先判斷是否是第一行,不是,再判斷是否是第十行,不是,然後列印至標準輸出,所以在標準輸出中只顯示了不包含第一行和第十行的其他行的內容。註意:sed不會修改原文件的內容。

-f  /path/to/script:把sed的編輯命令放在文件中,註意每一行一個命令:

#例子4
[root@localhost ~]# cat sed_script.txt    #編輯命令存放的文本文件
1d
10d
[root@localhost ~]# sed -f sed_script.txt poetry 
2)Never lose hope.  
3)Always have faith,  
4)It allows you to cope.  
5)Trying times will pass,  
6)As they always do.  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
11)Know it will pass,  
12)And strength you will gain 

我們可以看到例子4中sed使用-f選項指定編輯命令所在的文件,執行的是和例子3同樣的操作,刪除第一行和第十行,當我們得編輯命令有很多,把它放在文件也是一個不錯的做法吧!

 

行定界

預設情況下不指定行的定界,會讀取處理整個文件的每一行。如果指定了行,還是會讀取每一行,但是在執行各種命令操作之後,會進行判斷是不是定界的行,如果判斷成功,則執行操作,如刪除行,如果判斷失敗,則不做任何的處理然後列印至標準輸出,下麵來看一下幾種常見的定界方法:

(1)定界為空,對全文的每一行進行處理

[root@localhost ~]# sed  '' poetry 
1)Never give up,  
2)Never lose hope.  
3)Always have faith,  
4)It allows you to cope.  
5)Trying times will pass,  
6)As they always do.  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength you will gain 
(2)單行指定 a.直接寫單個數字,表示指定行
[root@localhost ~]# sed  '2d' poetry  #當判斷為第二行的時候,會刪除第二行
1)Never give up,  
3)Always have faith,  
4)It allows you to cope.  
5)Trying times will pass,  
6)As they always do.  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength you will gain

b./pattern/:被模式匹配到的行,預設可以是基本的正在表達式

[root@localhost ~]# sed  '/As/ d' poetry  #刪除被模式匹配的行
1)Never give up,  
2)Never lose hope.  
3)Always have faith,  
4)It allows you to cope.  
5)Trying times will pass,  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength you will gain 

c.$最後一行

[root@localhost ~]# sed  '$ d' poetry  #刪除最後一行
1)Never give up,  
2)Never lose hope.  
3)Always have faith,  
4)It allows you to cope.  
5)Trying times will pass,  
6)As they always do.  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
10)You'll live through your pain.  
11)Know it will pass,

(3)定界範圍

a.x,y:x至y行

[root@localhost ~]# sed  '1,9 d' poetry  #刪除了1-9行
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength you will gain 

b.x,+y:x行到,x行往下數y個行

[root@localhost ~]# sed  '1,+3 d' poetry #刪除第一行,和第一行往下的3行,也就是一直刪除到第四行
5)Trying times will pass,  
6)As they always do.  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength you will gain 

c.x,/pattren/:x到被模式匹配的行

[root@localhost ~]# sed  '2,/smile/ d' poetry  #刪除第二行至被smile匹配的行,smile被第九行匹配
1)Never give up,  
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength you will gain 

d./pattren1/,/pattern2/:被模式1匹配的行開始到被模式2匹配的行

[root@localhost ~]# sed '/Always/,/put/ d' poetry 
1)Never give up,  
2)Never lose hope.  
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength you will gain 
(4)使用"~"指定步進 "~"號前面為開始行,後面為步進的長度:
[root@localhost ~]# sed '1~2 d' poetry  #顯示偶數行
2)Never lose hope.  
4)It allows you to cope.  
6)As they always do.  
8)Your dreams will come true.  
10)You'll live through your pain.  
12)And strength you will gain 
[root@localhost ~]# sed '2~2 d' poetry   #顯示奇數行
1)Never give up,  
3)Always have faith,  
5)Trying times will pass,  
7)Just have patience,  
9)So put on a smile,  
11)Know it will pass, 

 

編輯命令

下麵我們來看一下編輯命令,如前面的d選項是刪除行的意思,那麼除了d選項,還有哪些呢?

p:顯示模式空間的內容

#例5
[root@localhost ~]# sed   'p' poetry 
1)Never give up,  
1)Never give up,  
2)Never lose hope.  
2)Never lose hope.  
3)Always have faith,  
3)Always have faith,  
4)It allows you to cope.  
4)It allows you to cope.  
5)Trying times will pass,  
5)Trying times will pass,  
6)As they always do.  
6)As they always do.  
7)Just have patience,  
7)Just have patience,  
8)Your dreams will come true.  
8)Your dreams will come true.  
9)So put on a smile,  
9)So put on a smile,  
10)You'll live through your pain.  
10)You'll live through your pain.  
11)Know it will pass,  
11)Know it will pass,  
12)And strength you will gain 
12)And strength you will gain 
#例6
[root@localhost ~]# sed -n  'p' poetry 
1)Never give up,  
2)Never lose hope.  
3)Always have faith,  
4)It allows you to cope.  
5)Trying times will pass,  
6)As they always do.  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength you will gain 
View Code

在例5中,每一行都被列印了2次,原因是在模式空間的時候被列印了一次,然後處理完成之後,在標準輸出又被列印了一次,所以自然會顯示出兩次。如果使用-n,則僅僅列印的模式空間的內容。

a test:在行後面追加文本"test",可以使用\n實現多行追加

[root@localhost ~]# sed '1 a hi i am here\nhey ' poetry 
1)Never give up,  
hi i am here
hey 
2)Never lose hope.  
3)Always have faith,  
4)It allows you to cope.  
5)Trying times will pass,  
6)As they always do.  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength you will gain 

i test:在 行前面插入文本test,支持使用\n實現多行插入

[root@localhost ~]# sed '2i  hi i am here\nhey ' poetry 
1)Never give up,  
hi i am here
hey 
2)Never lose hope.  
3)Always have faith,  
4)It allows you to cope.  
5)Trying times will pass,  
6)As they always do.  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength you will gain 

c test:把匹配到的行替換為此處指定的行

[root@localhost ~]# sed '2c  hi i am here ' poetry 
1)Never give up,  
hi i am here 
3)Always have faith,  
4)It allows you to cope.  
5)Trying times will pass,  
6)As they always do.  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength you will gain 

w /path/to/somefile:保存模式空間中匹配到的內容到指定的文件中

[root@localhost ~]# sed '/So/  w  /tmp/poetry' poetry 
1)Never give up,  
2)Never lose hope.  
3)Always have faith,  
4)It allows you to cope.  
5)Trying times will pass,  
6)As they always do.  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength you will gain 
[root@localhost ~]# 
[root@localhost ~]# cat /tmp/poetry 
9)So put on a smile,  

r /path/from/somefile:讀取指定文件的內容至當前文件中被模式匹配的行後面

[root@localhost ~]# cat poetry2.txt 
13)yes! I can!
[root@localhost ~]# sed '12 r poetry2.txt' poetry
1)Never give up,  
2)Never lose hope.  
3)Always have faith,  
4)It allows you to cope.  
5)Trying times will pass,  
6)As they always do.  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength you will gain 
13)yes! I can!

=:為模式匹配到的行列印行號,列印在行的上面

[root@localhost ~]# sed '/Never/ =' poetry
1
1)Never give up,  
2
2)Never lose hope.  
3)Always have faith,  
4)It allows you to cope.  
5)Trying times will pass,  
6)As they always do.  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength you will gain 

!:條件取反

[root@localhost ~]# sed '/Never/ !d' poetry  #不刪除被預設匹配的行
1)Never give up,  
2)Never lose hope.  

s///:查找替換,分割符可以自定義,比如使用s@@@flag或者s###flag,預設會替換被匹配的第一個位置,但是可以添加替換標記:

s///g:全局替換

[root@localhost ~]# cat poetry
1)Never give up,  
2)Never lose hope.  
3)Always have faith,  
4)It allows you to cope.  
5)Trying times will pass,  
6)As they always do.  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength you will gain 
[root@localhost ~]# sed 's/\<you\>/your/g' poetry
1)Never give up,  
2)Never lose hope.  
3)Always have faith,  
4)It allows your to cope.  
5)Trying times will pass,  
6)As they always do.  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength your will gain 
View Code

sed預設支持的基本的正則表達式,使用-r, --regexp-extended選項可以支持擴展的正則表達式。

 

練習1:刪除/boot/grub2/grub.cfg文件中所有以空白字元開頭的行的行首的所有空白字元

[root@localhost ~]# sed -r 's@^[[:space:]]+@@g' /boot/grub2/grub.cfg
View Code

練習2:刪除/etc/fstab文件中所有以#開頭的行的行首的#號及#後面的所有空白字元

[root@localhost ~]# sed 's/^#[[:space:]]*//g' /etc/fstab
View Code

練習3:輸出一個目錄給sed,取出其目錄,類似於dirname

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

 

 高級編輯命令

 sed還有一些高級的命令,會用到之前說到的保持空間。

 

舉一些例子來看一下吧!

[root@localhost ~]# cat poetry
1)Never give up,  
2)Never lose hope.  
3)Always have faith,  
4)It allows you to cope.  
5)Trying times will pass,  
6)As they always do.  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength you will gain 

可以自己先思考一下答案,再點開:

[root@localhost ~]# sed -n 'n;p' poetry  #只顯示偶數行
2)Never lose hope.  
4)It allows you to cope.  
6)As they always do.  
8)Your dreams will come true.  
10)You'll live through your pain.  
12)And strength you will gain 
sed -n 'n;p' poetry
[root@localhost ~]# sed  '1!G;h;$!d'  poetry  #逆序顯示
12)And strength you will gain 
11)Know it will pass,  
10)You'll live through your pain.  
9)So put on a smile,  
8)Your dreams will come true.  
7)Just have patience,  
6)As they always do.  
5)Trying times will pass,  
4)It allows you to cope.  
3)Always have faith,  
2)Never lose hope.  
1)Never give up, 
sed '1!G;h;$!d' poetry
[root@localhost ~]# sed  '$!d'  poetry  #只保留最後一行,類似tail -1
12)And strength you will gain 
sed '$!d' poetry
[root@localhost ~]# sed  '$!N;$!D' poetry  #取出最後兩行
11)Know it will pass,  
12)And strength you will gain 
sed '$!N;$!D' poetry
[root@localhost ~]# sed 'G' poetry  #在每一行的後面添加一個空白行
1)Never give up,  

2)Never lose hope.  

3)Always have faith,  

4)It allows you to cope.  

5)Trying times will pass,  

6)As they always do.  

7)Just have patience,  

8)Your dreams will come true.  

9)So put on a smile,  

10)You'll live through your pain.  

11)Know it will pass,  

12)And strength you will gain 
sed 'G' poetry

這些都是一些高級的用法,在工作中用的可能比較少,因為有的結果完全可以用其他簡單的命令去實現。

 

迴圈和分支

 sed最牛逼之處莫過於這個了,sed提供了一個迴圈和分支工來控製程序的執行流程。

 迴圈

sed迴圈的工作原理類似於現代編程語言中的goto語句。

定義sed的標簽:

:label

跳轉到標簽的位置,使用b命令後面跟著標簽名即可,下麵我們來看一個具體的實例:

[root@localhost ~]# cat teleplay 
劇名:白夜追凶
評分:9.0
劇名:秘密深林
評分:9.3
劇名:權利的游戲第七季
評分:9.3
劇名:請回答1988
評分:9.6
[root@localhost ~]# sed -n '
h;n;H;x
s@\n@,@
/請回答/!b label
s@^@---@
:label
p' teleplay
劇名:白夜追凶,評分:9.0
劇名:秘密深林,評分:9.3
劇名:權利的游戲第七季,評分:9.3
---劇名:請回答1988,評分:9.6

分析:

由於命令很長,我們可以將命令分行來寫,當然也可以寫在一行裡面:

1.h;n;H;x:將第一行內容讀取到模式空間,執行h,將模式空間的內容覆蓋至保持空間,執行n,從文件中讀取第二行覆蓋至模式空間,執行H,將模式空間的內容追加至保持空間,執行x,將保持空間的內容和模式空間調換;(這裡得到的結果就是模式空間中存在兩行);

2.s@\n@,@,將換行符替換為逗號;這樣原來的兩行就變成了一行;

3./請回答/!b label,判斷這一行中是否有“請回答”三個字元,如果沒有則直接跳到":label",然後執行列印"p",列印模式空間中的內容,如果有則執行"s@^@---@",將在行首添加字元"---"。

這樣一來,只有"劇名:請回答1988,評分:9.6"被匹配,然後進行了行首的替換操作。

 

分支

 可以使用t來創建分支。使用t命令跳轉到指定的標簽,一樣我們來看一個例子:

[root@localhost ~]# sed -n '
> h;n;H;x
> s/\n/,/
> :loop
> /白夜追凶/s/^/-/
> /-----/!t loop
> p' teleplay
-----劇名:白夜追凶,評分:9.0
劇名:秘密深林,評分:9.3
劇名:權利的游戲第七季,評分:9.3
劇名:請回答1988,評分:9.6

分析:
由於命令很長,我們可以將命令分行來寫,當然也可以寫在一行裡面:

1:h;n;H;x:將第一行內容讀取到模式空間,執行h,將模式空間的內容覆蓋至保持空間,執行n,從文件中讀取第二行覆蓋至模式空間,執行H,將模式空間的內容追加至保持空間,執行x,將保持空間的內容和模式空間調換;(這裡得到的結果就是模式空間中存在兩行);

2:s@\n@,@,將換行符替換為逗號;這樣原來的兩行就變成了一行;

3::loop:定義了一個loop標簽;

4:/白夜追凶/s/^/-/:是否匹配模式,如果匹配則在其行首添加一個"-";如果不匹配,則直接列印;

5:/-----/!t loop:是否存在5個"-",如果不存在,則跳到標簽loop處,繼續執行第4步添加"-",直到滿足5個"-",則跳出迴圈列印。

最後,我們之前的所有的操作,都是沒有修改文件的本身的,可以使用 -i 選項來直接修改文件本身,慎用,建議在使用之前,先備份一下文件!

[root@localhost ~]# sed -i '1d' poetry
[root@localhost ~]# 
[root@localhost ~]# cat poetry
2)Never lose hope.  
3)Always have faith,  
4)It allows you to cope.  
5)Trying times will pass,  
6)As they always do.  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength you will gain 

總結:sed的確是一個強大的文本處理工具,功能非常豐富,需要在今後的日常使用和工作中不斷的熟悉和鞏固。

 

參考鏈接:http://blog.jobbole.com/109088/     《三分鐘學會SED》寫的非常好!

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 繼續第三天學習,每天下班後積累一點點,始終相信厚積薄發。 一、處理文件的命令 touch dest_file:在當前目錄下創建指定的文件。 cp source dest:將指定的猿文件複製到目標文件,並且以dest命名。雖然說這個命令有點重命名文件的影子,但是區別就是目標文件有新的修改時間。 cp ...
  • RHEL(Oracle Linxu/CentOS)系統下,如果使用sendmail發送郵件,如果不特殊設置,一般發件箱地址為user@hostname,例如,hostname為DB-Server.localdomain,在root用戶下使用下麵命令發送一封郵件: [root@DB-Server ~]... ...
  • nginx編譯安裝步驟 ①. 檢查軟體安裝的系統環境 cat /etc/redhat-release uname -r ②. 安裝nginx的依賴包(pcre-devel openssl-devel) 假設不進行安裝 yum install -y pcre-devel openssl-devel ③ ...
  • 我們可以把路由器比作網路世界的骨架,我們之所以能夠在網路世界里暢游,很大程度上是得益於這個鐵盒子。 路由器硬體架構 隨著專用多核網路處理器、專用轉發晶元的出現,使得現代路由器擺脫了以往純軟體轉發的局限,向著高吞吐率、硬體快速轉發等方向發展。高端的路由器設計成多板分散式+冗餘備份的架構,使轉發能力成倍 ...
  • 本文目錄: 1.幾個顯示函數2.action函數3.is_true和is_false函數4.confirm函數5.pid檢測相關函數 5.1 checkpid、__pids_var_run和__pids_pidof函數 5.2 pidfileofproc和pidofproc函數6.重頭戲(一):da ...
  • 原文發表於cu:2016-07-04 參考文檔: http://seanlook.com/2015/01/21/openldap-install-guide-ssl/ 一.環境 Server:基於CentOS-7-x86_64-1511 Server IP: 172.18.12.203 OpenLD ...
  • 依賴包和常用包yum install gcc gcc-c++ make zlib-devel readline readline-devel tkutil tk tkutil-devel tk-devel openssl openssl-devel wget vim ntp -y下載rediswge ...
  • CentOS7自帶的SSH服務是OpenSSH中的一個獨立守護進程SSHD。由於使用telnet在網路中是明文傳輸所以用其管理伺服器是非常不安全的不安全,SSH協議族可以用來對伺服器的管理以及在電腦之間傳送文件。 一、配置文件 伺服器配置文件 /etc/ssh/sshd_config 日誌文件 / ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...