sed的替換可用:斜杠/,豎或|,井號# 等符號, 但是查找只能用斜杠/ , sed的查找和替換可以一起用 筆記240711

sed的替換可用:斜杠/,豎或|,井號# 等符號, 但是… … 查找只能用斜杠/

替換必須用s開頭, 如:s/ , s| , s#

例如:

  • s/正則/替換內容/
  • s/正則/替換內容/g
  • s|正則|替換內容|
  • s|正則|替換內容|g
  • s#正則#替換內容#
  • s#正則#替換內容#g

當內容包含斜杠/時, (例如路徑) , 使用 豎或|,井號# 比較方便, 可以不用轉義路徑分隔符斜杠/


與替換相比, 查找只能用斜杠 / , ???sed '/hello/ 不能寫成 sed ‘|hello|’ 或 sed ‘#hello#’

ip addr|sed '/inet /' 效果類似 ip addr|grep 'inet '



sed的查找和替換可以一起用

sed的查找和替換可以一起用, 先用查找過濾一部分內容, 再在剩余的內容中執行替換.

查找只能用/ , 例如:
將所有包含"hello"的行中的"world"替換成"世界" , 可寫成:

  • /hello/s/world/世界/
  • /hello/s/world/世界/g
  • /hello/s|world|世界|
  • /hello/s|world|世界|g
  • /hello/s#world#世界#
  • /hello/s#world#世界#g

實測:

tempStringVar="$(echo -e "
hello world world world 
world world world world 
hello world world world 
world world world world 
hello world world world 
world world world world 
")"
echo "${tempStringVar}" | sed   '/hello/s/world/世界/'
echo "${tempStringVar}" | sed   '/hello/s/world/世界/g'
echo "${tempStringVar}" | sed   '/hello/s|world|世界|'
echo "${tempStringVar}" | sed   '/hello/s|world|世界|g'
echo "${tempStringVar}" | sed   '/hello/s#world#世界#'
echo "${tempStringVar}" | sed   '/hello/s#world#世界#g'

結果:

[root@1235vm-c69w yum.repos.d]# tempStringVar="$(echo -e "
> hello world world world 
> world world world world 
> hello world world world 
> world world world world 
> hello world world world 
> world world world world 
> ")"
[root@1235vm-c69w yum.repos.d]# echo "${tempStringVar}" | sed   '/hello/s/world/世界/'hello 世界 world world 
world world world world 
hello 世界 world world 
world world world world 
hello 世界 world world 
world world world world 
[root@1235vm-c69w yum.repos.d]# echo "${tempStringVar}" | sed   '/hello/s/world/世界/g'hello 世界 世界 世界 
world world world world 
hello 世界 世界 世界 
world world world world 
hello 世界 世界 世界 
world world world world 
[root@1235vm-c69w yum.repos.d]# echo "${tempStringVar}" | sed   '/hello/s|world|世界|'hello 世界 world world 
world world world world 
hello 世界 world world 
world world world world 
hello 世界 world world 
world world world world 
[root@1235vm-c69w yum.repos.d]# echo "${tempStringVar}" | sed   '/hello/s|world|世界|g'hello 世界 世界 世界 
world world world world 
hello 世界 世界 世界 
world world world world 
hello 世界 世界 世界 
world world world world 
[root@1235vm-c69w yum.repos.d]# echo "${tempStringVar}" | sed   '/hello/s#world#世界#'hello 世界 world world 
world world world world 
hello 世界 world world 
world world world world 
hello 世界 world world 
world world world world 
[root@1235vm-c69w yum.repos.d]# echo "${tempStringVar}" | sed   '/hello/s#world#世界#g'hello 世界 世界 世界 
world world world world 
hello 世界 世界 世界 
world world world world 
hello 世界 世界 世界 
world world world world 

實測2

tempStringVar="
hello world world world 
world world world world "
echo "${tempStringVar}" | sed   '/hello/s/world/世界/'
echo "${tempStringVar}" | sed   '/hello/s/world/世界/g'
echo "${tempStringVar}" | sed   '/hello/s|world|世界|'
echo "${tempStringVar}" | sed   '/hello/s|world|世界|g'
echo "${tempStringVar}" | sed   '/hello/s#world#世界#'
echo "${tempStringVar}" | sed   '/hello/s#world#世界#g'

結果

[root@1235vm-c69w yum.repos.d]# tempStringVar="
> hello world world world 
> world world world world "
[root@1235vm-c69w yum.repos.d]# echo "${tempStringVar}" | sed   '/hello/s/world/世界/'hello 世界 world world 
world world world world 
[root@1235vm-c69w yum.repos.d]# echo "${tempStringVar}" | sed   '/hello/s/world/世界/g'hello 世界 世界 世界 
world world world world 
[root@1235vm-c69w yum.repos.d]# echo "${tempStringVar}" | sed   '/hello/s|world|世界|'hello 世界 world world 
world world world world 
[root@1235vm-c69w yum.repos.d]# echo "${tempStringVar}" | sed   '/hello/s|world|世界|g'hello 世界 世界 世界 
world world world world 
[root@1235vm-c69w yum.repos.d]# echo "${tempStringVar}" | sed   '/hello/s#world#世界#'hello 世界 world world 
world world world world 
[root@1235vm-c69w yum.repos.d]# echo "${tempStringVar}" | sed   '/hello/s#world#世界#g'hello 世界 世界 世界 
world world world world

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/44352.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/44352.shtml
英文地址,請注明出處:http://en.pswp.cn/web/44352.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

06_TypeScript 中的函數

TypeScript 中的函數 一、函數的定義es5 定義函數的方法TypeScript 定義函數的方法ts 中定義方法傳參 二、可選參數三、默認參數(跟可選參數一樣)四、剩余參數(三點運算符的應用)五、函數重載六、箭頭函數 es6 一、函數的定義 es…

Windows圖形界面(GUI)-SDK-C/C++ - 編輯框(edit)

公開視頻 -> 鏈接點擊跳轉公開課程博客首頁 -> 鏈接點擊跳轉博客主頁 目錄 編輯框(edit) 控件樣式 創建控件 初始控件 消息處理 示例代碼 編輯框(edit) 控件樣式 編輯框(Edit Control)是Windows中最常用的控件之一,用于接收用戶…

python 知識點累積

.items() tinydict {Google: www.google.com, Runoob: www.runoob.com, taobao: www.taobao.com}print (tinydict.items())運行結果 dict_items([(Google, www.google.com), (Runoob, www.runoob.com), (taobao, www.taobao.com)]) 返回的 dict_items([...]) 是一個字典項的…

【持續集成_05課_Linux部署SonarQube及結合開發項目部署】

一、Linux下安裝SonarQube 1、安裝sonarQube 前置條件:sonarQube不能使用root賬號進行啟動,所以需要創建普通用戶及 其用戶組 1)創建組 2)添加用戶、組名、密碼 3)CMD上傳qube文件-不能傳到home路徑下哦 4&#xff09…

王牌站士Ⅳ--矢量數據庫對 RAG 效率的影響

前言 近年來,檢索增強生成 (RAG) 模型越來越受歡迎。RAG 模型利用大型神經網絡以及外部知識源的檢索機制。這使得模型擁有的知識比其內部存儲的更多,從而使其能夠為廣泛的主題和領域生成高質量的輸出。 影響 RAG 模型性能的關鍵因素之一是從外部源檢索相…

C++之STL簡介

提示:文章寫完后,目錄可以自動生成,如何生成可參考右邊的幫助文檔 文章目錄 前言一、STL是什么二、STL的發展歷史三、STL的組成部分3.1 容器(Containers)3.2 迭代器(Iterators)3.3 算法&#xf…

【啟明智顯分享】ESP32-S3 4.3寸觸摸串口屏HMI應用方案:WIFI/藍牙無線通信助力烘干設備實現遠程遙控

技術不斷進步,人們對烘干設備的美觀度、功能多樣性提出更高要求,傳統的數碼管顯示、按鍵式控制已經無法滿足客戶的需求。用智能屏替代傳統的數碼管可以很好的解決這個問題,為用戶帶來更好的人機交互體驗。 基于此,啟明智顯提出將樂…

solidity實戰練習2--ERC20實現

//SPDX-License-Identifier:MTT pragma solidity ^0.8.24;interface erc20{function name()external view returns (string memory);//查看代幣的名稱function symbol() external view returns(string memory);//查看代幣簡稱function totalSupply()external view returns(uint…

低代碼:架起產教融合的“立交橋”

隨著信息技術的飛速發展,傳統的軟件開發模式正面臨著前所未有的挑戰。為了應對這一挑戰,低代碼平臺應運而生,其通過簡化開發流程、提高開發效率,成為了推動產業與教育深度融合的重要力量。本文將探討低代碼平臺如何架起產教融合的…

C++基礎(十八):繼承(重點)

各位看官,大家好!今天我們將探討C中的三大特性之一:繼承。繼承是一種面向對象編程的重要概念,它允許我們通過創建新的類,從而復用和擴展現有類的功能。通過繼承,我們不僅能夠提高代碼的可重用性和可維護性&…

算法刷題筆記 KMP字符串(C++實現,并給出了求next數組的獨家簡單理解方式)

文章目錄 題目描述基本思路實現代碼 題目描述 給定一個字符串S,以及一個模式串P,所有字符串中只包含大小寫英文字母以及阿拉伯數字。模式串P在字符串S中多次作為子串出現。求出模式串P在字符串S中所有出現的位置的起始下標。 輸入格式 第一行輸入整數…

docker拉取鏡像,報錯error pulling image configuration: download failed after attempts=6: dial tcp 157.240.1

error pulling image configuration: download failed after attempts6: dial tcp 157.240.10.32:443: i/o timeout docker compose pull docker pull langgenius/dify-web:0.6.13 重啟docker sudo systemctl restart dockerhttps://stackoverflow.com/questions/72353203/do…

9.5 柵格圖層符號化多波段彩色渲染

文章目錄 前言多波段彩色渲染QGis設置為多波段彩色二次開發代碼實現多波段彩色 總結 前言 介紹柵格圖層數據渲染之多波段彩色渲染說明:文章中的示例代碼均來自開源項目qgis_cpp_api_apps 多波段彩色渲染 以“3420C_2010_327_RGB_LATLNG.tif”數據為例&#xff0c…

代碼隨想錄打卡第二十一天

代碼隨想錄–二叉樹部分 day 21 二叉樹第八天 文章目錄 代碼隨想錄--二叉樹部分一、力扣669--修建二叉搜索樹二、力扣108--將有序數組轉換為二叉搜索樹三、力扣538--把二叉搜索樹轉換為累加樹 一、力扣669–修建二叉搜索樹 代碼隨想錄題目鏈接:代碼隨想錄 給你二叉…

常見條件控制算法流程圖

內容講解:流程控制[if…else…(if…elif…else…),while,for] 常見條件控制算法流程圖高清圖

新手教學系列——高效管理MongoDB數據:批量插入與更新的實戰技巧

前言 在日常開發中,MongoDB作為一種靈活高效的NoSQL數據庫,深受開發者喜愛。然而,如何高效地進行數據的批量插入和更新,卻常常讓人頭疼。今天,我們將一起探討如何使用MongoDB的bulk_write方法,簡化我們的數據管理流程,讓代碼更加簡潔高效。 常規做法:find、insertone…

Unity 之 抖音小游戲集成排行榜功能詳解

Unity 之 抖音小游戲集成排行榜功能詳解 一,前言1.1 為游戲設計利于傳播的元素?2.2 多人競技、社交傳播?二,集成說明2.1 功能介紹2.2 完整代碼2.3 效果展示三,發現的問題和迭代計劃一,前言 對于 Unity 開發者而言,在開發抖音小游戲時集成排行榜功能是提升游戲社交性和玩…

Java實戰中處理高并發的策略

引言 隨著互聯網的快速發展,高并發成為了許多應用必須面對的挑戰。Java作為一門廣泛應用于企業級開發的語言,提供了豐富的工具和技術來應對高并發問題。本文將詳細探討Java中處理高并發的幾種常見策略和技術。 1. 并發編程基礎 1.1 線程與線程池 Jav…

【TVM 教程】使用 TVM 部署框架預量化模型

本文介紹如何將深度學習框架量化的模型加載到 TVM。預量化模型的導入是 TVM 中支持的量化之一。有關 TVM 中量化的更多信息,參閱 此處。 這里演示了如何加載和運行由 PyTorch、MXNet 和 TFLite 量化的模型。加載后,可以在任何 TVM 支持的硬件上運行編譯…

【Linux】常見指令收官權限理解

tar指令 上一篇博客已經介紹了zip/unzip指令,接下來我們來看一下另一個關于壓縮和解壓的指令:tar指令tar指令:打包/解包,不打開它,直接看內容 關于tar的指令有太多了: tar [-cxtzjvf] 文件與目錄 ...…