2
27
2013
1

Awk 学习

AWK 这货简直无情,先不说功能无比强大,关键是极大地弥补了shell渣的各种缺陷,是非常耐玩的人妻语言

--more--

ARGC ARGV FILENAME FNR(F=file) NR(R=row) FS(S=sep) RS OFS(O=output) ORS NF(F=field) 顾名思义。

 

!注意 NF是一共多少列,NR是当前行号,AWK没有统计行数功能的样子。

 

awk '/str/{ print $1"*"$2 }'    对包含str的行输出。

awk 'gsub(/str/,"def"){ gsub(/def/,"spd"); print $0 }'    对包含str的行(过滤),先替换为def,然后把所有def替换为spd。

 

 

把 f1 f2 f3 合并到 fall,再把 fall 分割成 f1 f2 f3,挺nb的。

awk '{ print FILENAME$0 }' f1 f2 f3 >fall

awk ' $1 != prev{ close(prev); prev=$1 } { print substr( $0, 1+index($0," ") ) > $1 }' fall

 

 

awk '{"ls -alh"|getline d; print NR, d }'    每次输出一行最后重复

 

引用 shell 变量:

(1) awk 'BEGIN{ $F=1; print $F; print '$F' }'

(2) (只能传数字)F=1; awk 'BEGIN{ print '$F' }'

(3) 2013-01-17 又学到一招! F=abc; awk 'BEGIN{ print " '$F' " }' (双 单 $F 单 双)

 

 

后面引用别人的:

 

  • $ awk '/^(no|so)/' test-----打印所有以模式no或so开头的行。

  • $ awk '/^[ns]/print $1 }' test-----如果记录以n或s开头,就打印这个记录。

  • $ awk '$1~/[0-9][0-9]$/{ print $1 }' test-----如果第一个域以两个数字结束就打印这个记录。

  • $ awk '$1== 10 || $2 < 5' test-----如果第一个等于10或者第二个域小于5,则打印该行。

  • $ awk '/^A/,/^B/' test    如 AAABBXXAAACCXXAAADDXX 输出 AAABAAACAAAD 。

     
Category: 未分类 | Tags: linux | Read Count: 3332
Avatar_small
Emma 说:
2023年1月31日 18:34

AWK is an incredibly powerful programming language and an indispensable tool for shell scripting. It offers a number of features that help to make up for the shortcomings of other scripting languages. In particular, the use of ARGC, ARGV, FILENAME, FNR, NR, FS, RS, OFS, ORS and NF offers a comprehensive set of engagement wedding ring sets variables and functions for manipulating the data. For example, NF is the total number of columns, NR is the current line number and AWK does not have the function of counting the number of lines. Additionally, AWK can be used to search for specific strings, and replace them with different strings. All in all, AWK is an incredibly useful language and can be a great asset for scripting.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com