awk 中 FS的用法

在openwrt文件 ar71xx.sh中

查询设备类型时,有这么一句,

machine=$(awk 'BEGIN{FS="[ ]+:[ ]"} /machine/ {print $2}' /proc/cpuinfo)
解决:
1)查看cpuinfo
root@hbg:/dev# cat  /proc/cpuinfo
system type             : Qualcomm Atheros QCA9533 rev 2
machine                 : Atheros AP143 reference board
processor               : 0
cpu model               : MIPS 24Kc V7.4
BogoMIPS                : 432.53
wait instruction        : yes
microsecond timers      : yes
tlb_entries             : 16
extra interrupt vector  : yes
hardware watchpoint     : yes, count: 4, address/irw mask: [0x0000, 0x0ff8, 0x0ff8, 0x0ff8]
isa                     : mips1 mips2 mips32r1 mips32r2
ASEs implemented        : mips16
shadow register sets    : 1
kscratch registers      : 0
core                    : 0
VCED exceptions         : not available
VCEI exceptions         : not available
2)awk截取信息, FS为指定格式; 以 “:” 冒号为分界符,两边都是制表符" ",第二个参数
   /machine指定了以machine为开始的那一行
  
所以命令的结果是:
Atheros AP143 reference board  

==============================================

补充知识:

awk中常见的内建变量(Built-in Variables)

NF( Number of Fields ) : awk 读入一行数据的字段数,通俗地说,就是一行数据被划分成了几段?便于对各字段进行遍历
NR( Number of Records ) : awk 已读入的行数,相当于一个计数器。
RS( Record Separator ) : 行分隔符。awk从文件上读取资料时, 将根据 RS 的定义把资料切割成许多Records,awk一次仅读入一个Record,以进行处理。预设值是' '
FS( Field Separator ) : 列分割符。决定了怎么将一行划分为几段。预设值是 空白符(空白和Tab)
FILENAME : awk 正在处理的数据文件名

例如:

创建一个 data.txt的文件,内如如下:(共3行,其中第三行为空)

a:b   c:d    123

w:q   d:e    234

执行命令:

hbg@root:~/dl/test$ awk 'BEGIN{FS="[ :]+"}{print $0, $1, NR, NF}' data.txt
a:b c:d 123 a 1 5
q:w d:e 234 q 2 5
  3 0
hbg@root:~/dl/test$ awk 'BEGIN{FS="[ :]+"}{print $0}' data.txt       // 打印所有数据
a:b c:d 123
q:w d:e 234

hbg@root:~/dl/test$ awk 'BEGIN{FS="[ :]+"}{print $1}' data.txt      // 打印分隔符的第一列
a
q

hbg@root:~/dl/test$ awk 'BEGIN{FS="[ :]+"}{print $2}' data.txt      // 打印分隔符的第二列
b
w

hbg@root:~/dl/test$ awk 'BEGIN{FS="[ :]+"}{print NR}' data.txt     // 打印行号
1
2
3
hbg@root:~/dl/test$ awk 'BEGIN{FS="[ :]+"}{print NF}' data.txt    // 段数(一行被分成了几段)
5
5
0
hbg@root:~/dl/test$

原文地址:https://www.cnblogs.com/rohens-hbg/p/5510890.html