2017-11-28 18 views
-3

ファイル内の文字列から単語を削除しようとしています。問題は、この単語がファイル内にさらに存在することです。AWK inside SEDの使い方

私は私がやっているように、ファイルからホスト名を削除する必要があります。

less /etc/dhcp/dhclient.conf | grep host-name 

を、これは私が取得していますものです:だから

send host-name = gethostname(); 
    domain-name, domain-name-servers, domain-search, host-name, 
# option host-name "andare.swiftmedia.com"; 

は、今私はAWKを使用してみました:

less /etc/dhcp/dhclient.conf | grep host-name | awk 'NR>1 {print $4}' 
host-name, 
"andare.swiftmedia.com"; 

しかし、私はどのように特定のオカレンス上のファイルからこれを削除することはできません(2番目のオカレンスで)

sedとawkの両方が常に私を混乱させます:/ あなたの助けに感謝します。

EDIT: 現在/etc/dhcp/dhclient.conf

# Configuration file for /sbin/dhclient. 
# 
# This is a sample configuration file for dhclient. See dhclient.conf's 
#  man page for more information about the syntax of this file 
#  and a more comprehensive list of the parameters understood by 
#  dhclient. 
# 
# Normally, if the DHCP server provides reasonable information and does 
#  not leave anything out (like the domain name, for example), then 
#  few changes must be made to this file, if any. 
# 

option rfc3442-classless-static-routes code 121 = array of unsigned integer 8; 

send host-name = gethostname(); 
request subnet-mask, broadcast-address, time-offset, routers, 
     domain-name, domain-name-servers, domain-search, host-name, 
     dhcp6.name-servers, dhcp6.domain-search, dhcp6.fqdn, dhcp6.sntp-servers, 
     netbios-name-servers, netbios-scope, interface-mtu, 
     rfc3442-classless-static-routes, ntp-servers; 

私がリクエストから始まる行からホスト名を削除する必要があります。

予想:

# Configuration file for /sbin/dhclient. 
# 
# This is a sample configuration file for dhclient. See dhclient.conf's 
#  man page for more information about the syntax of this file 
#  and a more comprehensive list of the parameters understood by 
#  dhclient. 
# 
# Normally, if the DHCP server provides reasonable information and does 
#  not leave anything out (like the domain name, for example), then 
#  few changes must be made to this file, if any. 
# 

option rfc3442-classless-static-routes code 121 = array of unsigned integer 8; 

send host-name = gethostname(); 
request subnet-mask, broadcast-address, time-offset, routers, 
     domain-name, domain-name-servers, domain-search, 
     dhcp6.name-servers, dhcp6.domain-search, dhcp6.fqdn, dhcp6.sntp-servers, 
     netbios-name-servers, netbios-scope, interface-mtu, 
     rfc3442-classless-static-routes, ntp-servers; 

よろしく

+3

期待される出力はどのように見えますか?適切なサンプルと期待される出力で明確に更新できますか? – Inian

+0

現在の出力と期待される出力で編集されました – LukeJ

+0

'sed'コマンドで削除されるテキストとして' host-name'ではなく 'host-name'を単にテキストとして使用してみませんか? – Sundeep

答えて

1

あなたは自分の具体的な例では、「ホスト名」の第二の外観を削除したい場合は、文字列を除去することによって、それが簡単に作ることができる "ホスト-name、 "、それは次のようになります。

sed -e 's/ host-name,//g' 

だからと正確に一致する文字列には影響しないだろう『ホスト名』

+0

@Inianあなたの編集に関して、私は二重引用符をまあ、それは私のために動作しませんでした。私はちょうどここでそれについて尋ねました:https://unix.stackexchange.com/questions/407479/use-of-sed-with-double-quotation –

+1

「N、M」の範囲をsedと一緒に使って、変更は希望の(17番目の)行 'sed '17/host-name、// g''でのみ行われます。これは期待どおりに動作するはずです。 – PesaThe

2

sed(またはその逆)内でAwkを実行するのは実際には意味がありません。 Awkでsed(またはgrep)でできることは何でもできます。grepまたはsedとAwkのパイプラインがある場合、通常はAwkですべてリファクタリングできます。

awk '/host-name/ {if (++j == 2) sub(", host-name", ",") }1' /etc/dhcp/dhclient.conf 

カウンタjは、正規表現を見た回数を記録しています。 2番目の発生時に置換を実行します。最後の1は、各入力行を標準出力に出力すると言います。

GNU Awk(gawk)をお持ちの場合は、その場所でファイルを編集するように要求できます(読み込み、編集、書き戻し)。 Awkが完了したら、出力を一時ファイルに保存して元のファイルに移動することもできます。

関連する問題