A. linux 文件內容替換問題
文件內容替換涉及到的命令如下:
1、打開文件後,進入一般模式(即沒法修改文件的模式)
2、輸入 :1,$s/word1/word2/g 指從第一行到最後一行,將word1換為word2
3、可以在g後面加上字母c,這樣替換前都需要用戶確認。
B. Linux查找和替換目錄下所有文件中字元串
linux查找和替換目錄下所有文件中字元串單個文件中查找替換很簡單,就不說了。文件夾下所有文件中字元串的查找替換就要記憶了,最近部署幾十台linux伺服器,記錄下總結。查找文件夾下包含字元串的文件例:查找/usr/local目錄下所有包含rubyer.me的文件。1
grep
-lr
'rubyer.me'
/usr/local/*vim替換單個文件中所有字元串方法例:替換當前文件中所有old為new
www.shiwu.com
1
:%s/old/new/g2#%表示替換說有行,g表示替換一行中所有匹配點。
替換文件夾下包含字元串的文件sed結合grep例:要將目錄/www下面所有文件中的zhangsan都修改成lisi,這樣做:1
sed
-i
s/old/new/g
`grep
old
-rl
/www`作者
skq
C. 根據查找Linux文件的內容進行替換操作
#!/bin/bash
GW=`cat network.cfg |grep gateway |awk -F= '{print $2}'`
IP1=`cat network.cfg |grep ntpsrv |awk -F= '{print $2}'`
sed -i "s#${IP1}#${GW}#g" network.cfg
D. 總結linux替換字元串的幾種方法
一)通過vi編輯器來替換。
vi/vim 中可以使用 :s 命令來替換字元串。
:s/well/good/ 替換當前行第一個 well 為 good
:s/well/good/g 替換當前行所有 well 為 good
:n,$s/well/good/ 替換第 n 行開始到最後一行中每一行的第一個 well 為 good
:n,$s/well/good/g 替換第 n 行開始到最後一行中每一行所有 well 為 good
n 為數字,若 n 為 .,表示從當前行開始到最後一行
:%s/well/good/(等同於 :g/well/s//good/) 替換每一行的第一個 well 為 good
:%s/well/good/g(等同於 :g/well/s//good/g) 替換每一行中所有 well 為 good
可以使用 # 作為分隔符,此時中間出現的 / 不會作為分隔符
:s#well/#good/# 替換當前行第一個 well/ 為 good/
:%s#/usr/bin#/bin#g 可以把文件中所有路徑/usr/bin換成/bin
(二)直接替換文件中的字元串。(此法不用打開文件即可替換字元串,而且可以批量替換多個文件。)
1.perl命令替換,參數含義如下:
-a 自動分隔模式,用空格分隔$_並保存到@F中。相當於@F = split 」。分隔符可以使用-F參數指定
-F 指定-a的分隔符,可以使用正則表達式
-e 執行指定的腳本。
-i<擴展名> 原地替換文件,並將舊文件用指定的擴展名備份。不指定擴展名則不備份。
-l 對輸入內容自動chomp,對輸出內容自動添加換行
-n 自動循環,相當於 while(<>) { 腳本; }
-p 自動循環+自動輸出,相當於 while(<>) { 腳本; print; }
用法示例:
perl -p -i.bak -e 's/foo/bar/g' *.c
將所有C程序中的foo替換成bar,舊文件備份成.bak
perl -p -i -e "s/shan/hua/g" ./lishan.txt ./lishan.txt.bak
將當前文件夾下lishan.txt和lishan.txt.bak中的「shan」都替換為「hua」
perl -i.bak -pe 's/(d+)/ 1 + $1 /ge' file1 file2
將每個文件中出現的數值都加一
2.sed命令下批量替換文件內容
格式: sed -i "s/查找欄位/替換欄位/g" `grep 查找欄位 -rl 路徑` 文件名
-i 表示inplace edit,就地修改文件
-r 表示搜索子目錄
-l 表示輸出匹配的文件名
s表示替換,d表示刪除
示例:sed -i "s/shan/hua/g" lishan.txt
把當前目錄下lishan.txt里的shan都替換為hua 各個Linux詳細命令介紹及應用可如下進行查找
E. linux下文本查找替換問題
一個文件包含行 "id": "abcd.123" —— 假設該文件為 file1.txt
另一個文件包含行 Package: test —— 假設該文件為 file2.txt
腳本如下:
#!/bin/bash
id=`sed 's/.*\"id\":\"\(.*\)\"/\1/' file1.txt`
sed "s/\(Package: \)test/\1$id/" file2.txt >result.txt
exit 0
結果保存到 result.txt 中。
如果要直接修改到 file2.txt 中而不用重定向的話,請使用 sed 的 -i 選項。
F. linux系統替換文件怎麼替換
摘要 您好,您的問題我已經看到了,正在整理答案,請稍等一會兒哦~
G. linux怎麼查找文件中的字元串替換
你好,
grep -rnl '要搜索的字元串' *
*表示當前目錄的所有文件和子文件夾,可以替換為某個文件夾名或者指定的文件名
-r 表示遞歸查找
-n 表示不顯示匹配所在的行號
-l 表示只輸出文件名
H. linux替換原來文件的命令
可以使用 Linux 系統的拷貝命令 cp,對原來的文件進行替換。例如:
$cp newfile oldfile
該命令的作用就是使用現在 newfile 的內容,替換掉原來 oldfile 的內容。
I. Linux下的shell編程 如何替換文件中的內容
shell編程中替換文件中的內容用到四個命sed,find
,grep,awk
下面是三種使用替換的方法
方法一:find
-name
'要查找的文件名'
|
xargs
perl
-pi
-e
's|被替換的字元串|替換後的字元串|g'下面這個例子就是將當前目錄及所有子目錄下的所有pom.xml文件中的」http://repo1.maven.org/maven2「
替換為」http://localhost:8081/nexus/content/groups/public「.
find
-name
'pom.xml'
|
xargs
perl
-pi
-e
's|http://repo1.maven.org/maven2|http://localhost:8081/nexus/content
/groups/public|g'這里用到了Perl語言,
perl
-pi
-e
在Perl
命令中加上-e
選項,後跟一行代碼,那它就會像運行一個普通的Perl
腳本那樣運行該代碼.
從命令行中使用Perl
能夠幫助實現一些強大的、實時的轉換。認真研究正則表達式,並正確地使用,將會為您省去大量的手工編輯工作。
find
-name
'pom.xml'
|
xargs
perl
-pi
-e
's|http://repo1.maven.org/maven2|http://localhost:8081/nexus/content/groups/public|g'
方法二:Linux下批量替換多個文件中的字元串的簡單方法。用sed命令可以批量替換多個文件中的字元串。
用sed命令可以批量替換多個文件中的
字元串。
sed
-i
"s/原字元串/新字元串/g"
`grep
原字元串
-rl
所在目錄`
例如:我要把mahuinan替換
為huinanma,執行命令:
sed
-i
"s/mahuinan/huinanma/g"
'grep
mahuinan
-rl
/www'
這是目前linux最簡單的批量替換字元串命令了!
具體格式如下:
sed
-i
"s/oldString/newString/g"
`grep
oldString
-rl
/path`
實例代碼:sed
-i
"s/大小多少/日月水火/g"
`grep
大小多少
-rl
/usr/aa`
sed
-i
"s/大小多少/日月水火/g"
`grep
大小多少
-rl
./`
方法三:在日程的開發過程中,可能大家會遇到將某個變數名修改
為另一個變數名的情況,如果這個變數是一個局部變數的話,vi足以勝任,但是如果是某個全局變數的話,並且在很多文件中進行了使用,這個時候使用vi就是
一個不明智的選擇。這里給出一個簡單的shell命令,可以一次性將所有文件中的指定字元串進行修改:
grep
"abc"
*
-R
|
awk
-F:
'{print
$1}'
|
sort
|
uniq
|
xargs
sed
-i
's/abc/abcde/g'
批量替換
配置文件中的IP:
grep
"[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}"
*
-R
|
awk
-F:
'{print
$1}'
|
sort
|
uniq
|
xargs
sed
-i
's/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/172\.0\.0\.1/g'
J. linux查找文件內容命令
搜索、查找文件當中的內容,一般最常用的是grep命令,另外還有egrep, vi命令也能搜索文件裡面內容
1:搜索某個文件裡面是否包含字元串,使用grep "search content" filename1, 例如
$ grep ORA alert_gsp.log
$ grep "ORA" alert_gsp.log
例如我們需要搜索、查找utlspadv.sql文件中包含ORA的字元內容
[oracle@DB-Server admin]$ grep "ORA" utlspadv.sql
-- ORA-XXXXX: Monitoring already started. If for example you want
-- ORA-20111:
-- ORA-20112:
-- ORA-20113: 'no active monitoring job found'
-- ORA-20113: 'no active monitoring job found'
-- 0 |<PS> =>DBS2.REGRESS.RDBMS.DEV.US.ORACLE.COM 0 0 2 99.3% 0% 0.7% ""
-- |<PR> DBS1.REGRESS.RDBMS.DEV.US.ORACLE.COM=> 100% 0% 0% "" |<PR> ...
-- =>DBS2.REGRESS.RDBMS.DEV.US.ORACLE.COM 92 7 99.3% 0% 0.7% "" |<PR> ...
-- |<C> CAPTURE_USER1=>DBS2.REGRESS.RDBMS.DEV.US.ORACLE.COM 2 0 0 0.E+00
-- |<C> CAPTURE_USER1=>DBS2.REGRESS.RDBMS.DEV.US.ORACLE.COM
-- ORA-20111:
-- ORA-20112:
-- ORA-20100:
-- ORA-20113: 'no active monitoring job found'
-- ORA-20113: 'no active monitoring job found'
[oracle@DB-Server admin]$
如上所示,這個是一個模糊匹配,其實我是想要查看ORA這類錯誤,那麼我要過濾掉哪一些沒有用的,搜索的內容修改一下即可(當然也可以使用特殊參數,後面有講述),如下所示。
[oracle@DB-Server admin]$ grep "ORA-" utlspadv.sql
-- ORA-XXXXX: Monitoring already started. If for example you want
-- ORA-20111:
-- ORA-20112:
-- ORA-20113: 'no active monitoring job found'
-- ORA-20113: 'no active monitoring job found'
-- ORA-20111:
-- ORA-20112:
-- ORA-20100:
-- ORA-20113: 'no active monitoring job found'
-- ORA-20113: 'no active monitoring job found'
[oracle@DB-Server admin]$
7:有些場景,我們並不知道文件類型、或那些文件包含有我們需要搜索的字元串,那麼可以遞歸搜索某個目錄以及子目錄下的所有文件
[oracle@DB-Server ~]$ grep -r "v$temp_space_header" /u01/app/oracle/proct/11.1.0/dbhome_1/rdbms/admin/
/u01/app/oracle/proct/11.1.0/dbhome_1/rdbms/admin/catspace.sql:create or replace view v_$temp_space_header as select * from v$temp_space_header;
/u01/app/oracle/proct/11.1.0/dbhome_1/rdbms/admin/catspace.sql:create or replace public synonym v$temp_space_header for v_$temp_space_header;
/u01/app/oracle/proct/11.1.0/dbhome_1/rdbms/admin/catspace.sql:create or replace view gv_$temp_space_header as select * from gv$temp_space_header;
/u01/app/oracle/proct/11.1.0/dbhome_1/rdbms/admin/catspace.sql:create or replace public synonym gv$temp_space_header
/u01/app/oracle/proct/11.1.0/dbhome_1/rdbms/admin/catspace.sql: FROM gv$temp_space_header
/u01/app/oracle/proct/11.1.0/dbhome_1/rdbms/admin/catspacd.sql:drop public synonym v$temp_space_header;
/u01/app/oracle/proct/11.1.0/dbhome_1/rdbms/admin/catspacd.sql:drop public synonym gv$temp_space_header;
[oracle@DB-Server ~]$
8:如果我們只想獲取那些文件包含搜索的內容,那麼可以使用下命令
[oracle@DB-Server ~]$ grep -H -r "v$temp_space_header" /u01/app/oracle/proct/11.1.0/dbhome_1/rdbms/admin/ | cut -d: -f1
/u01/app/oracle/proct/11.1.0/dbhome_1/rdbms/admin/catspace.sql
/u01/app/oracle/proct/11.1.0/dbhome_1/rdbms/admin/catspace.sql
/u01/app/oracle/proct/11.1.0/dbhome_1/rdbms/admin/catspace.sql
/u01/app/oracle/proct/11.1.0/dbhome_1/rdbms/admin/catspace.sql
/u01/app/oracle/proct/11.1.0/dbhome_1/rdbms/admin/catspace.sql
/u01/app/oracle/proct/11.1.0/dbhome_1/rdbms/admin/catspacd.sql
/u01/app/oracle/proct/11.1.0/dbhome_1/rdbms/admin/catspacd.sql
[oracle@DB-Server ~]$ grep -H -r "v$temp_space_header" /u01/app/oracle/proct/11.1.0/dbhome_1/rdbms/admin/ | cut -d: -f1 | uniq
/u01/app/oracle/proct/11.1.0/dbhome_1/rdbms/admin/catspace.sql
/u01/app/oracle/proct/11.1.0/dbhome_1/rdbms/admin/catspacd.sql
[oracle@DB-Server ~]$
9:如果只想獲取和整個搜索字元匹配的內容,那麼可以使用參數w
你可以對比一下兩者的區別
[oracle@DB-Server admin]$ grep -w "ORA" utlspadv.sql
-- ORA-XXXXX: Monitoring already started. If for example you want
-- ORA-20111:
-- ORA-20112:
-- ORA-20113: 'no active monitoring job found'
-- ORA-20113: 'no active monitoring job found'
-- ORA-20111:
-- ORA-20112:
-- ORA-20100:
-- ORA-20113: 'no active monitoring job found'
-- ORA-20113: 'no active monitoring job found'
[oracle@DB-Server admin]$ grep "ORA" utlspadv.sql
-- ORA-XXXXX: Monitoring already started. If for example you want
-- ORA-20111:
-- ORA-20112:
-- ORA-20113: 'no active monitoring job found'
-- ORA-20113: 'no active monitoring job found'
-- 0 |<PS> =>DBS2.REGRESS.RDBMS.DEV.US.ORACLE.COM 0 0 2 99.3% 0% 0.7% ""
-- |<PR> DBS1.REGRESS.RDBMS.DEV.US.ORACLE.COM=> 100% 0% 0% "" |<PR> ...
-- =>DBS2.REGRESS.RDBMS.DEV.US.ORACLE.COM 92 7 99.3% 0% 0.7% "" |<PR> ...
-- |<C> CAPTURE_USER1=>DBS2.REGRESS.RDBMS.DEV.US.ORACLE.COM 2 0 0 0.E+00
-- |<C> CAPTURE_USER1=>DBS2.REGRESS.RDBMS.DEV.US.ORACLE.COM
-- ORA-20111:
-- ORA-20112:
-- ORA-20100:
-- ORA-20113: 'no active monitoring job found'
-- ORA-20113: 'no active monitoring job found'
[oracle@DB-Server admin]$
10: grep命令結合find命令搜索
[oracle@DB-Server admin]$ find . -name '*.sql' -exec grep -i 'v$temp_space_header' {} ; -print
create or replace view v_$temp_space_header as select * from v$temp_space_header;
create or replace public synonym v$temp_space_header for v_$temp_space_header;
create or replace view gv_$temp_space_header as select * from gv$temp_space_header;
create or replace public synonym gv$temp_space_header
FROM gv$temp_space_header
./catspace.sql
drop public synonym v$temp_space_header;
drop public synonym gv$temp_space_header;
./catspacd.sql
[oracle@DB-Server admin]$
11: egrep -w -R 'word1|word2' ~/klbtmp
12: vi命令其實也能搜索文件裡面的內容,只不過沒有grep命令功能那麼方便、強大。