コマンドラインを使用して、列のすべての項目に固定数を掛けます。ただし、その行がコメントでない場合にのみ適用されます。

コマンドラインを使用して、列のすべての項目に固定数を掛けます。ただし、その行がコメントでない場合にのみ適用されます。

努力しています:

  1. 常に0 0 0ファイルの最初の行に追加されます。
  2. 2*piまたは、次の形式に似た形式で3列ファイルの最初の列を掛けます6.2832。ただし、行が数字で始まる場合にのみ適用されます。 2番目と3番目の列はそのまま残ります。
  3. *行が数字で始まらない場合、aはすでにaではない限り、行の先頭に追加されます*。つまり、すでにコメントされていない限り、現在の行をコメントアウトするだけです。

以下はサンプル入力ファイルです。

* radius, section, index
1.12 A 0
2.0 A 1
   * There is white space before this comment
* This is a comment indicating a new section
5 B 0
3.17 B 1
7.3 B 7
This row starts with an alphabet char and should be commented out by the script.
0 C 1
1 C 2

予想される結果は次のとおりです。

0 0 0
* radius, section, index
7.037184 A 0
12.5664 A 1
* There is white space before this comment
* This is a comment indicating a new section
31.416 B 0
19.917744 B 1
45.86736 B 7
* This row starts with an alphabet char and should be commented out by the script.
0 C 1
6.2832 C 2

これまで私がしたこと:

for tempfile in *.txt; do
    echo '0 0 0' > temp
    cat $tempfile >> temp
    awk '{$1*=6.2832}{print}' temp > $tempfile
    #awk '/^(0-9)/{$1*=6.2832}{print}' temp > $tempfile
rm temp
done

しかし、上記の例のユースケースでは、このスクリプトは何を行いますか?

0 0 0
0 radius, section, index
7.03718 A 0
12.5664 A 1
0 There is white space before this comment
0 This is a comment indicating a new section
31.416 B 0
19.9177 B 1
45.8674 B 7
0 row starts with an alphabet char and should be commented out by the script.
0 C 1
6.2832 C 2

PS。 Linuxボックスはスタンドアロンで、mlrルート/管理者の資格情報もありません。

コミュニティのどんな助けにも感謝します。よろしくお願いします。

ベストアンサー1

POSIX awkを使用してください。

$ cat tst.awk
NR == 1 {
    CONVFMT = "%.17g"
    pi = atan2(0, -1)
    two_pi = 2 * pi
    print 0, 0, 0
}
{
    if ( $1 ~ /^[0-9]/ ) {
        $1 *= two_pi
    }
    else {
        sub(/^[[:space:]]*\*?[[:space:]]*/,"* ")
    }
    print
}

$ awk -f tst.awk file
0 0 0
* radius, section, index
7.03718 A 0
12.5664 A 1
* There is white space before this comment
* This is a comment indicating a new section
31.416 B 0
19.9177 B 1
45.8674 B 7
* This row starts with an alphabet char and should be ignored by the script.
0 C 1
6.2832 C 2

おすすめ記事