Linux 상용 명령 을 깊이 해석 합 니 다 -- awk

  • 1. 개술
  • 2. awk 명령 행 형식
  • 3. awk 예시
  • 1) 모든 파일 내용 인쇄
  • 2) 인쇄 부분 열
  • 3) 모드 매 칭
  • 4) 정확 한 일치
  • 5) 일치 하지 않 음
  • 6) 조건/논리 조합
  • 7) 내장 변수
  • 8) 계산
  • 9) 신규 수출 내용
  • 4. 주의사항

  • 1. 개술
    awk 는 기능 이 매우 강 한 텍스트 처리 명령 으로 본 고 는 주로 자주 사용 하 는 텍스트 처리 방법 을 소개 한다.
    2. awk 명령 행 형식
    형식 1: awk '{[pattern] action}' 파일 이름
    형식 2: awk - F\# 분할 문자 지정
    형식 1:
    [root@smart Desktop]# cat test.log
    Apr 21 15:34:49 smart user: hello
    Apr 21 15:34:59 smart user: hello2
    Apr 22 15:45:55 smart test1.bin: test1.c main 15 
    Apr 22 15:46:08 smart test1.bin: test1.c main 15 
    [root@smart Desktop]# awk '{print $5}' test.log
    user:
    user:
    test1.bin:
    test1.bin:
    [root@smart Desktop]# 
    [root@smart Desktop]# awk '{print}' test.log
    Apr 21 15:34:49 smart user: hello
    Apr 21 15:34:59 smart user: hello2
    Apr 22 15:45:55 smart test1.bin: test1.c main 15 
    Apr 22 15:46:08 smart test1.bin: test1.c main 15 

    형식 2:
    [root@smart Desktop]# awk -F: '{print $1}' test.log
    Apr 21 15
    Apr 21 15
    Apr 22 15
    Apr 22 15
    [root@smart Desktop]# awk -F: '{print $2}' test.log
    34
    34
    45
    46
    [root@smart Desktop]# awk -F: '{print $3}' test.log
    49 smart user
    59 smart user
    55 smart test1.bin
    08 smart test1.bin
    [root@smart Desktop]# 

    3. awk 예제
    [root@smart Desktop]# cat table.txt
    Index  Name       Sex     Age     Date        Location  
     1     Xiaohong   Man     10      1990/1/2    Shenzhen
     2     Xiaoming   Woman   10      1990/1/3    Shenzhen
     3     Xiaogang   Man     10      1990/1/4    Shenzhen

    1) 모든 파일 내용 인쇄
    [root@smart Desktop]# awk '{print}' table.txt
    Index  Name       Sex     Age     Date        Location  
     1     Xiaohong   Man     10      1990/1/2    Shenzhen
     2     Xiaoming   Woman   10      1990/1/3    Shenzhen
     3     Xiaogang   Man     10      1990/1/4    Shenzhen
    
    [root@smart Desktop]# awk '{print $0}' table.txt
    Index  Name       Sex     Age     Date        Location  
     1     Xiaohong   Man     10      1990/1/2    Shenzhen
     2     Xiaoming   Woman   10      1990/1/3    Shenzhen
     3     Xiaogang   Man     10      1990/1/4    Shenzhen 

    2) 부분 열 인쇄
    [root@smart Desktop]# awk '{print $1}' table.txt
    Index
    1
    2
    3
    [root@smart Desktop]# awk '{print $2}' table.txt
    Name
    Xiaohong
    Xiaoming
    Xiaogang

    3) 패턴 일치
    [root@smart Desktop]# awk '/user/' test.log
    Apr 21 15:34:49 smart user: hello
    Apr 21 15:34:59 smart user: hello2
    [root@smart Desktop]# awk '/1\/3/'  table.txt
     2     Xiaoming   Woman   10      1990/1/3    Shenzhen
    [root@smart Desktop]# awk '/1\/3/  {print $2}'  table.txt
    Xiaoming
    [root@smart Desktop]# 

    4) 정확 한 일치
    [root@smart Desktop]# awk '$2=="Xiaoming" {print}' table.txt
     2     Xiaoming   Woman   10      1990/1/3    Shenzhen

    5) 일치 하지 않 음
    [root@smart Desktop]# awk '/Xiaoming/ {print}' table.txt
     2     Xiaoming   Woman   10      1990/1/3    Shenzhen
    [root@smart Desktop]# awk '!/Xiaoming/ {print}' table.txt
    Index  Name       Sex     Age     Date        Location  
     1     Xiaohong   Man     10      1990/1/2    Shenzhen
     3     Xiaogang   Man     10      1990/1/4    Shenzhen

    6) 조건/논리 조합
    [root@smart Desktop]# awk '$1>=2 {print}' table.txt
    Index  Name       Sex     Age     Date        Location  
     2     Xiaoming   Woman   10      1990/1/3    Shenzhen
     3     Xiaogang   Man     10      1990/1/4    Shenzhen
    [root@smart Desktop]# awk '$1>1 && $1<3  {print}' table.txt
     2     Xiaoming   Woman   10      1990/1/3    Shenzhen
    [root@smart Desktop]# awk '$2=="Xiaoming"||$2=="Xiaohong"  {print}' table.txt
     1     Xiaohong   Man     10      1990/1/2    Shenzhen
     2     Xiaoming   Woman   10      1990/1/3    Shenzhen
    [root@smart Desktop]# awk '/Xiaoming|Xiaohong/ {print}' table.txt
     1     Xiaohong   Man     10      1990/1/2    Shenzhen
     2     Xiaoming   Woman   10      1990/1/3    Shenzhen
    [root@smart Desktop]# 
    
    

    7) 내장 변수
    NR: 읽 은 기록 수
    NF: 현재 기록 필드 총수
    [root@smart Desktop]# awk '{print NF,NR,$0}' table.txt
    6 1 Index  Name       Sex     Age     Date        Location  
    6 2  1     Xiaohong   Man     10      1990/1/2    Shenzhen
    6 3  2     Xiaoming   Woman   10      1990/1/3    Shenzhen
    6 4  3     Xiaogang   Man     10      1990/1/4    Shenzhen
    [root@smart Desktop]# awk '{print NF,$0}' table.txt
    6 Index  Name       Sex     Age     Date        Location  
    6  1     Xiaohong   Man     10      1990/1/2    Shenzhen
    6  2     Xiaoming   Woman   10      1990/1/3    Shenzhen
    6  3     Xiaogang   Man     10      1990/1/4    Shenzhen
    [root@smart Desktop]# awk '{print NR,$0}' table.txt
    1 Index  Name       Sex     Age     Date        Location  
    2  1     Xiaohong   Man     10      1990/1/2    Shenzhen
    3  2     Xiaoming   Woman   10      1990/1/3    Shenzhen
    4  3     Xiaogang   Man     10      1990/1/4    Shenzhen

    8) 계산
    [root@smart Desktop]# awk '$4=$4+4 {print $0}' table.txt
    Index Name Sex 4 Date Location
    1 Xiaohong Man 14 1990/1/2 Shenzhen
    2 Xiaoming Woman 14 1990/1/3 Shenzhen
    3 Xiaogang Man 14 1990/1/4 Shenzhen
    [root@smart Desktop]# awk '{if($4!="Age") $4=$4+4}{ print $0}' table.txt
    Index  Name       Sex     Age     Date        Location  
    1 Xiaohong Man 14 1990/1/2 Shenzhen
    2 Xiaoming Woman 14 1990/1/3 Shenzhen
    3 Xiaogang Man 14 1990/1/4 Shenzhen
    [root@smart Desktop]# 
    [root@smart Desktop]# awk '{if($4!="Age") $4=$4+4  print $0}' table.txt
    awk: {if($4!="Age") $4=$4+4  print $0}
    awk:                         ^ syntax error
    [root@smart Desktop]# awk '{if($4!="Age") $4=$4+4;  print $0}' table.txt
    Index  Name       Sex     Age     Date        Location  
    1 Xiaohong Man 14 1990/1/2 Shenzhen
    2 Xiaoming Woman 14 1990/1/3 Shenzhen
    3 Xiaogang Man 14 1990/1/4 Shenzhen
    [root@smart Desktop]# 
    

    9) 출력 내용 추가
    [root@smart Desktop]# awk 'BEGIN{print 1 2 3} {print $0}' table.txt
    123
    Index  Name       Sex     Age     Date        Location  
     1     Xiaohong   Man     10      1990/1/2    Shenzhen
     2     Xiaoming   Woman   10      1990/1/3    Shenzhen
     3     Xiaogang   Man     10      1990/1/4    Shenzhen
    [root@smart Desktop]# awk 'BEGIN{print "1 2 3"} {print $0}' table.txt
    1 2 3
    Index  Name       Sex     Age     Date        Location  
     1     Xiaohong   Man     10      1990/1/2    Shenzhen
     2     Xiaoming   Woman   10      1990/1/3    Shenzhen
     3     Xiaogang   Man     10      1990/1/4    Shenzhen
    [root@smart Desktop]# 

    4. 주의사항
    1) 작은 따옴표 와 작은 따옴표
    특히 awk 와 셸 스 크 립 트 에서 작은 따옴표 와 작은 따옴표 가 서로 포 함 된 차이 점 에 주의 하 십시오.
    [root@smart Desktop]# awk "{print}" table.txt
    Index  Name       Sex     Age     Date        Location  
     1     Xiaohong   Man     10      1990/1/2    Shenzhen
     2     Xiaoming   Woman   10      1990/1/3    Shenzhen
     3     Xiaogang   Man     10      1990/1/4    Shenzhen
     [root@smart Desktop]# awk "$2=="Xiaoming" {print}" table.txt
    awk: ==Xiaoming {print}
    awk: ^ syntax error
    [root@smart Desktop]# awk "$2=='Xiaoming' {print}" table.txt
    awk: =='Xiaoming' {print}
    awk: ^ syntax error
    [root@smart Desktop]# 
    [root@smart Desktop]# A=1
    [root@smart Desktop]# B=2
    [root@smart Desktop]# echo "'$A' $B"
    '1' 2
    [root@smart Desktop]# echo ''$A' $B'
    1 $B
    [root@smart Desktop]# echo '"$A" $B'
    "$A" $B
    [root@smart Desktop]# 
    

    Juyin@2018/4/28, 댓 글 교류 환영

    좋은 웹페이지 즐겨찾기