Bash - 特定のURLを除くすべてのURLを抽出する

Bash - 特定のURLを除くすべてのURLを抽出する

複数行のURLを含むファイルがあります。処理や表示に興味がなく、無視したいURLがいくつかあります。私は他にも出力として表示したいと思います。

これまで私のコマンドは次のようになります。

grep 'http://' data.txt | sed 's/.*\(http:.*\)\".*/\1/'

次のURLを除外したいと思います。

http://schemas.openxmlformats.org...

私はこれに慣れておらず、どんな助けでも心から感謝します。

更新:これは私が作業しているファイルです。

Relationships Xmlns             : http://schemas.openxmlformats.org/package/2006/relationships
Relationships Relationship Type : http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties
Style Sheet Xmlns               : http://schemas.openxmlformats.org/spreadsheetml/2006/main
Relationships Xmlns             : http://schemas.openxmlformats.org/package/2006/relationships
Relationships Relationship Type : http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings
Workbook Xmlns                  : http://schemas.openxmlformats.org/spreadsheetml/2006/main
Relationships Xmlns             : http://schemas.openxmlformats.org/package/2006/relationships
Relationships Relationship Type : http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink
Relationships Relationship Target: http://www.yahoo.com/
Worksheet Xmlns                 : http://schemas.openxmlformats.org/spreadsheetml/2006/main
Sst Xmlns                       : http://schemas.openxmlformats.org/spreadsheetml/2006/main
Types Xmlns                     : http://schemas.openxmlformats.org/package/2006/content-types
Properties Xmlns                : http://schemas.openxmlformats.org/officeDocument/2006/extended-properties

欲しいhttp://www.yahoo.com別々に抽出し、Schemas.openxmlformatsが含まれているので、残りは無視します。

ベストアンサー1

私の考えでは、sedだけを使うとこれを行うことができると思います。

sed -n '\,http://schemas.openxmlformats.org,!s/.*\(http:.*\).*/\1/p'
  • -nテキスト自動印刷を無効にして、選択した行のみを印刷します。
  • \,http://schemas.openxmlformats.org,!一致しない行でのみ、次のコマンドを実行してください(したがって!最後に)http://schemas.openxmlformats.org。ここでは正規表現の区切り記号,として not を使用しているので、最初はこうします。これにより、パターンから脱出する必要性が減ります。/\,\
  • コマンドはs あなたのコマンドと同じですが、pそれ以降はそのコマンドを使用してURLのみを含む行を印刷しました。

1行に1つのURLしかないとします。

追加の引用符を削除すると、正しい出力が得られます。

$ sed -n '\,http://schemas.openxmlformats.org,!s/.*\(http:.*\).*/\1/p' inpu-file
http://www.yahoo.com/

おすすめ記事