これは私の最初の質問ですので、私が何かを見逃している場合はお知らせください。awk配列は、印刷時にそれ自身をオーバーライドする
これは配列を使用してキーと値のペアを作るawkスクリプトです。
私はコロンで区切られたヘッダー情報を持つファイルを持っています。データはその下にあり、コロンで区切られています。私の目標は、新しいファイルにプリントアウトするキーと値のペアを作ることです。私はすべて配列に配置するように設定しており、それはほぼを完全に出力します。ここ
が入力される:ここ
...:iscsi_name:iscsi_alias:panel_name:enclosure_id:canister_id:enclosure_serial_number
...:iqn.1111-00.com.abc:2222.blah01.blah01node00::11BLAH00:::
コードである:ここ
#!/bin/awk -f
BEGIN {
FS = ":"
}
{
x = 1
if (NR==1) {
num_fields = NF ###This is done incase there are uneven head fields to data fields###
while (x <= num_fields) {
head[x] = $x
x++
}
}
y = 2
while (y <= NR) {
if (NR==y) {
x = 1
while (x <= num_fields) {
data[x] = $x
x++
}
x = 1
while (x <= num_fields) {
print head[x]"="data[x]
x++
}
}
y++
}
}
END {
print "This is the end of the arrays and the beginning of the test"
print head[16]
print "I am head[16]-"head[16]"- and now I'm going to overwrite everything"
print "I am data[16]-"data[16]"- and I will not overwrite everything, also there isn't any data in data[16]"
}
を出力する。
...
iscsi_name=iqn.1111-00.com.abc
iscsi_alias=2222.blah01.blah01node00
panel_name=
enclosure_id=11BLAH00
canister_id=
=nclosure_serial_number ### Here is my issue ###
This is the end of the arrays and the beginning of the test
enclosure_serial_number
- and now I'm going to overwrite everything
I am data[16]-- and I will not overwrite everything, also there isn't any data in data[16]
注:data [16]は何らかの理由で行末にないため、データ行に余分なコロンがあるため、上記のnum_fieldsの注記があります。
head [16]はなぜ自分自身を上書きしますか?フィールドの最後に改行(\ n)があるのでしょうか?もしそうなら、どうすればそれを取り除くことができますか?私は最後の文字を引くことを追加しようとしましたが、運はありません。私は配列がそのフィールドで運ぶことができる文字数を制限しようとしましたが、運はありません。私はもっと多くのアイデアを試しました、運はありません。 完全開示:私はこのすべてに対して比較的新しいです、私はこれらの以前の修正を台無しにしたかもしれません!
これはなぜ起こっているのですか? ありがとう! -cheezter88
TL; DRが、 awkの 'dos2unix'や' sub(/ \ r /、 ""、$ NF) 'を使用してください)。 –
ところで、あなたのスクリプトには最後の2回の印刷呼び出しで一重引用符が付きます。 – thanasisp
私は下のコメントを投稿した後、ちょうどこれを見た、ありがとう!すべてをLinuxに転送する。私は文字どおり机に向かって頭を叩いていましたが、それはちょっとばかりのWindowsです。 –