Shell gawk統計檔案的各個單詞數

2021-07-25 12:02:10 字數 995 閱讀 9813

gawk 'begin

}end

}' datafile

datafile的內容是以空格分開,使用輸入字段分割符fs來區分每一行的單詞;

通過使用內建變數nf(當前行的字段總數);

通過使用以單詞作為索引的陣列,累加;

通過end輸出陣列和index就可得到

cat datafile

this is the header line.

this is the

first data line.

this is the

second data line.

this is the

last

line.

執行上述gawk

執行結果

first

1line. 4

the4

this 4

last

1second

1data 2

header 1

is 4

如果需要將輸出按照降序排列,可以把

print test, varr[test]
改為

print varr[test], test
然後通過管道

gawk

'begin

}end

}' datafile | sort -n -r | gawk ''

執行結果:

this 4

the4

line. 4

is 4

data 2

second

1last

1header 1

first

1

這就是通過gawk命令對檔案進行統計各個單詞的方法

mysql查詢各專業人數 SQL統計各專業學生人數

統計 學生 表中學生的總人數的sql語句是 select count from student 其中select代表查詢,count 是統計行數量,student是學生表,使用上述語句可以統計學生表中的所有行記錄也就是學生的總人數。擴充套件資料 常用sql語句介紹 1 查詢指定列 sql selec...

linux各檔案的作用

boot 系統啟動相關的檔案,如核心 initrd,以及grub bootloader dev 裝置檔案 裝置檔案 塊裝置 隨機訪問,資料塊 字元裝置 線性訪問,按字元為單位 裝置號 主裝置號 major 和次號 minor etc 配置檔案 home 使用者的家目錄,每乙個使用者的家目錄通常預設為...

linux的各資料夾

目錄名稱 意義 bin 該目錄中存放linux的常用命令,在有的版本中是一些和根目錄下相同的目錄。boot 該目錄下存放的都是系統啟動時要用到的程式,當用lilo引導linux時,會用到這裡的一些資訊 dev 該目錄包含了linux系統中使用的所有外部裝置,它實際上是訪問這些外部裝置的埠,你可以訪問...