我在 ubuntu 中有以下資料集,我想在 bash 中進行迭代(while 或 for),以便生成一個新列,其中包含失敗和通過主題之間的商。
id, name, country, Continent, grade, passed, failed
1, Louise Smith, UK, Europe, 7, 5, 1
2, Okio Kiomoto, Japan, Asia, 9, 5, 0
3, Ralph Watson, USA, Northern America, 5.6, 5, 2
4, Mary Mcaann, South Africa, Africa, 4.7, 5, 3
5, Jack Thomson, Australia, Oceania, 10, 5, 0
6, N'dongo Mbaye, Senegal, Africa, 7.9, 5, 1
為此,我嘗試在腳本中使用以下代碼。但我無法得到任何結果,因為我找不到任何方法將這個新生成的列添加到當前資料集中。有什么想法嗎?
while IFS=, read _ _ _ _ _ passed failed; do
newcolumn=$($passed/$failed |bc)
done
作為指導,所需的輸出如下。
id, name, country, Continent, grade, passed, failed, new
1, Louise Smith, UK, Europe, 7, 5, 1, 0.2
2, Okio Kiomoto, Japan, Asia, 9, 5, 0, 0
3, Ralph Watson, USA, Northern America, 5.6, 5, 2, 0.4
4, Mary Mcaann, South Africa, Africa, 4.7, 5, 3, 0.6
5, Jack Thomson, Australia, Oceania, 10, 5, 0, 0
6, N'dongo Mbaye, Senegal, Africa, 7.9, 5, 1, 0.2
謝謝
uj5u.com熱心網友回復:
使用awk
$ awk 'BEGIN { FS=OFS=", " } NR == 1 { $8="new" } NR > 1 { $8=$NF/$(NF-1) }1' input_file
id, name, country, Continent, grade, passed, failed, new
1, Louise Smith, UK, Europe, 7, 5, 1, 0.2
2, Okio Kiomoto, Japan, Asia, 9, 5, 0, 0
3, Ralph Watson, USA, Northern America, 5.6, 5, 2, 0.4
4, Mary Mcaann, South Africa, Africa, 4.7, 5, 3, 0.6
5, Jack Thomson, Australia, Oceania, 10, 5, 0, 0
6, N'dongo Mbaye, Senegal, Africa, 7.9, 5, 1, 0.2
uj5u.com熱心網友回復:
我對您的代碼進行了一些重構,并提出了以下內容:
#!/bin/bash
# create new header
header=$(awk 'NR==1 {print}' s.dat)
printf "%s, new\n" "${header}"
# read data file data rows
while IFS=, read a b c d e passed failed; do
newcolumn=0
# avoid divide-by-zero
if [[ "${passed}" -ne "0" ]] ; then
newcolumn=$(bc <<<"scale=2; ${failed} / ${passed}")
fi
# output data with new generated column
printf "%s %3.2f\n" "${a}, ${b}, ${c}, ${d}, ${e}, ${passed}, ${failed}, " "${newcolumn}"
done < <(awk 'NR!=1 {print}' s.dat)
s.dat 的內容:
id, name, country, Continent, grade, passed, failed
1, Louise Smith, UK, Europe, 7, 5, 1
2, Okio Kiomoto, Japan, Asia, 9, 5, 0
3, Ralph Watson, USA, Northern America, 5.6, 5, 2
4, Mary Mcaann, South Africa, Africa, 4.7, 5, 3
5, Jack Thomson, Australia, Oceania, 10, 5, 0
6, N'dongo Mbaye, Senegal, Africa, 7.9, 5, 1
執行腳本時的輸出:
id, name, country, Continent, grade, passed, failed, new
1, Louise Smith, UK, Europe, 7, 5, 1, 0.20
2, Okio Kiomoto, Japan, Asia, 9, 5, 0, 0.00
3, Ralph Watson, USA, Northern America, 5.6, 5, 2, 0.40
4, Mary Mcaann, South Africa, Africa, 4.7, 5, 3, 0.60
5, Jack Thomson, Australia, Oceania, 10, 5, 0, 0.00
6, N'dongo Mbaye, Senegal, Africa, 7.9, 5, 1, 0.20
uj5u.com熱心網友回復:
測驗并確認在gawk 5.1.1, mawk 1.3.4, mawk 1.9.9.6, 和macos nawk
______ # this pair of empty double quotes
/ # is *** essential ***, since it forces string
/ # compare, allowing rows getting "0" value in new
/ # column to print out properly
\
{m,n,g}awk '"" ($(_=NF = ! FS) = !/[0-9]/ ? "new" : \
($--_) / ( $--_ ? $_ : --_^--_^_))' FS = '[,][ \t]*'
OFS = ', '
————————————————————————————————————
id, name, country, Continent, grade, passed, failed, new
1, Louise Smith, UK, Europe, 7, 5, 1, 0.2
2, Okio Kiomoto, Japan, Asia, 9, 5, 0, 0
3, Ralph Watson, USA, Northern America, 5.6, 5, 2, 0.4
4, Mary Mcaann, South Africa, Africa, 4.7, 5, 3, 0.6
5, Jack Thomson, Australia, Oceania, 10, 5, 0, 0
6, N'dongo Mbaye, Senegal, Africa, 7.9, 5, 1, 0.2
————————————————————————————————————
(condensed version :::)
mawk '""($(_=NF =! FS)=!/[0-9]/?"new":($--_)/( $--_?$_:--_^--_^_) )' FS='[,][ \t]*' OFS=', '
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/478753.html
