使用 sed 和 grep 批量修改文件指定內(nèi)容

/opt/path 目錄下查找hello,并將所有的hello替換為world

sed -i 's/hello/world/g' `grep -rl hello /opt/path`

/opt/path 目錄下查找hello/hello,并將所有的hello/hello替換為world/world

sed -i 's/hello\/hello/world\/world/g' `grep -rl 'hello/hello' /opt/path`

The s command (as in substitute) is probably the most important in sed and has a lot of different options. The syntax of the s command is ‘<samp>s/<var style="font-style: italic; font-weight: inherit;">regexp</var>/<var style="font-style: italic; font-weight: inherit;">replacement</var>/<var style="font-style: italic; font-weight: inherit;">flags</var></samp>’.

Its basic concept is simple: the s command attempts to match the pattern space against the supplied regular expression <var style="font-style: italic; font-weight: inherit;">regexp</var>; if the match is successful, then that portion of the pattern space which was matched is replaced with <var style="font-style: italic; font-weight: inherit;">replacement</var>.

For details about <var style="font-style: italic; font-weight: inherit;">regexp</var> syntax see Regular Expression Addresses.

The <var style="font-style: italic; font-weight: inherit;">replacement</var> can contain \<var style="font-style: italic; font-weight: inherit;">n</var> (<var style="font-style: italic; font-weight: inherit;">n</var> being a number from 1 to 9, inclusive) references, which refer to the portion of the match which is contained between the <var style="font-style: italic; font-weight: inherit;">n</var>th \( and its matching \). Also, the <var style="font-style: italic; font-weight: inherit;">replacement</var> can contain unescaped & characters which reference the whole matched portion of the pattern space.

The / characters may be uniformly replaced by any other single character within any given s command. The / character (or whatever other character is used in its stead) can appear in the <var style="font-style: italic; font-weight: inherit;">regexp</var> or <var style="font-style: italic; font-weight: inherit;">replacement</var> only if it is preceded by a \ character.


關(guān)于本文

參考資料

  1. http://www.gnu.org/software/sed/manual/sed.html
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末个束,一起剝皮案震驚了整個(gè)濱河市慕购,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌茬底,老刑警劉巖沪悲,帶你破解...
    沈念sama閱讀 218,204評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異阱表,居然都是意外死亡殿如,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,091評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門最爬,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)涉馁,“玉大人,你說(shuō)我怎么就攤上這事爱致】舅停” “怎么了?”我有些...
    開(kāi)封第一講書人閱讀 164,548評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵糠悯,是天一觀的道長(zhǎng)帮坚。 經(jīng)常有香客問(wèn)我妻往,道長(zhǎng),這世上最難降的妖魔是什么试和? 我笑而不...
    開(kāi)封第一講書人閱讀 58,657評(píng)論 1 293
  • 正文 為了忘掉前任讯泣,我火速辦了婚禮,結(jié)果婚禮上灰署,老公的妹妹穿的比我還像新娘判帮。我一直安慰自己局嘁,他們只是感情好溉箕,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,689評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著悦昵,像睡著了一般肴茄。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上但指,一...
    開(kāi)封第一講書人閱讀 51,554評(píng)論 1 305
  • 那天寡痰,我揣著相機(jī)與錄音,去河邊找鬼棋凳。 笑死拦坠,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的剩岳。 我是一名探鬼主播贞滨,決...
    沈念sama閱讀 40,302評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼拍棕!你這毒婦竟也來(lái)了晓铆?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書人閱讀 39,216評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤绰播,失蹤者是張志新(化名)和其女友劉穎骄噪,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體蠢箩,經(jīng)...
    沈念sama閱讀 45,661評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡链蕊,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,851評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了谬泌。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片滔韵。...
    茶點(diǎn)故事閱讀 39,977評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖呵萨,靈堂內(nèi)的尸體忽然破棺而出奏属,到底是詐尸還是另有隱情,我是刑警寧澤潮峦,帶...
    沈念sama閱讀 35,697評(píng)論 5 347
  • 正文 年R本政府宣布囱皿,位于F島的核電站勇婴,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏嘱腥。R本人自食惡果不足惜耕渴,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,306評(píng)論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望齿兔。 院中可真熱鬧橱脸,春花似錦、人聲如沸分苇。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 31,898評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)医寿。三九已至栏赴,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間靖秩,已是汗流浹背须眷。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 33,019評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留沟突,地道東北人花颗。 一個(gè)月前我還...
    沈念sama閱讀 48,138評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像惠拭,于是被迫代替她去往敵國(guó)和親扩劝。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,927評(píng)論 2 355