head -1 vs head -n 1
两者等价
lscpu|grep CPU(s)|head -1
等价于:
lscpu|grep CPU(s)|head -n 1
都显示如下:
CPU(s): 8
awk 用法
https://www.runoob.com/linux/linux-comm-awk.html
awk '{[pattern] action}' {filenames} # 行匹配语句 awk '' 只能用单引号
# 每行按空格或TAB分割,输出文本中的1、4项
$ awk '{print $1,$4}' log.txt
---------------------------------------------
2 a
3 like
This's
10 orange,apple,mongo
# 格式化输出
$ awk '{printf "%-8s %-10s
",$1,$4}' log.txt
---------------------------------------------
2 a
3 like
This's
10 orange,apple,mongo
(lscpu|grep CPU(s)|head -1|awk '{print $2}')
显示8
shell将字符串分割为数组
https://blog.csdn.net/butterfly5211314/article/details/83095084
str="192.168.31.65"
array=(${str//./ })
for i in "${!array[@]}"; do
echo "$i=>${array[i]}"
done