shell 命令记录

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
好记性不如烂键盘---点滴、积累、进步!
原文地址:https://www.cnblogs.com/yanghailin/p/14949043.html