#!/bin/bash #file=/dir1/dir2/dir3/my.file.txt #可以用${ }分别替换得到不同的值: #${file#*/}:删掉第一个 / 及其左边的字符串:dir1/dir2/dir3/my.file.txt #${file##*/}:删掉最后一个 / 及其左边的字符串:my.file.txt #${file#*.}:删掉第一个 . 及其左边的字符串:file.txt #${file##*.}:删掉最后一个 . 及其左边的字符串:txt #${file%/*}:删掉最后一个 / 及其右边的字符串:/dir1/dir2/dir3 #${file%%/*}:删掉第一个 / 及其右边的字符串:(空值) #${file%.*}:删掉最后一个 . 及其右边的字符串:/dir1/dir2/dir3/my.file #${file%%.*}:删掉第一个 . 及其右边的字符串:/dir1/dir2/dir3/my #【记忆的方法为】: # 是 去掉左边(键盘上#在 $ 的左边) #%是去掉右边(键盘上% 在$ 的右边) #单一符号是最小匹配;两个符号是最大匹配 #${file:0:5}:提取最左边的 5 个字节:/dir1 #${file:5:5}:提取第 5 个字节右边的连续5个字节:/dir2 #也可以对变量值里的字符串作替换: #${file/dir/path}:将第一个dir 替换为path:/path1/dir2/dir3/my.file.txt #${file//dir/path}:将全部dir 替换为 path:/path1/path2/path3/my.file.txt #result=$(cat < nginx_log.txt | grep '23/Apr/2020:2[0-3]:' | cut -d'-' -f1 | sort | uniq -c | wc -l ) #echo "${result}" #https://www.runoob.com/linux/linux-comm-sort.html result=$(cat < nowcoder.txt | cut -d'-' -f1 | sort -n -r | uniq -c) number=0 IFS=$'\n' for item in ${result}; do number=$( echo "${item}" | sed 's/^[ ]*//g' | cut -d " " -f1) if (( number > 3 )); then #用不了 ${item//search/replace} echo "${item}" | sed 's/^[ ]*//g' fi number=0 done exit 0
sort -n -r: 实现降序排序
sed 's/^[ ]*//g' :实现去除行前边的空格
IFS与 for结合,遍历数据