Linux:在文件中查找指定內容並輸出到文件


1 滿足一個條件

例如:將文件 file1 中包含 name 的行輸出到 file2.

grep 'name' file1 > file2
# 或者
cat file1 | grep 'name' > file2

2 滿足兩個條件任意一個

例如:將文件 file1 中包含 name 或者 age 的行輸出到 file2.

egrep 'name|age' file1 > file2
# 或者
grep -E 'name|age' file1 > file2
# 或者
cat file1 | grep -E 'name|age' > file2

3 同時滿足兩個條件

例如:將文件 file1 中包含 name 和 age 的行輸出到 file2.

grep 'name' file1 | grep 'age' > file2
# 或者
cat file1 | grep 'name' | grep 'age' > file2

注意:符號“>”表示擦除文件原內容並寫入;“>>”表示追加內容。

4 命令 grep 和 tee 搭配使用

grep 'name' file1 | tee -a file2

注意:-a 表示追加內容,不加此參數則表示:擦除原內容並寫入,


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM