The awk script should produce a table that lists words with more than 4 letters and more than 2 occurrences. The last line of the output should display the number of words in the file.
輸入檔案
In navigation, the heading of a vessel or aircraft is the compass direction
in which the craft's bow or nose is pointed. Note that the heading may not
#necessarily be the direction that the vehicle actually travels, which is
known as its course or track. Any difference between the heading and
course is due to the motion of the underlying medium, the air or water,
or other effects like skidding or slipping. The difference is known as
the drift, and can be determined by the wind triangle. At least seven ways
to measure the heading of a vehicle have been described. A compass installed
in a vehicle or vessel has a certain amount of error caused by the magnetic
properties of the vessel. This error is known as compass deviation. The
magnitude of the compass deviation varies greatly depending upon the local
anomalies created by the vessel. A fiberglass recreational vessel will
#generally have much less compass deviation than a steel-hulled vessel.
Electrical wires carrying current have a small magnetic field around them
and can cause deviation. Any type of magnet, such as found in a speaker
can also cause large magnitudes of compass deviation. The error can be
corrected using a deviation table. Deviation tables are very difficult to
create. Once a deviation table is established, it is only good for that
particular vessel, with that particular configuration. If electrical wires
are moved or anything else magnetic (speakers, electric motors, etc.) are
moved, the deviation table will change. All deviations in the deviation
table are indicated west or east. If the compass is pointing west of the
Magnetic North Pole, then the deviation is westward. If the compass is
pointing east of the Magnetic North Pole, then the deviation is eastward.
撰寫一個 awk 腳本,從輸入檔案生成報告。該報告計算輸入檔案中某個單詞出現的次數。**
單獨實作忽略標點符號的附加功能,例如
".,;:()".*
cat input.txt|awk -F" "'{for(i=1;i<=NF;i ) a[$i] } END {for(k in a) print k,a[k]}'
我期望的結果在此處輸入影像描述
uj5u.com熱心網友回復:
注意:這看起來(對我來說)像一個家庭作業,所以我只是要解決當前的代碼。
awk '{ for (i=1;i<=NF;i ) a[$i] } # remains the same
END { for (k in a) {
total =a[k] # keep track of total word count
if ( a[k] > 2 && length(k) > 4 ) # apply filters to limit output
print k,a[k]
}
printf "\nTotal: %s\n", total
}
' input.txt
這會產生:
compass 8
deviation 8 # 1 for 'Deviation' ?
deviation. 3 # need to strip "."
error 3
heading 4
known 3
magnetic 3 # 2 for 'Magnetic' ?
table 3 # 1 for 'table.' ?
vehicle 3
vessel 3 # 1 for 'vessel,' ?
vessel. 3 # need to strip "."
# 2 for 'moved' & 'moved.' ?
# 2 for 'electrical' & 'Electrical' ?
Total: 292
筆記:
- 沒有提供排序要求(我通過管道
sort -f生成此輸出,以便更容易看到需要解決的潛在問題) - 假設 OP 所需的輸出(在影像中)是預期輸出的子集(例如,
vehicle未出現在 OP 的輸出中;我的腳本compass 8在 OP(影像)顯示時找到compass 7) - 假設
number of words in file適用于所有單詞,而不僅僅是length > 4和的單詞count > 2(我292在 OP 的輸出顯示時發現271);否則 OP 將需要添加邏輯來確定是否a[$i]應該執行 - 在執行
a[$i](提示:gsub()或gensub()函式)之前, OP 需要實作額外的邏輯來去除標點符號 - OP 是否需要實作不區分大小寫的單詞存盤以進行計數?如果是這樣,這將增加某些單詞的點擊次數(例如,
magnetic和deviation)(提示:tolower()功能)
uj5u.com熱心網友回復:
$ cat awk.script
#!/usr/bin/env awk -f
BEGIN {
print "\tWord Count\n--------------------"
} /vessel/ {
vessel ;next
} /compass/ {
compass
} /known/ {
known
} /table/ {
table
} /heading/ {
heading
} /magnetic/ {
mag
} /deviation/ {
dev
} /error/ {
error
} END {
print "\tvessel "vessel "\n\tcompass "compass "\n\tknown "known "\n\ttable "table "\n\theading "heading "\n\tmagnetic "mag "\n\tdeviation "dev "\n\terror "error "\n--------------------\nNumber of words: " total
} ; {
total=vessel compass known table heading mag dev error
}
$ awk -f awk.script RS=" " input_file
Word Count
--------------------
vessel 7
compass 8
known 3
table 5
heading 4
magnetic 3
deviation 12
error 3
--------------------
Number of words: 45
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/338733.html
上一篇:在git別名中設定環境變數
下一篇:使用grep洗掉字串中的重復項
