awk

实例

  • 分隔文件并取第一列
$  awk -F":" '{print $1}'  /etc/passwd

##
# User Database
#
# Note that this file is consulted directly only when the system is running
# in single-user mode.  At other times this information is provided by
# Open Directory.
#
# See the opendirectoryd(8) man page for additional information about
# Open Directory.
##
nobody
root
daemon
_uucp
_taskgated
  • 查询以 # 开头的行
$  awk '/#/{print $0}' /etc/passwd
##
# User Database
#
# Note that this file is consulted directly only when the system is running
# in single-user mode.  At other times this information is provided by
# Open Directory.
#
# See the opendirectoryd(8) man page for additional information about
# Open Directory.
##
  • 查询不以 # 开头的行
$  awk '!/#/{print $0}' /etc/passwd
nobody:*:-2:-2:Unprivileged User:/var/empty:/usr/bin/false
root:*:0:0:System Administrator:/var/root:/bin/sh
daemon:*:1:1:System Services:/var/root:/usr/bin/false
_uucp:*:4:4:Unix to Unix Copy Protocol:/var/spool/uucp:/usr/sbin/uucico
_taskgated:*:13:13:Task Gate Daemon:/var/empty:/usr/bin/false
_networkd:*:24:24:Network Services:/var/networkd:/usr/bin/false
  • 对文本内容重新编排
$  cat a
a,1
b,2
c,3
d,4
e,5
f,6

$  awk -F',' 'BEGIN{OFS=","}{print $2,$1}' a
1,a
2,b
3,c
4,d
5,e
6,f
  • 提取前10行
    awk 'NR<12' result.txt
    

https://www.cnblogs.com/xudong-bupt/p/3721210.html

results matching ""

    No results matching ""