192.统计词频
https://leetcode.cn/problems/word-frequency/solution/qie-ge-pai-xu-dan-ci-tong-ji-ci-shu-pai-8sdgt/
# Read from the file words.txt and output the word frequency list to stdout.
cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -r | awk '{ print $2, $1 }'
193.有效电话号码
(xxx) xxx-xxxx 或 xxx-xxx-xxxx。(x 表示一个数字)
https://leetcode.cn/problems/valid-phone-numbers/solution/zheng-ze-biao-da-shi-zhong-xian-ding-fu-yu-ding-we/
# Read from the file file.txt and output all valid phone numbers to stdout.
gawk '/^([0-9]{3}-|\([0-9]{3}\) )[0-9]{3}-[0-9]{4}$/' file.txt
194.转置文件
给定一个文件 file.txt,转置它的内容
https://leetcode.cn/problems/transpose-file/solution/194-chao-97de-wcawkxargsjie-fa-by-ffretu-a469/
# Read from the file file.txt and print its transposed content to stdout.
# 获取第一行,然后用wc来获取列数
COLS=`head -1 file.txt | wc -w`
# 使用awk依次去输出文件的每一列的参数,然后用xargs做转置
for (( i = 1; i <= $COLS; i++ )); do
# 这里col就是在代码里要替换的参数,而它等于$i
awk -v col=$i '{print $col}' file.txt | xargs
done
195.第十行
给定一个文本文件 file.txt,请只打印这个文件中的第十行。
https://leetcode.cn/problems/tenth-line/solution/shuang-bai-jie-da-by-ffreturn-68bk/
# Read from the file file.txt and output the tenth line to stdout.
# Read from the file file.txt and output the tenth line to stdout.
sed -n "10p" file.txt