好的,我是 Unix 的新手,我正在嘗試學習 Unix 的基礎知識。我有一個名為 adafile 的檔案,我正在嘗試配置它,只是在玩弄它。我基本上想為第 2 列中的數字(即 1、3、5、6、4、6、2、4)列印小數點后第二位的數字。
File Output:
3.0 .91 7 36 January JAN Wayne Smith
2.8 .83 4 17 February FEB Eric Kittle
5.1 .95 8 15 March MAR Morgan Leach
9.0 .76 7 19 April APR Jane Holder
3.5 .84 4 43 October OCT Victor Carter
5.2 .98 6 13 November NOV Michael John
4.5 .86 2 8 December DEC Harry Kaka
5.7 .96 4 13 September SEP Tim Simple
我如何在不使用腳本的情況下完成這項作業?我嘗試使用命令:nawk '{print $2}' adafile
Output:
.91
.83
.95
.76
.84
.98
.86
.96
- 但我知道它會在第二列中顯示所有內容。
我應該使用 nawk 命令還是有更好的命令?
如果我能得到任何幫助,我將不勝感激!
uj5u.com熱心網友回復:
我會AWK按照以下方式利用 GNU,讓file.txt內容成為
File Output:
3.0 .91 7 36 January JAN Wayne Smith
2.8 .83 4 17 February FEB Eric Kittle
5.1 .95 8 15 March MAR Morgan Leach
9.0 .76 7 19 April APR Jane Holder
3.5 .84 4 43 October OCT Victor Carter
5.2 .98 6 13 November NOV Michael John
4.5 .86 2 8 December DEC Harry Kaka
5.7 .96 4 13 September SEP Tim Simple
然后
awk 'NR>1{print $2*100}' file.txt
給出輸出
1
3
5
6
4
8
6
6
解釋:對于超出第一個 ( NR>1) 的行,將第二個欄位值乘以,100然后將除法 ( %) 的余數乘以10。
(在 GNU Awk 5.0.1 中測驗)
uj5u.com熱心網友回復:
gawk '$_ = substr($(_ = _),_ --_,_--)' nawk '$-__= substr($(_=NF), _,!__)' FS='^.*[.]' mawk '$!NF = substr($(_=NF), _,!__)' FS='^.*[.]'
1
3
5
6
4
8
6
6
uj5u.com熱心網友回復:
如果不使用腳本,我猜是:
$ cut -c7 file
1
3
5
6
4
8
6
6
從您發布的示例輸入中獲取發布的預期輸出,但對于其他值使用 YMMV,所以如果我是您,我會使用腳本,例如:
$ awk '{print ( match($2,/\./) ? substr($2,RSTART 2,1) 0 : 0 )}' file
1
3
5
6
4
8
6
6
即使 $0 中的值沒有小數點或有但只有 1 位數字而不是示例中的 2 位數字,這將產生數字輸出,例如:
$ cat file
3.0 .91 7 36 January JAN Wayne Smith
2.8 .8 4 17 February FEB Eric Kittle
5.7 2 4 13 September SEP Tim Simple
$ awk '{print ( match($2,/\./) ? substr($2,RSTART 2,1) 0 : 0 )}' file
1
0
0
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/537365.html
標籤:unixawk
