linux sed浣跨敤
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux sed浣跨敤相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/%e7%bb%93%e6%9e%9c' title='缁撴灉'>缁撴灉
test 鏂囨湰缂栬緫 鍚﹀垯 杈撳叆 shell 缇庣殑 閫夐」 鎵╁睍sed
鍔熻兘寮哄ぇ鐨勬祦寮忔枃鏈紪杈戝櫒
琛ュ厖璇存槑
sed 鏄竴绉嶆祦缂栬緫鍣紝瀹冩槸鏂囨湰澶勭悊涓潪甯搁噸瑕佺殑宸ュ叿锛岃兘澶熷畬缇庣殑閰嶅悎姝e垯琛ㄨ揪寮忎娇鐢紝鍔熻兘涓嶅悓鍑″搷銆傚鐞嗘椂锛屾妸褰撳墠澶勭悊鐨勮瀛樺偍鍦ㄤ复鏃剁紦鍐插尯涓紝绉颁负鈥滄ā寮忕┖闂粹€濓紙pattern space锛夛紝鎺ョ潃鐢╯ed鍛戒护澶勭悊缂撳啿鍖轰腑鐨勫唴瀹癸紝澶勭悊瀹屾垚鍚庯紝鎶婄紦鍐插尯鐨勫唴瀹归€佸線灞忓箷銆傛帴鐫€澶勭悊涓嬩竴琛岋紝杩欐牱涓嶆柇閲嶅锛岀洿鍒版枃浠舵湯灏俱€傛枃浠跺唴瀹瑰苟娌℃湁 鏀瑰彉锛岄櫎闈炰綘浣跨敤閲嶅畾鍚戝瓨鍌ㄨ緭鍑恒€係ed涓昏鐢ㄦ潵鑷姩缂栬緫涓€涓垨澶氫釜鏂囦欢锛涚畝鍖栧鏂囦欢鐨勫弽澶嶆搷浣滐紱缂栧啓杞崲绋嬪簭绛夈€?/p>
sed [options] 鈥榗ommand鈥?file(s)
sed [options] -f scriptfile file(s)
閫夐」
-e<script>鎴?-expression=<script>锛氫互閫夐」涓殑鎸囧畾鐨剆cript鏉ュ鐞嗚緭鍏ョ殑鏂囨湰鏂囦欢锛?-f<script鏂囦欢>鎴?-file=<script鏂囦欢>锛氫互閫夐」涓寚瀹氱殑script鏂囦欢鏉ュ鐞嗚緭鍏ョ殑鏂囨湰鏂囦欢锛?-h鎴?-help锛氭樉绀哄府鍔╋紱
-n鎴?-quiet鎴栤€斺€攕ilent锛氫粎鏄剧ずscript澶勭悊鍚庣殑缁撴灉锛?-V鎴?-version锛氭樉绀虹増鏈俊鎭€?
鍙傛暟
-e<script>鎴?-expression=<script>锛氫互閫夐」涓殑鎸囧畾鐨剆cript鏉ュ鐞嗚緭鍏ョ殑鏂囨湰鏂囦欢锛?-f<script鏂囦欢>鎴?-file=<script鏂囦欢>锛氫互閫夐」涓寚瀹氱殑script鏂囦欢鏉ュ鐞嗚緭鍏ョ殑鏂囨湰鏂囦欢锛?-h鎴?-help锛氭樉绀哄府鍔╋紱
-n鎴?-quiet鎴栤€斺€攕ilent锛氫粎鏄剧ずscript澶勭悊鍚庣殑缁撴灉锛?-V鎴?-version锛氭樉绀虹増鏈俊鎭€?
鏂囦欢锛氭寚瀹氬緟澶勭悊鐨勬枃鏈枃浠跺垪琛ㄣ€?/p>
sed鍛戒护
a # 鍦ㄥ綋鍓嶈涓嬮潰鎻掑叆鏂囨湰銆?i # 鍦ㄥ綋鍓嶈涓婇潰鎻掑叆鏂囨湰銆?c # 鎶婇€夊畾鐨勮鏀逛负鏂扮殑鏂囨湰銆?d # 鍒犻櫎锛屽垹闄ら€夋嫨鐨勮銆?D # 鍒犻櫎妯℃澘鍧楃殑绗竴琛屻€?s # 鏇挎崲鎸囧畾瀛楃
h # 鎷疯礉妯℃澘鍧楃殑鍐呭鍒板唴瀛樹腑鐨勭紦鍐插尯銆?H # 杩藉姞妯℃澘鍧楃殑鍐呭鍒板唴瀛樹腑鐨勭紦鍐插尯銆?g # 鑾峰緱鍐呭瓨缂撳啿鍖虹殑鍐呭锛屽苟鏇夸唬褰撳墠妯℃澘鍧椾腑鐨勬枃鏈€?G # 鑾峰緱鍐呭瓨缂撳啿鍖虹殑鍐呭锛屽苟杩藉姞鍒板綋鍓嶆ā鏉垮潡鏂囨湰鐨勫悗闈€?l # 鍒楄〃涓嶈兘鎵撳嵃瀛楃鐨勬竻鍗曘€?n # 璇诲彇涓嬩竴涓緭鍏ヨ锛岀敤涓嬩竴涓懡浠ゅ鐞嗘柊鐨勮鑰屼笉鏄敤绗竴涓懡浠ゃ€?N # 杩藉姞涓嬩竴涓緭鍏ヨ鍒版ā鏉垮潡鍚庨潰骞跺湪浜岃€呴棿宓屽叆涓€涓柊琛岋紝鏀瑰彉褰撳墠琛屽彿鐮併€?p # 鎵撳嵃妯℃澘鍧楃殑琛屻€?P # (澶у啓) 鎵撳嵃妯℃澘鍧楃殑绗竴琛屻€?q # 閫€鍑篠ed銆?b lable # 鍒嗘敮鍒拌剼鏈腑甯︽湁鏍囪鐨勫湴鏂癸紝濡傛灉鍒嗘敮涓嶅瓨鍦ㄥ垯鍒嗘敮鍒拌剼鏈殑鏈熬銆?r file # 浠巉ile涓琛屻€?t label # if鍒嗘敮锛屼粠鏈€鍚庝竴琛屽紑濮嬶紝鏉′欢涓€鏃︽弧瓒虫垨鑰匱锛宼鍛戒护锛屽皢瀵艰嚧鍒嗘敮鍒板甫鏈夋爣鍙风殑鍛戒护澶勶紝鎴栬€呭埌鑴氭湰鐨勬湯灏俱€?T label # 閿欒鍒嗘敮锛屼粠鏈€鍚庝竴琛屽紑濮嬶紝涓€鏃﹀彂鐢熼敊璇垨鑰匱锛宼鍛戒护锛屽皢瀵艰嚧鍒嗘敮鍒板甫鏈夋爣鍙风殑鍛戒护澶勶紝鎴栬€呭埌鑴氭湰鐨勬湯灏俱€?w file # 鍐欏苟杩藉姞妯℃澘鍧楀埌file鏈熬銆?
W file # 鍐欏苟杩藉姞妯℃澘鍧楃殑绗竴琛屽埌file鏈熬銆?
! # 琛ㄧず鍚庨潰鐨勫懡浠ゅ鎵€鏈夋病鏈夎閫夊畾鐨勮鍙戠敓浣滅敤銆?
= # 鎵撳嵃褰撳墠琛屽彿鐮併€?
# # 鎶婃敞閲婃墿灞曞埌涓嬩竴涓崲琛岀浠ュ墠銆?
sed鏇挎崲鏍囪
g # 琛ㄧず琛屽唴鍏ㄩ潰鏇挎崲銆?
p # 琛ㄧず鎵撳嵃琛屻€?
w # 琛ㄧず鎶婅鍐欏叆涓€涓枃浠躲€?
x # 琛ㄧず浜掓崲妯℃澘鍧椾腑鐨勬枃鏈拰缂撳啿鍖轰腑鐨勬枃鏈€?
y # 琛ㄧず鎶婁竴涓瓧绗︾炕璇戜负鍙﹀鐨勫瓧绗︼紙浣嗘槸涓嶇敤浜庢鍒欒〃杈惧紡锛?1 # 瀛愪覆鍖归厤鏍囪
& # 宸插尮閰嶅瓧绗︿覆鏍囪
sed鍏冨瓧绗﹂泦
^ # 鍖归厤琛屽紑濮嬶紝濡傦細/^sed/鍖归厤鎵€鏈変互sed寮€澶寸殑琛屻€?$ # 鍖归厤琛岀粨鏉燂紝濡傦細/sed$/鍖归厤鎵€鏈変互sed缁撳熬鐨勮銆?. # 鍖归厤涓€涓潪鎹㈣绗︾殑浠绘剰瀛楃锛屽锛?s.d/鍖归厤s鍚庢帴涓€涓换鎰忓瓧绗︼紝鏈€鍚庢槸d銆?* # 鍖归厤0涓垨澶氫釜瀛楃锛屽锛?*sed/鍖归厤鎵€鏈夋ā鏉挎槸涓€涓垨澶氫釜绌烘牸鍚庣揣璺焥ed鐨勮銆?[] # 鍖归厤涓€涓寚瀹氳寖鍥村唴鐨勫瓧绗︼紝濡?[ss]ed/鍖归厤sed鍜孲ed銆?
[^] # 鍖归厤涓€涓笉鍦ㄦ寚瀹氳寖鍥村唴鐨勫瓧绗︼紝濡傦細/[^A-RT-Z]ed/鍖归厤涓嶅寘鍚獳-R鍜孴-Z鐨勪竴涓瓧姣嶅紑澶达紝绱ц窡ed鐨勮銆?(..) # 鍖归厤瀛愪覆锛屼繚瀛樺尮閰嶇殑瀛楃锛屽s/(love)able/1rs锛宭oveable琚浛鎹㈡垚lovers銆?& # 淇濆瓨鎼滅储瀛楃鐢ㄦ潵鏇挎崲鍏朵粬瀛楃锛屽s/love/ **&** /锛宭ove杩欐垚 **love** 銆?< # 鍖归厤鍗曡瘝鐨勫紑濮嬶紝濡?/<love/鍖归厤鍖呭惈浠ove寮€澶寸殑鍗曡瘝鐨勮銆?> # 鍖归厤鍗曡瘝鐨勭粨鏉燂紝濡?love>/鍖归厤鍖呭惈浠ove缁撳熬鐨勫崟璇嶇殑琛屻€?x{m} # 閲嶅瀛楃x锛宮娆★紝濡傦細/0{5}/鍖归厤鍖呭惈5涓?鐨勮銆?x{m,} # 閲嶅瀛楃x锛岃嚦灏憁娆★紝濡傦細/0{5,}/鍖归厤鑷冲皯鏈?涓?鐨勮銆?x{m,n} # 閲嶅瀛楃x锛岃嚦灏憁娆★紝涓嶅浜巒娆★紝濡傦細/0{5,10}/鍖归厤5~10涓?鐨勮銆?
sed鐢ㄦ硶瀹炰緥
鏇挎崲鎿嶄綔锛歴鍛戒护
鏇挎崲鏂囨湰涓殑瀛楃涓诧細
sed 鈥榮/book/books/鈥?file
-n閫夐」 鍜?p鍛戒护 涓€璧蜂娇鐢ㄨ〃绀哄彧鎵撳嵃閭d簺鍙戠敓鏇挎崲鐨勮锛?/p>
sed -n 鈥榮/test/TEST/p鈥?file
鐩存帴缂栬緫鏂囦欢 閫夐」-i 锛屼細鍖归厤file鏂囦欢涓瘡涓€琛岀殑鎵€鏈塨ook鏇挎崲涓篵ooks锛?/p>
sed -i 鈥榮/book/books/g鈥?file
鍏ㄩ潰鏇挎崲鏍囪g
浣跨敤鍚庣紑 /g 鏍囪浼氭浛鎹㈡瘡涓€琛屼腑鐨勬墍鏈夊尮閰嶏細
sed 鈥榮/book/books/g鈥?file
褰撻渶瑕佷粠绗琋澶勫尮閰嶅紑濮嬫浛鎹㈡椂锛屽彲浠ヤ娇鐢?/Ng锛?/p>
echo sksksksksksk | sed 鈥榮/sk/SK/2g鈥?skSKSKSKSKSK
echo sksksksksksk | sed 鈥榮/sk/SK/3g鈥?skskSKSKSKSK
echo sksksksksksk | sed 鈥榮/sk/SK/4g鈥?skskskSKSKSK
sed 鈥榮:test:TEXT:g鈥?sed 鈥榮|test|TEXT|g鈥?
瀹氱晫绗﹀嚭鐜板湪鏍峰紡鍐呴儴鏃讹紝闇€瑕佽繘琛岃浆涔夛細
sed 鈥榮//bin//usr/local/bin/g鈥?
鍒犻櫎鎿嶄綔锛歞鍛戒护
鍒犻櫎绌虹櫧琛岋細
sed 鈥?^$/d鈥?file
鍒犻櫎鏂囦欢鐨勭2琛岋細
sed 鈥?d鈥?file
鍒犻櫎鏂囦欢鐨勭2琛屽埌鏈熬鎵€鏈夎锛?/p>
sed 鈥?,$d鈥?file
鍒犻櫎鏂囦欢鏈€鍚庝竴琛岋細
sed 鈥?d鈥?file
鍒犻櫎鏂囦欢涓墍鏈夊紑澶存槸test鐨勮锛?/p>
sed 鈥?^test/鈥榙 file
宸插尮閰嶅瓧绗︿覆鏍囪&
姝e垯琛ㄨ揪寮?w+ 鍖归厤姣忎竴涓崟璇嶏紝浣跨敤 [&] 鏇挎崲瀹冿紝& 瀵瑰簲浜庝箣鍓嶆墍鍖归厤鍒扮殑鍗曡瘝锛?/p>
echo this is a test line | sed 鈥榮/w+/[&]/g鈥?[this] [is] [a] [test] [line]
鎵€鏈変互192.168.0.1寮€澶寸殑琛岄兘浼氳鏇挎崲鎴愬畠鑷凡鍔爈ocalhost锛?/p>
sed 鈥榮/^192.168.0.1/&localhost/鈥?file
192.168.0.1localhost
瀛愪覆鍖归厤鏍囪1
鍖归厤缁欏畾鏍峰紡鐨勫叾涓竴閮ㄥ垎锛?/p>
echo this is digit 7 in a number | sed 鈥榮/digit ([0-9])/1/鈥?this is 7 in a number
鍛戒护涓?digit 7锛岃鏇挎崲鎴愪簡 7銆傛牱寮忓尮閰嶅埌鐨勫瓙涓叉槸 7锛?..) 鐢ㄤ簬鍖归厤瀛愪覆锛屽浜庡尮閰嶅埌鐨勭涓€涓瓙涓插氨鏍囪涓?1 锛屼緷姝ょ被鎺ㄥ尮閰嶅埌鐨勭浜屼釜缁撴灉灏辨槸 2锛屼緥濡傦細
echo aaa BBB | sed 鈥榮/([a-z]+) ([A-Z]+)/2 1/鈥?BBB aaa
love琚爣璁颁负1锛屾墍鏈塴oveable浼氳鏇挎崲鎴恖overs锛屽苟鎵撳嵃鍑烘潵锛?/p>
sed -n 鈥榮/(love)able/1rs/p鈥?file
sed 鈥樿〃杈惧紡鈥?| sed 鈥樿〃杈惧紡鈥?
绛変环浜庯細
sed 鈥樿〃杈惧紡; 琛ㄨ揪寮忊€?
寮曠敤
sed琛ㄨ揪寮忓彲浠ヤ娇鐢ㄥ崟寮曞彿鏉ュ紩鐢紝浣嗘槸濡傛灉琛ㄨ揪寮忓唴閮ㄥ寘鍚彉閲忓瓧绗︿覆锛屽氨闇€瑕佷娇鐢ㄥ弻寮曞彿銆?/p>
test=hello
echo hello WORLD | sed "s/$test/HELLO"
HELLO WORLD
sed -n 鈥?test/,/check/p鈥?file
鎵撳嵃浠庣5琛屽紑濮嬪埌绗竴涓寘鍚互test寮€濮嬬殑琛屼箣闂寸殑鎵€鏈夎锛?/p>
sed -n 鈥?,/^test/p鈥?file
瀵逛簬妯℃澘test鍜寃est涔嬮棿鐨勮锛屾瘡琛岀殑鏈熬鐢ㄥ瓧绗︿覆aaa bbb鏇挎崲锛?/p>
sed 鈥?test/,/west/s/$/aaa bbb/鈥?file
澶氱偣缂栬緫锛歟鍛戒护
-e閫夐」鍏佽鍦ㄥ悓涓€琛岄噷鎵ц澶氭潯鍛戒护锛?/p>
sed -e 鈥?,5d鈥?-e 鈥榮/test/check/鈥?file
涓婇潰sed琛ㄨ揪寮忕殑绗竴鏉″懡浠ゅ垹闄?鑷?琛岋紝绗簩鏉″懡浠ょ敤check鏇挎崲test銆傚懡浠ょ殑鎵ц椤哄簭瀵圭粨鏋滄湁褰卞搷銆傚鏋滀袱涓懡浠ら兘鏄浛鎹㈠懡浠わ紝閭d箞绗竴涓浛鎹㈠懡浠ゅ皢褰卞搷绗簩涓浛鎹㈠懡浠ょ殑缁撴灉銆?/p>
鍜?-e 绛変环鐨勫懡浠ゆ槸 --expression锛?/p>
sed --expression=鈥榮/test/check/鈥?--expression=鈥?love/d鈥?file
浠庢枃浠惰鍏ワ細r鍛戒护
file閲岀殑鍐呭琚杩涙潵锛屾樉绀哄湪涓巘est鍖归厤鐨勮鍚庨潰锛屽鏋滃尮閰嶅琛岋紝鍒檉ile鐨勫唴瀹瑰皢鏄剧ず鍦ㄦ墍鏈夊尮閰嶈鐨勪笅闈細
sed 鈥?test/r file鈥?filename
鍐欏叆鏂囦欢锛歸鍛戒护
鍦╡xample涓墍鏈夊寘鍚玹est鐨勮閮借鍐欏叆file閲岋細
sed -n 鈥?test/w file鈥?example
杩藉姞锛堣涓嬶級锛歛鍛戒护
灏?this is a test line 杩藉姞鍒?浠est 寮€澶寸殑琛屽悗闈細
sed 鈥?^test/a his is a test line鈥?file
鍦?test.conf 鏂囦欢绗?琛屼箣鍚庢彃鍏?this is a test line锛?/p>
sed -i 鈥?a his is a test line鈥?test.conf
鎻掑叆锛堣涓婏級锛歩鍛戒护
灏?this is a test line 杩藉姞鍒颁互test寮€澶寸殑琛屽墠闈細
sed 鈥?^test/i his is a test line鈥?file
鍦╰est.conf鏂囦欢绗?琛屼箣鍓嶆彃鍏his is a test line锛?/p>
sed -i 鈥?i his is a test line鈥?test.conf
涓嬩竴涓細n鍛戒护
濡傛灉test琚尮閰嶏紝鍒欑Щ鍔ㄥ埌鍖归厤琛岀殑涓嬩竴琛岋紝鏇挎崲杩欎竴琛岀殑aa锛屽彉涓篵b锛屽苟鎵撳嵃璇ヨ锛岀劧鍚庣户缁細
sed 鈥?test/{ n; s/aa/bb/; }鈥?file
鍙樺舰锛歽鍛戒护
鎶?~10琛屽唴鎵€鏈塧bcde杞彉涓哄ぇ鍐欙紝娉ㄦ剰锛屾鍒欒〃杈惧紡鍏冨瓧绗︿笉鑳戒娇鐢ㄨ繖涓懡浠わ細
sed 鈥?,10y/abcde/ABCDE/鈥?file
閫€鍑猴細q鍛戒护
鎵撳嵃瀹岀10琛屽悗锛岄€€鍑簊ed
sed 鈥?0q鈥?file
淇濇寔鍜岃幏鍙栵細h鍛戒护鍜孏鍛戒护
鍦╯ed澶勭悊鏂囦欢鐨勬椂鍊欙紝姣忎竴琛岄兘琚繚瀛樺湪涓€涓彨妯″紡绌洪棿鐨勪复鏃剁紦鍐插尯涓紝闄ら潪琛岃鍒犻櫎鎴栬€呰緭鍑鸿鍙栨秷锛屽惁鍒欐墍鏈夎澶勭悊鐨勮閮藉皢 鎵撳嵃鍦ㄥ睆骞曚笂銆傛帴鐫€妯″紡绌洪棿琚竻绌猴紝骞跺瓨鍏ユ柊鐨勪竴琛岀瓑寰呭鐞嗐€?/p>
sed -e 鈥?test/h鈥?-e 鈥?G鈥?file
鍦ㄨ繖涓緥瀛愰噷锛屽尮閰峵est鐨勮琚壘鍒板悗锛屽皢瀛樺叆妯″紡绌洪棿锛宧鍛戒护灏嗗叾澶嶅埗骞跺瓨鍏ヤ竴涓О涓轰繚鎸佺紦瀛樺尯鐨勭壒娈婄紦鍐插尯鍐呫€傜浜屾潯璇彞鐨勬剰鎬濇槸锛屽綋鍒拌揪鏈€鍚庝竴琛屽悗锛孏鍛戒护鍙栧嚭淇濇寔缂撳啿鍖虹殑琛岋紝鐒跺悗鎶婂畠鏀惧洖妯″紡绌洪棿涓紝涓旇拷鍔犲埌鐜板湪宸茬粡瀛樺湪浜庢ā寮忕┖闂翠腑鐨勮鐨勬湯灏俱€傚湪杩欎釜渚嬪瓙涓氨鏄拷鍔犲埌鏈€鍚庝竴琛屻€傜畝鍗曟潵璇达紝浠讳綍鍖呭惈test鐨勮閮借澶嶅埗骞惰拷鍔犲埌璇ユ枃浠剁殑鏈熬銆?/p>
淇濇寔鍜屼簰鎹細h鍛戒护鍜寈鍛戒护
浜掓崲妯″紡绌洪棿鍜屼繚鎸佺紦鍐插尯鐨勫唴瀹广€備篃灏辨槸鎶婂寘鍚玹est涓巆heck鐨勮浜掓崲锛?/p>
sed -e 鈥?test/h鈥?-e 鈥?check/x鈥?file
鑴氭湰scriptfile
sed鑴氭湰鏄竴涓猻ed鐨勫懡浠ゆ竻鍗曪紝鍚姩Sed鏃朵互-f閫夐」寮曞鑴氭湰鏂囦欢鍚嶃€係ed瀵逛簬鑴氭湰涓緭鍏ョ殑鍛戒护闈炲父鎸戝墧锛屽湪鍛戒护鐨勬湯灏句笉鑳芥湁浠讳綍绌虹櫧鎴栨枃鏈紝濡傛灉鍦ㄤ竴琛屼腑鏈夊涓懡浠わ紝瑕佺敤鍒嗗彿鍒嗛殧銆備互#寮€澶寸殑琛屼负娉ㄩ噴琛岋紝涓斾笉鑳借法琛屻€?/p>
sed [options] -f scriptfile file(s)
sed -n 鈥榩;n鈥?test.txt #濂囨暟琛?sed -n 鈥榥;p鈥?test.txt #鍋舵暟琛?
鏂规硶2锛?/p>
sed -n 鈥?~2p鈥?test.txt #濂囨暟琛?sed -n 鈥?~2p鈥?test.txt #鍋舵暟琛?
grep -A 1 SCC URFILE sed -n 鈥?SCC/{n;p}鈥?URFILE awk 鈥?SCC/{getline; print}鈥?URFILE
以上是关于linux sed浣跨敤的主要内容,如果未能解决你的问题,请参考以下文章
Linux(CentOS)浣跨敤瀹氭椂浠诲姟鍒犻櫎Tomcat鏃ュ織鏂囦欢
濡備綍浣跨敤 lftp 鏉ュ姞閫?ftp/https 涓嬭浇閫熷害 | Linux 涓浗