Re: [問題] 文字搜尋與替換

看板Linux作者 (唔~)時間17年前 (2009/03/11 00:45), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串3/3 (看更多)
※ 引述《demintree.bbs@bbs.cs.nctu.edu.tw (0rz)》之銘言: : ※ 引述《lance0705.bbs@ptt.cc (藍‧聲‧口‧琴)》之銘言: : > 想請教各位shell script的高手 : > 如果今天有一個文件如下 : > ................................., Execution CWD </home/hp : > c/lance0705/M2O3-CeO2/Potential_Trials/Hayashi_potential/B : > igCell_pairDoped/annealed_1273K/11.11mol/Gd/D1_Tscaling_st : > ep2fs>; : > 文件中的換行處是包含一串空白及\n : > 請問有沒有方法可以把CWD後面的<.........>這一串字串併成一行輸出? : > 感謝回答<(_ _)>! : vim : 如果你只是單純的想把這幾行接起來的話指令下 : %s/\n//cgi : 不知道這樣子是不是你想要的 使用perl好了 $ cat -n test.pl 1 #!/usr/bin/perl -w 2 3 use strict; 4 use warnings; 5 6 my $str; 7 my $in=0; 8 9 while ( <STDIN> ) { 10 if ( $in == 0 ) { 11 if ( /CWD\ </ ) { 12 s/\ |\n//g; 13 if ( ! />/ ) { 14 $in=1; 15 } 16 } 17 } else { 18 s/\ |\n//g; 19 if ( />/ ) { 20 $in=0; 21 } 22 } 23 $str.=$_; 24 } 25 26 print $str; $ cat test .................................., Execution CWD </home/hp c/lance0705/M2O3-CeO2/Potential_Trials/Hayashi_potential/B igCell_pairDoped/annealed_1273K/11.11mol/Gd/D1_Tscaling_st ep2fs>; $ perl test.pl < test -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.123.237.163

03/11 08:40, , 1F
解決了^^ 謝謝您 perl好像蠻值得學的
03/11 08:40, 1F
文章代碼(AID): #19jfaZvx (Linux)
文章代碼(AID): #19jfaZvx (Linux)