1 問題
1)合并2個檔案,這裡用paste指令
2)找到比對的檔案裡面替換内容,這裡用find 和 -exec或xargs指令組合
2 實作
1)合并2個檔案,這裡用paste指令,我們在paste後面加參數-d 然後加" ",表示檔案之間内容隔着空格,“,”表示檔案之間内容隔着内容
cat 1.txt
1
2
3
4
5
6
cat 2.txt
chenyu
chengongyu
hello
paste 1.txt 2.txt -d ','
1,chenyu
2,chengongyu
3,hello
4,
5,
6,
paste 1.txt 2.txt -d ' '
1 chenyu
2 chengongyu
3 hello
4
5
6
2)把目前目錄下的cpp檔案裡的chenyu替換成hello(find和-exec組合)
cat 1.cpp
chenyu
chengongyu
chenzixuan
chenyu
cat 2.cpp
chenyu
chenzixuan
chengongyu
chenyu
cat 3.cpp
chenyu
chencaifeng
chengongyu
cat 4.cpp
chenyu
chengong
chenyu
chencaifeng
find . -name "*.cpp" -exec sed -i 's/chenyu/hello/' {} \;
cat 1.cpp 2.cpp 3.cpp 4.cpp
hello
chengongyu
chenzixuan
hello
hello
chenzixuan
chengongyu
hello
hello
chencaifeng
chengongyu
hello
chengong
hello
chencaifeng
3)把目前目錄下的cpp檔案裡的hello替換成chenyu(find和xargs組合)
find . -name "*.cpp" | xargs sed -i 's/hello/chenyu/'
cat 1.cpp 2.cpp 3.cpp 4.cpp
chenyu
chengongyu
chenzixuan
chenyu
chenyu
chenzixuan
chengongyu
chenyu
chenyu
chencaifeng
chengongyu
chenyu
chengong
chenyu
chencaifeng