屬性 : 系統相關 - 文書處理
語法 : sed [參數] [語法] [檔案名稱]
參數 | 功能
sed 的語法眾多且複雜, 因此這裡只列出兩個常用語法如下 :
'[範圍][動作]/[源字串]/[新字串]/[動作]'
'[字串][動作]/[源字串]/[新字串]/[動作]'
執行範例 :
以下範例將以 testfile 為例說明, testfile內容如下 :
* 將 testfile 的2-3行刪除.
john:~/test2 # sed '2,3d' testfile
start of line
this is line2
this is line3
this is line3
end of line
* 將內容中的第一個 is 字串換程 paper
john:~/test2 # sed 's/is/paper/' testfile
start of line
thpaper is line1
thpaper is line2
thpaper is line2
thpaper is line3
thpaper is line3
end of line
john:~/test2 # sed 's/is/paper/g' testfile <將所有 is 都替換成 paper>
start of line
thpaper paper line1
thpaper paper line2
thpaper paper line2
thpaper paper line3
thpaper paper line3
end of line
* 將含有2 的該行內容中, 若是有 line 這個字串, 全部替換成 special
john:~/test2 # sed '/2/s/line/special/g' testfile
start of line
this is line1
this is special2
this is special2
this is line3
this is line3
end of line
* 把含有 is 這個字串的列全部刪除
john:~/test2 # sed '/is/d' testfile
start of line
end of line
* 把 e 開頭的列全部刪除
john:~/test2 # sed '/^e/d' testfile <如果為 '/^$/d' 即為把所有空白列刪除>
start of line
this is line1
this is line2
this is line2
this is line3
this is line3
* 把含有 3 的字元的列全部列印出來
john:~/test2 # sed -n '/3/p' testfile <-n 是抑制預設秀出全部的動作>
this is line3
this is line3
* 把每一列的頭三個字刪除
john:~/test2 # sed 's/^...//' testfile <'^' 是指開頭, '$'則是結尾, 更多請參考正則表示式>
rt of line
s is line1
s is line2
s is line2
s is line3
s is line3
of line
* 將 is 替換成 is a (不含 this 的is)
john:~/test2 # sed 's/\( is\)/\1 a/g' testfile <把找到的 is 存起來,用 \1 取回來使用, 並使用 ' is' 來區分 this 的 is>
start of line
this is a line1
this is a line2
this is a line2
this is a line3
this is a line3
end of line
* 將 2~4列的 line 字串拿掉
john:~/test2 # sed '2,4s/line//' testfile
start of line
this is 1
this is 2
this is 2
this is line3
this is line3
end of line
補充說明 :
@. sed 與 awk 是 Linux 命令列下強大指令, 對於文書編輯與管理, 都是好用的東西.
This is a blog to track what I had learned and share knowledge with all who can take advantage of them
標籤
- [ 英文學習 ]
- [ 計算機概論 ]
- [ 深入雲計算 ]
- [ 雜七雜八 ]
- [ Algorithm in Java ]
- [ Data Structures with Java ]
- [ IR Class ]
- [ Java 文章收集 ]
- [ Java 代碼範本 ]
- [ Java 套件 ]
- [ JVM 應用 ]
- [ LFD Note ]
- [ MangoDB ]
- [ Math CC ]
- [ MongoDB ]
- [ MySQL 小學堂 ]
- [ Python 考題 ]
- [ Python 常見問題 ]
- [ Python 範例代碼 ]
- [心得扎記]
- [網路教學]
- [C 常見考題]
- [C 範例代碼]
- [C/C++ 範例代碼]
- [Intro Alg]
- [Java 代碼範本]
- [Java 套件]
- [Linux 小技巧]
- [Linux 小學堂]
- [Linux 命令]
- [ML In Action]
- [ML]
- [MLP]
- [Postgres]
- [Python 學習筆記]
- [Quick Python]
- [Software Engineering]
- [The python tutorial]
- 工具收集
- 設計模式
- 資料結構
- ActiveMQ In Action
- AI
- Algorithm
- Android
- Ansible
- AWS
- Big Data 研究
- C/C++
- C++
- CCDH
- CI/CD
- Coursera
- Database
- DB
- Design Pattern
- Device Driver Programming
- Docker
- Docker 工具
- Docker Practice
- Eclipse
- English Writing
- ExtJS 3.x
- FP
- Fraud Prevention
- FreeBSD
- GCC
- Git
- Git Pro
- GNU
- Golang
- Gradle
- Groovy
- Hadoop
- Hadoop. Hadoop Ecosystem
- Java
- Java Framework
- Java UI
- JavaIDE
- JavaScript
- Jenkins
- JFreeChart
- Kaggle
- Kali/Metasploit
- Keras
- KVM
- Learn Spark
- LeetCode
- Linux
- Lucene
- Math
- ML
- ML Udemy
- Mockito
- MPI
- Nachos
- Network
- NLP
- node js
- OO
- OpenCL
- OpenMP
- OSC
- OSGi
- Pandas
- Perl
- PostgreSQL
- Py DS
- Python
- Python 自製工具
- Python Std Library
- Python tools
- QEMU
- R
- Real Python
- RIA
- RTC
- Ruby
- Ruby Packages
- Scala
- ScalaIA
- SQLAlchemy
- TensorFlow
- Tools
- UML
- Unix
- Verilog
- Vmware
- Windows 技巧
- wxPython
2010年9月14日 星期二
訂閱:
張貼留言 (Atom)
[Git 常見問題] error: The following untracked working tree files would be overwritten by merge
Source From Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 # git clean -d -fx 方案2: 今天在服务器上 gi...
沒有留言:
張貼留言