awk built-in functions

本文介绍了GAWK的基本使用方法,包括数值与字符串操作、排序、替换、子串提取等功能,并详细展示了各种函数的应用实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考: http://www.gnu.org/software/gawk/

1. Numeric

int(x): return the nearest integer
rand(): return a random number
srand([x]): set the starting seed for random generator; if x is omitted, the current date and time are used for a seed.

$ awk 'BEGIN{print int(1.8), int(-1.8), int(010), int(0x0A), int(3a), int(b2)}'                                           
1 -1 8 10 3 0 

# rand()
function roll(n) { return 1 + int(rand() * n) }                                                                                                                                                                                                                                    
{ printf("%d points", roll(6) + roll(6) + roll(6)); } 

# fixed seed, unchanged random number
$ awk 'BEGIN{srand(1); print rand()}'                                                                                     
0.237788                                                                                                                          
$ awk 'BEGIN{srand(1); print rand()}'                                                                                     
0.237788                             

# the current date and time as a seed                                                                                             
$ awk 'BEGIN{srand(); print rand()}'                                                                                      
0.367979                                                                                                                          
$ awk 'BEGIN{srand(); print rand()}'                                                                                      
0.291809 

2. String

2.1 sort

2.1.1 sorted by values.

len = asort(a): a is changed, the indices are replaced by sequential integers starting with one
len = asort(a, b): a isn’t changed. b is new array that comprises sequential integer indices and values of a.

2.1.1 sorted by indeces.

len = asorti(a): s is changed. the indices change into values, the sequential integers act as the indices
len = asorti(a, b): s isn’t changed. b is new array that comprises sequential integer indices and values came from the indices of a.

$ cat afile                                                                                                               
last de                                                                                                                           
first sac                                                                                                                         
middle cul

$ awk '{a[$1]=$2} END{for(i in a) print i, a[i]}' afile                                                                   
first sac                                                                                                                         
middle cul                                                                                                                        
last de  

$ awk '{a[$1]=$2} END{len=asort(a); for(i=1;i<=len;i++) print i, a[i]}' afile                                             
1 cul                                                                                                                             
2 de                                                                                                                              
3 sac  

$ awk '{a[$1]=$2} END{len=asort(a,b); for(i=1;i<=len;i++) print i, b[i]}' afile                                           
1 cul                                                                                                                             
2 de                                                                                                                              
3 sac 

$ awk '{a[$1]=$2} END{len=asorti(a); for(i=1;i<=len;i++) print i, a[i]}' afile                                            
1 first                                                                                                                           
2 last                                                                                                                            
3 middle                                                                                                                          

$ awk '{a[$1]=$2} END{len=asorti(a,b); for(i=1;i<=len;i++) print i, b[i], a[b[i]]}' afile                                 
1 first sac                                                                                                                       
2 last de                                                                                                                         
3 middle cul   

2.2 substitution

result = gensub(regexp, replacement, how, [target]): “how” is g/G/1-n

$ gawk 'BEGIN{a="abc def"; b=gensub(/(.+) (.+)/, "\\2 \\1", "g", a); print b}'                                            
def abc  

$ echo "a b c a b c" | gawk '{s=gensub(/a/, "AA", 2); print s}'                                                           
a b c AA b c

sub(regexp, replacement [, target])
gsub(regexp, replacement [, target]): global sub()

$ awk 'BEGIN{s="water, water, everywhere"; sub(/at/,"ith",s); print s}'                                                   
wither, water, everywhere

# the precise substring (&) 
$ awk 'BEGIN{s="daabaaaa"; sub(/a+/, "C&C", s); print s}'                                                                 
dCaaCbaaaa

$ cat bfile                                                                                                               
American                                                                                                                          
Britain                                                                                                                           
China                                                                                                                             
Germany                                                                                                                           
France                                                                                                                            
$ awk '{gsub(/Britain/, "United Kingdom"); print}' bfile                                                                  
American                                                                                                                          
United Kingdom                                                                                                                    
China                                                                                                                             
Germany                                                                                                                           
France 

2.3 substring

substr(string, start [, length ])
split(string, array [, fieldsep [, seps ] ])

$ awk 'BEGIN{a="abcdefg";b=substr(a,1,2)"CDE"substr(a,6); print b}'                                                       
abCDEfg  

$ awk 'BEGIN{split("cul-de-sac",a,"-",seps); for(i in a) print "a["i"]="a[i]; for(j in seps) print "seps["j"]="seps[j]}'  
a[1]=cul                                                                                                                          
a[2]=de                                                                                                                           
a[3]=sac                                                                                                                          
seps[1]=-                                                                                                                         
seps[2]=-

2.4 others

pos = index(string, find)
len = length([string])

match(string, regexp [, array])

str = sprintf(format, expr-list)
num = strtonum(str)

tolower(str)
tolower(str)

$ awk 'BEGIN{print index("peanut", "an")}'                                                                                
3        

$ awk 'BEGIN{print length("peanut")}'                                                                                     
6    

# the older versions of awk, the length() function could be called without any parentheses, but forget it.                                                                                                                            
$ awk 'BEGIN{len=length "peanut"; print len}'                                                                             
0peanut

$ echo "foooobazbarrrrrr" | awk '{match($0, /(fo+).+(bar*)/, arr); print arr[1], arr[2]}'                                 
foooo barrrrrr

$ awk 'BEGIN{pi=sprintf("%.2f (approx.)", 22/7); print pi}'                                                                         
3.14 (approx.)   

$ awk 'BEGIN{s="0x11"; printf("%d\n", strtonum(s))}'                                                                    
17     

$ awk 'BEGIN{print toupper("cest la vie")}'                                                                               
CEST LA VIE                                                                                                                       
$ awk 'BEGIN{print tolower("Welcome You")}'                                                                               
welcome you      

3. IO

close(filename [, how]): “how” is “from” or “to”
fflush(filename)
system(command)

4. Time

mktime(“YYYY MM DD HH MM SS [DST]”)
strftime([format [, timestamp [, utc-flag] ] ])
systime(): the current time stamp since the system epoch

# default time format
$ awk 'BEGIN{print PROCINFO["strftime"]}'                                                                                                                          
%a %b %e %H:%M:%S %Z %Y 

$ awk 'BEGIN{print strftime()}'                                                                                                                                    
Mon Jul  6 09:29:13 EDT 2015

5. Bit-Manipulation

and(v1, v2 [, …])
or(v1, v2 [, …])
xor(v1, v2 [, …])
compl(val): the bitwise complement
lshift(val, count): shifted left by count bits
rshift(val, count): shifted right by count bits

$ cat bits2str.awk   
function bits2str(bits, data, mask)
{
    if (bits == 0)
        return "0"

    mask = 1;
    for (; bits != 0; bits = rshift(bits, 1))
        data = (and(bits, mask) ? "1" : "0") data

    while ((length(data) % 8) != 0)
        data = "0" data

    return data
}

BEGIN {
    printf "123 = %s\n", bits2str(123)
    printf "0123 = %s\n", bits2str(0123)
    printf "0x99 = %s\n", bits2str(0x99)
    comp = compl(0x99)
    printf "compl(0x99) = %#x = %s\n", comp, bits2str(comp)
    shift = lshift(0x99, 2)
    printf "lshift(0x99, 2) = %#x = %s\n", shift, bits2str(shift)
    shift = rshift(0x99, 2)
    printf "rshift(0x99, 2) = %#x = %s\n", shift, bits2str(shift)
}

$ awk -f bits2str.awk                                                                                                                                               
123 = 01111011                                                                                                                                                            
0123 = 01010011                                                                                                                                                           
0x99 = 10011001                                                                                                                                                           
compl(0x99) = 0x1fffffffffff66 = 00011111111111111111111111111111111111111111111101100110                                                                                 
lshift(0x99, 2) = 0x264 = 0000001001100100                                                                                                                                
rshift(0x99, 2) = 0x26 = 00100110 

6. Data Type(gawk 4.0+)

isarray(x): distinguish an array from a scalar variable

$ awk 'BEGIN{a[1]="one"; print isarray(a)}'                                                                                                                         
1                

$ awk 'BEGIN{a="one"; print isarray(a)}'                                                                                                                            
0       
# Makefile for GeekOS kernel, userspace, and tools # Copyright (c) 2004,2005 David H. Hovemeyer <daveho@cs.umd.edu> # $Revision: 1.45 $ # This is free software. You are permitted to use, # redistribute, and modify it as specified in the file "COPYING". # Required software to build GeekOS: # - GNU Make (http://www.gnu.org/software/make) # - gcc 2.95.2 generating code for target (i386/ELF) and host platforms # - nasm (http://nasm.sourceforge.net) # - Perl5, AWK (any version), egrep # # Cygwin (http://cygwin.com) may be used to build GeekOS. # Make sure that gcc, binutils, nasm, and perl are installed. # NOTES: # - This makefile has been written carefully to work correctly # with the -j (parallel make) option. I regularly use "make -j 2" # to speed the build process on 2 processor systems. PROJECT_ROOT := .. VPATH := $(PROJECT_ROOT)/src # Figure out if we're compiling with cygwin, http://cygwin.com SYSTEM_NAME := $(shell uname -s) ifeq ($(findstring CYGWIN,$(SYSTEM_NAME)),CYGWIN) SYM_PFX := _ EXTRA_C_OPTS := -DNEED_UNDERSCORE -DGNU_WIN32 EXTRA_NASM_OPTS := -DNEED_UNDERSCORE NON_ELF_SYSTEM := yes EXTRA_CC_USER_OPTS := -Dmain=geekos_main endif # ---------------------------------------------------------------------- # Configuration - # Various options specifying how GeekOS should be built, # what source files to build, which user programs to build, # etc. This is generally the only section of the makefile # that will need to be modified. # ---------------------------------------------------------------------- # List of targets to build by default. # These targets encompass everything needed to boot # and run GeekOS. ALL_TARGETS := fd.img # Kernel source files KERNEL_C_SRCS := idt.c int.c trap.c irq.c io.c \ keyboard.c screen.c timer.c \ mem.c crc32.c \ gdt.c tss.c segment.c \ bget.c malloc.c \ synch.c kthread.c \ main.c # Kernel object files built from C source files KERNEL_C_OBJS := $(KERNEL_C_SRCS:%.c=geekos/%.o) # Kernel assembly files KERNEL_ASM_SRCS := lowlevel.asm # Kernel object files build from assembler source files KERNEL_ASM_OBJS := \ $(KERNEL_ASM_SRCS:%.asm=geekos/%.o) # All kernel object files KERNEL_OBJS := $(KERNEL_C_OBJS) \ $(KERNEL_ASM_OBJS) # Common library source files. # This library is linked into both the kernel and user programs. # It provides string functions and generic printf()-style # formatted output. COMMON_C_SRCS := fmtout.c string.c memmove.c # Common library object files. COMMON_C_OBJS := $(COMMON_C_SRCS:%.c=common/%.o) # Base address of kernel KERNEL_BASE_ADDR := 0x00010000 # Kernel entry point function KERNEL_ENTRY = $(SYM_PFX)Main # ---------------------------------------------------------------------- # Tools - # This section defines programs that are used to build GeekOS. # ---------------------------------------------------------------------- # Uncomment if cross compiling #TARGET_CC_PREFIX := i386-elf- # Target C compiler. gcc 2.95.2 or later should work. TARGET_CC := $(TARGET_CC_PREFIX)gcc # Host C compiler. This is used to compile programs to execute on # the host platform, not the target (x86) platform. On x86/ELF # systems, such as Linux and FreeBSD, it can generally be the same # as the target C compiler. HOST_CC := gcc # Target linker. GNU ld is probably to only one that will work. TARGET_LD := $(TARGET_CC_PREFIX)ld # Target archiver TARGET_AR := $(TARGET_CC_PREFIX)ar # Target ranlib TARGET_RANLIB := $(TARGET_CC_PREFIX)ranlib # Target nm TARGET_NM := $(TARGET_CC_PREFIX)nm # Target objcopy TARGET_OBJCOPY := $(TARGET_CC_PREFIX)objcopy # Nasm (http://nasm.sourceforge.net) NASM := nasm # Tool to build PFAT filesystem images. BUILDFAT := tools/builtFat.exe # Perl5 or later PERL := perl # Pad a file so its size is a multiple of some unit (i.e., sector size) PAD := $(PERL) $(PROJECT_ROOT)/scripts/pad # Create a file filled with zeroes. ZEROFILE := $(PERL) $(PROJECT_ROOT)/scripts/zerofile # Calculate size of file in sectors NUMSECS := $(PERL) $(PROJECT_ROOT)/scripts/numsecs # ---------------------------------------------------------------------- # Definitions - # Options passed to the tools. # ---------------------------------------------------------------------- # Flags used for all C source files GENERAL_OPTS := -O -Wall $(EXTRA_C_OPTS) CC_GENERAL_OPTS := $(GENERAL_OPTS) -Werror # Flags used for kernel C source files CC_KERNEL_OPTS := -g -DGEEKOS -I$(PROJECT_ROOT)/include # Flags user for kernel assembly files NASM_KERNEL_OPTS := -I$(PROJECT_ROOT)/src/geekos/ -f elf $(EXTRA_NASM_OPTS) # Flags used for common library and libc source files CC_USER_OPTS := -I$(PROJECT_ROOT)/include -I$(PROJECT_ROOT)/include/libc \ $(EXTRA_CC_USER_OPTS) # Flags passed to objcopy program (strip unnecessary sections from kernel.exe) OBJCOPY_FLAGS := -R .dynamic -R .note -R .comment # ---------------------------------------------------------------------- # Rules - # Describes how to compile the source files. # ---------------------------------------------------------------------- # Compilation of kernel C source files geekos/%.o : geekos/%.c $(TARGET_CC) -c $(CC_GENERAL_OPTS) $(CC_KERNEL_OPTS) $< -o geekos/$*.o # Compilation of kernel assembly source files geekos/%.o : geekos/%.asm $(NASM) $(NASM_KERNEL_OPTS) $< -o geekos/$*.o geekos/%.o : geekos/%.S $(TARGET_CC) -c $(CC_GENERAL_OPTS) $(CC_KERNEL_OPTS) $< -o geekos/$*.o # Compilation of common library C source files common/%.o : common/%.c $(TARGET_CC) -c $(CC_GENERAL_OPTS) $(CC_USER_OPTS) $< -o common/$*.o # ---------------------------------------------------------------------- # Targets - # Specifies files to be built # ---------------------------------------------------------------------- # Default target - see definition of ALL_TARGETS in Configuration section all : $(ALL_TARGETS) # Standard floppy image - just boots the kernel fd.img : geekos/fd_boot.bin geekos/setup.bin geekos/kernel.bin cat geekos/fd_boot.bin geekos/setup.bin geekos/kernel.bin > $@ # Floppy boot sector (first stage boot loader). geekos/fd_boot.bin : geekos/setup.bin geekos/kernel.bin $(PROJECT_ROOT)/src/geekos/fd_boot.asm $(NASM) -f bin \ -I$(PROJECT_ROOT)/src/geekos/ \ -DNUM_SETUP_SECTORS=`$(NUMSECS) geekos/setup.bin` \ -DNUM_KERN_SECTORS=`$(NUMSECS) geekos/kernel.bin` \ $(PROJECT_ROOT)/src/geekos/fd_boot.asm \ -o $@ # Setup program (second stage boot loader). geekos/setup.bin : geekos/kernel.exe $(PROJECT_ROOT)/src/geekos/setup.asm $(NASM) -f bin \ -I$(PROJECT_ROOT)/src/geekos/ \ -DENTRY_POINT=0x`egrep 'Main$$' geekos/kernel.syms |awk '{print $$1}'` \ $(PROJECT_ROOT)/src/geekos/setup.asm \ -o $@ $(PAD) $@ 512 # Loadable (flat) kernel image. geekos/kernel.bin : geekos/kernel.exe $(TARGET_OBJCOPY) $(OBJCOPY_FLAGS) -S -O binary geekos/kernel.exe geekos/kernel.bin $(PAD) $@ 512 # The kernel executable and symbol map. geekos/kernel.exe : $(KERNEL_OBJS) $(COMMON_C_OBJS) $(TARGET_LD) -o geekos/kernel.exe -Ttext $(KERNEL_BASE_ADDR) -e $(KERNEL_ENTRY) \ $(KERNEL_OBJS) $(COMMON_C_OBJS) $(TARGET_NM) geekos/kernel.exe > geekos/kernel.syms # Clean build directories of generated files clean : for d in geekos common libc user tools; do \ (cd $$d && rm -f *); \ done # Build header file dependencies, so source files are recompiled when # header files they depend on are modified. depend : $(GENERATED_LIBC_SRCS) $(TARGET_CC) -M $(CC_GENERAL_OPTS) $(CC_KERNEL_OPTS) \ $(KERNEL_C_SRCS:%.c=$(PROJECT_ROOT)/src/geekos/%.c) \ | $(PERL) -n -e 's,^(\S),geekos/$$1,;print' \ > depend.mak $(TARGET_CC) -M $(CC_GENERAL_OPTS) $(CC_USER_OPTS) \ $(COMMON_C_SRCS:%.c=$(PROJECT_ROOT)/src/common/%.c) \ | $(PERL) -n -e 's,^(\S),common/$$1,;print' \ >> depend.mak # By default, there are no header file dependencies. depend.mak : touch $@ include depend.mak 在哪里修改
05-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值