`
gelongmei
  • 浏览: 198615 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

[awk]Awk常用字符串处理函数

 
阅读更多

gsub(regexp, replacement [, target])
Search target for all of the longest, leftmost, nonoverlapping matching substrings it can find and replace them with replacement. The ‘g’ in gsub() stands for “global,” which means replace everywhere. For example:
          { gsub(/Britain/, "United Kingdom"); print }
replaces all occurrences of the string ‘Britain’ with ‘United Kingdom’ for all input records.
The gsub() function returns the number of substitutions made. If the variable to search and alter (target) is omitted, then the entire input record ($0) is used. As in sub(), the characters ‘&’ and ‘\’ are special, and the third argument must be assignable.

index(in, find)
Search the string in for the first occurrence of the string find, and return the position in characters where that occurrence begins in the string in. Consider the following example:
          $ awk 'BEGIN { print index("peanut", "an") }'
          -| 3
If find is not found, index() returns zero. (Remember that string indices in awk start at one.)

length([string])
Return the number of characters in string. If string is a number, the length of the digit string representing that number is returned. For example, length("abcde") is five. By contrast, length(15 * 35) works out to three. In this example, 15 * 35 = 525, and 525 is then converted to the string "525", which has three characters.
If no argument is supplied, length() returns the length of $0.
NOTE: In older versions of awk, the length() function could be called without any parentheses. Doing so is considered poor practice, although the 2008 POSIX standard explicitly allows it, to support historical practice. For programs to be maximally portable, always supply the parentheses.
If length() is called with a variable that has not been used, gawk forces the variable to be a scalar. Other implementations of awk leave the variable without a type. (d.c.) Consider:

          $ gawk 'BEGIN { print length(x) ; x[1] = 1 }'
          -| 0
          error--> gawk: fatal: attempt to use scalar `x' as array
         
          $ nawk 'BEGIN { print length(x) ; x[1] = 1 }'
          -| 0
If --lint has been specified on the command line, gawk issues a warning about this.

With gawk and several other awk implementations, when given an array argument, the length() function returns the number of elements in the array. (c.e.) This is less useful than it might seem at first, as the array is not guaranteed to be indexed from one to the number of elements in it. If --lint is provided on the command line (see Options), gawk warns that passing an array argument is not portable. If --posix is supplied, using an array argument is a fatal error.

match(string, regexp)
Search string for the longest, leftmost substring matched by the regular expression, regexp and return the character position, or index, at which that substring begins (one, if it starts at the beginning of string). If no match is found, return zero.
The regexp argument may be either a regexp constant (/.../) or a string constant ("..."). In the latter case, the string is treated as a regexp to be matched.
The order of the first two arguments is backwards from most other string functions that work with regular expressions, such as sub() and gsub(). It might help to remember that for match(), the order is the same as for the ‘~’ operator: ‘string ~ regexp’.
The match() function sets the built-in variable RSTART to the index. It also sets the built-in variable RLENGTH to the length in characters of the matched substring. If no match is found, RSTART is set to zero, and RLENGTH to 0.
For example:
         {
                 if ($1 == "FIND")
                   regex = $2
                 else {
                   where = match($0, regex)
                   if (where != 0)
                     print "Match of", regex, "found at",
                               where, "in", $0
                 }
          }       
This program looks for lines that match the regular expression stored in the variable regex. This regular expression can be changed. If the first word on a line is ‘FIND’, regex is changed to be the second word on that line. Therefore, if given:
          FIND ru+n
          My program runs
          but not very quickly
          FIND Melvin
          JF+KM
          This line is property of Reality Engineering Co.
          Melvin was here.    
awk prints:
          Match of ru+n found at 12 in My program runs
          Match of Melvin found at 1 in Melvin was here.
If array is present, it is cleared, and then the zeroth element of array is set to the entire portion of string matched by regexp. If regexp contains parentheses, the integer-indexed elements of array are set to contain the portion of string matching the corresponding parenthesized subexpression. For example:

          $ echo foooobazbarrrrr |
          > gawk '{ match($0, /(fo+).+(bar*)/, arr)
          >         print arr[1], arr[2] }'
          -| foooo barrrrr
In addition, multidimensional subscripts are available providing the start index and length of each matched subexpression:
          $ echo foooobazbarrrrr |
          > gawk '{ match($0, /(fo+).+(bar*)/, arr)
          >           print arr[1], arr[2]
          >           print arr[1, "start"], arr[1, "length"]
          >           print arr[2, "start"], arr[2, "length"]
          > }'
          -| foooo barrrrr
          -| 1 5
          -| 9 7
There may not be subscripts for the start and index for every parenthesized subexpression, since they may not all have matched text; thus they should be tested for with the in operator (see Reference to Elements).


split(string, array [, fieldsep [, seps ] ])
Divide string into pieces separated by fieldsep and store the pieces in array and the separator strings in the seps array. The first piece is stored in array[1], the second piece in array[2], and so forth. The string value of the third argument, fieldsep, is a regexp describing where to split string (much as FS can be a regexp describing where to split input records; see Regexp Field Splitting). If fieldsep is omitted, the value of FS is used. split() returns the number of elements created. seps is a gawk extension with seps[i] being the separator string between array[i] and array[i+1]. If fieldsep is a single space then any leading whitespace goes into seps[0] and any trailing whitespace goes into seps[n] where n is the return value of split() (that is, the number of elements in array).
The split() function splits strings into pieces in a manner similar to the way input lines are split into fields. For example:

          split("cul-de-sac", a, "-", seps)
splits the string ‘cul-de-sac’ into three fields using ‘-’ as the separator. It sets the contents of the array a as follows:
          a[1] = "cul"
          a[2] = "de"
          a[3] = "sac"
and sets the contents of the array seps as follows:

          seps[1] = "-"
          seps[2] = "-"
The value returned by this call to split() is three.

As with input field-splitting, when the value of fieldsep is " ", leading and trailing whitespace is ignored in values assigned to the elements of array but not in seps, and the elements are separated by runs of whitespace. Also as with input field-splitting, if fieldsep is the null string, each individual character in the string is split into its own array element. (c.e.)

Note, however, that RS has no effect on the way split() works. Even though ‘RS = ""’ causes newline to also be an input field separator, this does not affect how split() splits strings.

Modern implementations of awk, including gawk, allow the third argument to be a regexp constant (/abc/) as well as a string. (d.c.) The POSIX standard allows this as well. See Computed Regexps, for a discussion of the difference between using a string constant or a regexp constant, and the implications for writing your program correctly.

Before splitting the string, split() deletes any previously existing elements in the arrays array and seps.

If string is null, the array has no elements. (So this is a portable way to delete an entire array with one statement. See Delete.)

If string does not match fieldsep at all (but is not null), array has one element only. The value of that element is the original string.

sprintf(format, expression1, ...)
Return (without printing) the string that printf would have printed out with the same arguments (see Printf). For example:
          pival = sprintf("pi = %.2f (approx.)", 22/7)
assigns the string ‘pi = 3.14 (approx.)’ to the variable pival.


sub(regexp, replacement [, target])
Search target, which is treated as a string, for the leftmost, longest substring matched by the regular expression regexp. Modify the entire string by replacing the matched text with replacement. The modified string becomes the new value of target. Return the number of substitutions made (zero or one).
The regexp argument may be either a regexp constant (/.../) or a string constant ("..."). In the latter case, the string is treated as a regexp to be matched. See Computed Regexps, for a discussion of the difference between the two forms, and the implications for writing your program correctly.
This function is peculiar because target is not simply used to compute a value, and not just any expression will do—it must be a variable, field, or array element so that sub() can store a modified value there. If this argument is omitted, then the default is to use and alter $0.42 For example:
          str = "water, water, everywhere"
          sub(/at/, "ith", str)
sets str to ‘wither, water, everywhere’, by replacing the leftmost longest occurrence of ‘at’ with ‘ith’.

If the special character ‘&’ appears in replacement, it stands for the precise substring that was matched by regexp. (If the regexp can match more than one string, then this precise substring may vary.) For example:

          { sub(/candidate/, "& and his wife"); print }
changes the first occurrence of ‘candidate’ to ‘candidate and his wife’ on each input line. Here is another example:

          $ awk 'BEGIN {
          >         str = "daabaaa"
          >         sub(/a+/, "C&C", str)
          >         print str
          > }'
          -| dCaaCbaaa
This shows how ‘&’ can represent a nonconstant string and also illustrates the “leftmost, longest” rule in regexp matching (see Leftmost Longest).

The effect of this special character (‘&’) can be turned off by putting a backslash before it in the string. As usual, to insert one backslash in the string, you must write two backslashes. Therefore, write ‘\\&’ in a string constant to include a literal ‘&’ in the replacement. For example, the following shows how to replace the first ‘|’ on each line with an ‘&’:

          { sub(/\|/, "\\&"); print }
As mentioned, the third argument to sub() must be a variable, field or array element. Some versions of awk allow the third argument to be an expression that is not an lvalue. In such a case, sub() still searches for the pattern and returns zero or one, but the result of the substitution (if any) is thrown away because there is no place to put it. Such versions of awk accept expressions like the following:

          sub(/USA/, "United States", "the USA and Canada")
For historical compatibility, gawk accepts such erroneous code. However, using any other nonchangeable object as the third parameter causes a fatal error and your program will not run.

Finally, if the regexp is not a regexp constant, it is converted into a string, and then the value of that string is treated as the regexp to match.

substr(string, start [, length])
Return a length-character-long substring of string, starting at character number start. The first character of a string is character number one.43 For example, substr("washington", 5, 3) returns "ing".
If length is not present, substr() returns the whole suffix of string that begins at character number start. For example, substr("washington", 5) returns "ington". The whole suffix is also returned if length is greater than the number of characters remaining in the string, counting from character start.

If start is less than one, substr() treats it as if it was one. (POSIX doesn't specify what to do in this case: Brian Kernighan's awk acts this way, and therefore gawk does too.) If start is greater than the number of characters in the string, substr() returns the null string. Similarly, if length is present but less than or equal to zero, the null string is returned.

The string returned by substr() cannot be assigned. Thus, it is a mistake to attempt to change a portion of a string, as shown in the following example:

          string = "abcdef"
          # try to get "abCDEf", won't work
          substr(string, 3, 3) = "CDE"
It is also a mistake to use substr() as the third argument of sub() or gsub():

          gsub(/xyz/, "pdq", substr($0, 5, 20))  # WRONG
(Some commercial versions of awk treat substr() as assignable, but doing so is not portable.)

If you need to replace bits and pieces of a string, combine substr() with string concatenation, in the following manner:

          string = "abcdef"
          ...
          string = substr(string, 1, 2) "CDE" substr(string, 6)

tolower(string)
Return a copy of string, with each uppercase character in the string replaced with its corresponding lowercase character. Nonalphabetic characters are left unchanged. For example, tolower("MiXeD cAsE 123") returns "mixed case 123".
toupper(string)
Return a copy of string, with each lowercase character in the string replaced with its corresponding uppercase character. Nonalphabetic characters are left unchanged. For example, toupper("MiXeD cAsE 123") returns "MIXED CASE 123".
分享到:
评论

相关推荐

    awk笔记 算数函数、字符串函数

    算数函数、字符串函数、其它一般函数、时间函数

    Python实现像awk一样分割字符串

    若你使用过 Shell 中的 awk 工具,会发现用它来分割字符串是非常方便的。特别是多个连续空格会被当做一个处理。 [root@localhost ~]# cat demo.txt hello world [root@localhost ~]# [root@localhost ~]# awk ...

    三剑客之【awk】.html

    awk命令常用用法整理;加入了自己在平时运用中的...awk有许多强大的字符串函数 gsub(r,s) #在整个$0中,用s代替r gsub(r,s,t) 在整个t中,用s代替r index(s,t) 返回s中字符串t的第一位置 length(s) 返回s长度 。。。

    shell中的awk命令

    内置字符串函数gsub函数indexlength 1.awk–“样式扫描和处理语言” awk是一种很棒的语言,它适合文本处理和报表生成,其语法较为常见,借鉴了某些语言的一些精华。在linux系统日常处理工作中,发挥很重要的工作。 ...

    php实现字符串反转输出的方法

    本文实例讲述了php实现字符串反转输出的方法。... 您可能感兴趣的文章:php实现字符串翻转的方法利用perl、python、php、shell、sed、awk、c 实现字符串的翻转php中实现字符串翻转的方法PHP 实现字符串翻转(包含

    linux文本处理三剑客之 awk 命令(二)

    文章目录表达式统计input文件中的空白行平均值的计算系统变量格式化输出awk的ascii字符的转换awk的字符型字符的转换printf 修饰符的栗子内置字符串函数举个例子 表达式 统计input文件中的空白行 平均值的计算 ...

    GNU AWK (GAWK) 是历史悠久的 AWK 编程语言的开放源代码实现

    AWK 是该编程语言本身的名称,它编写于 1977 年。其名称是三个主要作者的姓的首字母缩写:Drs....事实上,其中许多特性,包括控制语句和字符串函数,如 printf 和 sprintf,基本上是相同的。然而,也存在着一些差异。

    linux目录下的awk讲解

    关于linux下的awk 如调用awk的方式,与正则表达式的结合,内置变量 操作符,内置的字符串函数,格式化函数printf等

    shell三剑客过滤文件内字符串长度输出

    三剑客过滤长字符串 记一次过滤文件内容,三剑客awk、grep、...且看,统计字符串长度,用到招式 length() 函数 [root@centos]# echo hello | awk '{print length($1)}' 5 如看官所愿,得到字符串的长度 5 连招,加 if

    AWK_样式扫描和处理语言

    1.5 AWK的内置函数......................................................................................................................7 1.6 在命令行使用AWK..............................................

    Shell脚本实现查找字符串中某字符最后出现的位置

    需要对字符串查找其中某个字符最后出现的位置,这个在PHP (strrpos)或者Perl (rindex)里面都有现成函数可用的功能,在Shell里面居然一时想不出个道道来。在论坛上发贴也没人解答(不知道是问题太简单还是真的很高深...

    UNIX Handbook

    29.awk打印传入字符串的第n个分隔域 37 30.在awk中使用shell中的变量 37 31.在某个目录下查找包含特定字符串的文件 返回文件名称 39 五.其它命令 39 1.eval命令:可用于动态生成和执行代码 39 2.exit n:退出...

    一天一个shell命令 linux文本内容操作系列-awk命令详解

    简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行各种分析处理。 awk有3个不同版本: awk、nawk和gawk,未作特别说明,一般指gawk,gawk 是 AWK 的 GNU 版本。 awk其名称得自于它的...

    Linux中awk的使用方法详解

    在学习awk之前我们应该都学过sed,grep,tr,cut等等命令,这些命令都是为了方便我们对Linux下文本和数据的处理,但是我们会发现很多时候这些命令并不能一下子就完全解决我们的需求,很多时候我们都需要使用管道符结合...

    Linux与unix shell编程指南

    9.2.9 内置的字符串函数 78 9.2.10 字符串屏蔽序列 80 9.2.11 awk输出函数printf 81 9.2.12 printf修饰符 81 9.2.13 awk数组 86 9.3 小结 88 第10章 sed 用法介绍 89 10.1 sed怎样读取数据 89 10.2 调用sed 89 ...

    linux shell 编程教程

    9.2.9 内置的字符串函数 78 9.2.10 字符串屏蔽序列 80 9.2.11 awk输出函数printf 81 9.2.12 printf修饰符 81 9.2.13 awk数组 86 9.3 小结 88 第10章 sed 用法介绍 89 10.1 sed怎样读取数据 89 10.2 调用sed 89 ...

    宋劲彬的嵌入式C语言一站式编程

    2.7. 以字符串为单位的I/O函数 2.8. 以记录为单位的I/O函数 2.9. 格式化I/O函数 2.10. C标准库的I/O缓冲区 2.11. 本节综合练习 3. 数值字符串转换函数 4. 分配内存的函数 26. 链表、二叉树和哈希表 1. 链表 1.1. ...

    shell 编程指南pdf

    9.2.9 内置的字符串函数 78 9.2.10 字符串屏蔽序列 80 9.2.11 awk输出函数printf 81 9.2.12 printf修饰符 81 9.2.13 awk数组 86 9.3 小结 88 第10章 sed 用法介绍 89 10.1 sed怎样读取数据 89 10.2 调用sed 89 ...

    LINUX与UNIX SHELL编程指南(很全)

    9.2.9 内置的字符串函数 78 9.2.10 字符串屏蔽序列 80 9.2.11 awk输出函数printf 81 9.2.12 printf修饰符 81 9.2.13 awk数组 86 9.3 小结 88 第10章 sed 用法介绍 89 10.1 sed怎样读取数据 89 10.2 调用sed 89 ...

    shell从入门到精通

    shell基础知识、shell字符串处理、shell表达式与运算符、shell流程控制、shell函数与数组、shell正则表达式、shell文本处理三剑客(grep、sed、awk)、shell标准输入、输出和错误、shell信号发送与捕捉、shell编程时...

Global site tag (gtag.js) - Google Analytics