The UNIX and Linux Forums  


Go Back   UNIXおよびLinuxフォーラム > トップフォーラム > シェルプログラミングとスクリプティング
Googleのunix.com



シェルプログラミングとスクリプティング KSH 、 CSH 、 shに、 bashの、はPerl 、 PHPは、削除するsed 、 Awkの、他のシェルスクリプトやシェルスクリプト言語についての質問の投稿はこちら。

その他のUNIXおよびLinuxフォーラムトピックは参考にすること
スレッド スレッドスターター フォーラム 返信 最後の投稿
どのようにマッチした文字列が行を削除するsedまたはループを使用するためにはgrepを始めるために? AMBERは シェルプログラミングとスクリプティング 8 2009年7月13日 06:26午前
はgrepと文字列と行を除いて行を削除 vj8436 シェルプログラミングとスクリプティング 14 2009年4月17日 12:25午後
txtファイルに文字列の後に10行はgrepする。 suman82 シェルプログラミングとスクリプティング 6 2008年12月13日 01:08午前
数行の後にはgrepの文字列& ashterix シェルプログラミングとスクリプティング 7 2008年12月7日 09:20午前
次の文字列と行はgrep Ñ ashterix シェルプログラミングとスクリプティング 8 2005年11月21日 11:38午後

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek を搭載 Powered by Google
 
LinkBack スレッドツール このスレッドを検索 スレッドを評価 表示モード
  #1固定リンク)  
Old 2009年7月10日
bhagirathi bhagirathi is offline
登録ユーザー
  
 

参加日: 2009年7月
投稿: 10
ファイルの2文字列の間の行の文字列はgrep

やあ、
次の問題で私を助けてください:
私は、以下の行では、 XMLファイルが

コード:
<cisco:name>
  <cisco:mdNm>Cisco Device 7500 A Series</cisco:mdNm>
  <cisco:meNm>10.1.100.19</cisco:meNm>
  <cisco:ehNm>/shelf=1</cisco:ehNm>
      <cisco:subname>
         <cisco:sptp>Cisco PortA Series</cisco:sptp>
         <cisco:aliasNameList xsi:nil="true"/>
         <cisco:owner xsi:nil="true"/>
                      <cisco:subportname>
                 <cisco:cpt>Cisco SubPort B Series</cisco:cpt>
                 <cisco:aliasNamesubList xsi:nil="true"/>
                 <cisco:userLabel xsi:nil="true"/>
               </cisco:subportname>
       </cisco:subname>
           <cisco:subname>
         <cisco:sptp>Cisco PortAB Series</cisco:sptp>
         <cisco:aliasNameList xsi:nil="true"/>
         <cisco:owner xsi:nil="true"/>
                      <cisco:subportname>
                 <cisco:cpt>Cisco SubPort AB Series</cisco:cpt>
                 <cisco:aliasNamesubList xsi:nil="true"/>
                 <cisco:userLabel xsi:nil="true"/>
               </cisco:subportname>
       </cisco:subname>
</cisco:name>
<cisco:name>
  <cisco:mdNm>Cisco Device 7500B Series</cisco:mdNm>
  <cisco:meNm>10.1.100.20</cisco:meNm>
  <cisco:ehNm>/shelf=2</cisco:ehNm>
      <cisco:subname>
         <cisco:sptp>Cisco Port B Series</cisco:sptp>
         <cisco:aliasNameList xsi:nil="true"/>
         <cisco:owner xsi:nil="true"/>
                      <cisco:subportname>
                 <cisco:cpt>Cisco SubPort B Series</cisco:cpt>
                 <cisco:aliasNamesubList xsi:nil="true"/>
                 <cisco:userLabel xsi:nil="true"/>
               </cisco:subportname>
       </cisco:subname>
</cisco:name>

自分のコードでは、最初の<cisco:name>と< /シスコ:名前>とは、 2つの文字列をチェックします後にそれをカウントするgrepする<cisco:cpt> 。のようにファイルの最後まで続ける



コード:
#!/bin/sh
sed '/<\/cisco:name>/{G;}' test.xml >temp 

nawk 'BEGIN{RS=""} 
{ 
grep -c '<cisco:cpt>' print 


}' temp 
rm temp

このコードでは、オペアンプとして提供されていません
最初に<cisco:name>と< /シスコ:名前>ループは横にして1次の2通りを与える必要があります

支援plz
  #2固定リンク)  
Old 2009年7月10日
honglus honglus is offline
登録ユーザー
  
 

参加日: 2008年12月
投稿: 47

コード:
awk '{ if (  $0 ~ /<cisco:cpt>/  )  {i++} if ( $0 ~ /<\/cisco:name>/ ) {print i;i=0}}' test.xml

  #3固定リンク)  
Old 2009年7月10日
bhagirathi bhagirathi is offline
登録ユーザー
  
 

参加日: 2009年7月
投稿: 10
こんにちはHonglus 、
素晴らしい作品に感謝します。しかし、 1つのクエリがある
もし<cisco:cpt>のように2つまたは3パターン、検索する必要があります<cisco:mdNm> 、 <cisco:sptp>してはどうすれば進むことができます。
  #4固定リンク)  
Old 2009年7月10日
honglus honglus is offline
登録ユーザー
  
 

参加日: 2008年12月
投稿: 47
引用:
当初の投稿 bhagirathi View Post
こんにちはHonglus 、
素晴らしい作品に感謝します。しかし、 1つのクエリがある
もし<cisco:cpt>のように2つまたは3パターン、検索する必要があります<cisco:mdNm> 、 <cisco:sptp>してはどうすれば進むことができます。
または、このようなものとのリンク:
$ 0 〜 / ( 1 | 2 | 3 ) /
  #5固定リンク)  
Old 2009年7月10日
summer_cherry summer_cherry is offline Forum Advisor  
登録ユーザー
  
 

参加日: 2007年6月
所在地:中国北京
投稿数:1089
もっと正式には、 perlの 多くの利用可能なモジュールを使用してXML文書を扱うことができます。

下の結果を参照することがあります

コード:
use XML::Parser;
my $file = 'a.xml';
my ($flag,$index,@arr,%hash)=(0,-1);
sub start_handler
{
    my $expat = shift; 
    my $element = shift;
    if($element eq 'cisco:name'){
			$index++;
		}
		else{
			if($element eq 'cisco:cpt'){
				$flag=1
			}
			else{
				$flag=0;				
			}
		}
}
sub char_handler
{
    my ($p, $data) = @_;
    if( ($flag eq '1') and  !($data =~ /^\s*$/)){
    	$arr[$index]++;
    	$hash{$index}.="  ".$data;
    }
}
my $parser = new XML::Parser(ErrorContext => 2);
$parser->setHandlers(Start => \&start_handler,
                     Char  => \&char_handler);
$parser->parsefile($file);
for(my $i=0;$i<=$#arr;$i++){
	print $arr[$i],"\n";
	print $hash{$i},"\n";
	print "--------\n";
}

  #6固定リンク)  
Old 2009年7月10日
bhagirathi bhagirathi is offline
登録ユーザー
  
 

参加日: 2009年7月
投稿: 10
私は同じでした

コード:
awk '{ if (  $0 ~ /<cisco:cpt> | <cisco:sptp>/  )  {i++} if ( $0 ~ /<\/cisco:name>/ ) {print  "cpt=" i;i=0}}' test.xml

しかし、結果を追加しました。が異なるので、どのように印刷することができることをお勧めでした
好き
cpt \u003d 1
sptp \u003d 2
  #7固定リンク)  
Old 2009年7月10日
ghostdog74 ghostdog74 is offline Forum Advisor  
登録ユーザー
  
 

参加日: 2006年9月
投稿数: 2,555
のろま

コード:
awk 'BEGIN{ RS="</cisco:name>"}
{
  m=split($0,a,"\n")
  f=0
  for(i=1;i<=m;i++) {
    if(a[i] ~ /<cisco:cpt>/){
        f++    
    }
  }
  print f
}' file

Reply

ブックマーク

スレッドツール このスレッドを検索
このスレッドを検索

高度な検索
表示モード このスレッド
このスレッド

投稿ルール
あなた ことができない。 新しいスレッドを投稿
あなた ことができない。 返信の投稿
あなた ことができない。 添付ファイルの投稿
あなた ことができない。 自分の投稿を編集

BBコード なる 〜の上に
スマイリー なる 〜の上に
[イメージ] コードは 〜の上に
HTMLコードは、 オフ
トラックバック なる 〜の上に
ピングバック なる 〜の上に
Refbacks なる 〜の上に




すべてGMT -4です。現在の時刻は 12:27午後


提供: vBulletin、著作権© 2000 - 2006、Jelsoft企業株式会社。言語翻訳による電源
vBCredits v1.4著作権© 2007 - 2008 、 PixelFXスタジオ
は、 UNIXおよびLinuxフォーラムのコンテンツ著作権© 1993 〜 2009 。すべての権利を管理しReserved.Ad RedTyger

コンテンツ関連のURLで vBSEO 3.2.0