[Linux Command Explanation Encyclopedia] 052. Detailed explanation of Awk string functions, general functions and time functions

built-in function

Awk's built-in functions are mainly divided into the following three types: arithmetic functions, string functions, other general functions, and time functions.

arithmetic function

Format describe
atan2(y, x) Returns the arctangent of y/x.
cos(x) Returns the cosine of x; x is in radians.
sin(x) Returns the sine of x; x is radians.
exp(x) Returns the power of x function.
log(x) Returns the natural logarithm of x.
sqrt(x) Returns the square root of x.
int(x) Returns the value of x truncated to an integer.
rand() Return any number n where 0<=n<1.
srand([expr]) Sets the rand function's seed value to the value of the Expr argument, or a time of day if the Expr argument is omitted. Returns the previous seed value.

for example:

awk 'BEGIN{
  OFMT="%.3f";
  fs=sin(1);
  fe=exp(10);
  fl=log(10);
  fi=int(3.1415);
  print fs,fe,fl,fi;
}'

output:

0.841 22026.466 2.303 3

OFMT sets the output data format to keep 3 decimal places.

Get a random number:

awk 'BEGIN{srand();fr=int(100*rand());print fr;}'

output:

78
awk 'BEGIN{srand();fr=int(100*rand());print fr;}'

output:

31
awk 'BEGIN{srand();fr=int(100*rand());print fr;}'

output:

41 

string functions

Format describe
gsub(Ere, Repl, [In]) It performs exactly like the sub function except that all occurrences of the regular expression are substituted.
sub(Ere, Repl, [In]) Replaces the first occurrence of the extended regular expression specified by the Ere parameter in the string specified by the In parameter with the string specified by the Repl parameter. The sub function returns the number of substitutions. Occurrences of & (ampersand symbols) in the string specified by the Repl parameter are replaced by the string specified by the In parameter that matches the extended regular expression specified by the Ere parameter. If the In parameter is not specified, the default is the entire record ($0 record variable).
index(String1, String2) Returns the position, numbered starting from 1, within the string specified by the String1 parameter in which the parameter specified by String2 occurs. Returns 0 (zero) if the String2 parameter does not appear in the String1 parameter.
length [(String)] Returns the length, in characters, of the string specified by the String parameter. If no String parameter is given, the length of the entire record is returned ($0 record variable).
blength [(String)] Returns the length, in bytes, of the string specified by the String parameter. If no String parameter is given, the length of the entire record is returned ($0 record
substr(String, Start [, Length]) Extracts a substring of the string specified by the String parameter that begins at the position specified by the Start parameter and is the number of characters specified by the Length parameter. If the Length parameter is not given, returns the substring from the Start position to the end of the string.
match(String, Ere) Executes the matching operation of the extended regular expression specified by the Ere parameter on the string specified by the String parameter. Returns a non-zero integer value if successful, otherwise returns 0. The values ​​of the variables RSTART and RLENGTH specify the starting position and length of the matched substring.
split(String, Array [, Sep]) Divide the input field specified by the String parameter into multiple elements and store them in the Array array, each array element corresponds to a field. Fields are separated by the delimiter specified by the Sep parameter. If the Sep parameter is not specified, a space is used as the separator. Returns the number of splits.
tolower(String) Converts the string specified by the String parameter to lowercase and returns the result.
toupper(String) Converts the string specified by the String parameter to uppercase and returns the result.
sprintf(Format [,Item] ...) Generates a string according to the format specified by the Format parameter. The Item parameter is one or more expression values ​​to include in the string. Returns the resulting string.

for example:

awk 'BEGIN{
  surname="Jackson";
  name="Michael";
  full_name=surname " " name;
  print full_name;
}'

output:

Jackson Michael
awk 'BEGIN{
  string="Hello, World!";
  print length(string);
}'

output:

13
awk 'BEGIN{
  string="Hello, World!";
  print index(string, "World");
}'

output:

8
awk 'BEGIN{
  string="Hello, World!";
  substring=substr(string, 7, 5);
  print substring;
}'

output:

World
awk 'BEGIN{
  string="Hello, World!";
  count=split(string, array, ",");
  for (i=1; i<=count; i++) {
    print array[i];
  }
}'

output:

Hello
 World!
awk 'BEGIN{
  string="Hello, World!";
  print tolower(string);
}'

output:

hello, world!
awk 'BEGIN{
  string="Hello, World!";
  print toupper(string);
}'

output:

HELLO, WORLD!
awk 'BEGIN{
  number=42.12345;
  formatted=sprintf("%.2f", number);
  print formatted;
}'

output:

42.12

other general functions

Format describe
length [(String)] Returns the length, in characters, of the string specified by the String parameter. If no String parameter is given, the length of the entire record is returned ($0 record variable).
blength [(String)] Returns the length, in bytes, of the string specified by the String parameter. If no String parameter is given, the length of the entire record is returned ($0 record variable).
system(Command) Starts a new shell on the command specified by the Command parameter. The Command parameter can be any Unix command you want to execute, including commands using pipes, input/output redirection, and so on. The function returns the exit status code returned by the shell.
rand() Returns a pseudorandom number between 0 (inclusive) and 1 (exclusive).
srand([Expr]) If the argument Expr is given, changes the seed of the random number generator to the value of Expr. If no argument is specified, time is used as the seed, which will generate a different sequence of random numbers.

for example:

awk 'BEGIN{
  string="Hello, World!";
  print length(string);
}'

output:

13
awk 'BEGIN{
  string="Hello, World!";
  print blength(string);
}'

output:

14
awk 'BEGIN{
  system("date");
}'

The output is similar to:

Fri Sep  4 01:23:43 UTC 2023
awk 'BEGIN{
  srand();
  random=rand();
  print random;
}'

output:

0.621065
awk 'BEGIN{
  srand(12345);
  random=rand();
  print random;
}'

output:

0.605718

time function

Format describe
systime() Returns the number of seconds from January 1, 1970 00:00:00 GMT to the current time.
mktime(date [, utc-flag]) Creates a timestamp based on the date and time specified by the date parameter. The format of the date parameter is "YYYY MM DD HH MM SS [DST]". If utc-flag is non-zero (ie true), the timestamp is based on UTC time, otherwise it is based on the local time zone.
sprintf(Format [,Timestamp]) 根据 Format 参数指定的格式生成一个时间字符串。Timestamp 参数是由 mktime 函数生成的时间戳。如果未给出 Timestamp 参数,则使用当前时间。
strftime(Format [,Timestamp]) 根据 Format 参数指定的格式生成一个时间字符串。Timestamp 参数是由 mktime 函数生成的时间戳。如果未给出 Timestamp 参数,则使用当前时间。

举例说明:

awk 'BEGIN{
  timestamp=systime();
  print "Current timestamp:", timestamp;
}'

输出:

Current timestamp: 1700387423
awk 'BEGIN{
  date="2023 09 04 12 30 00";
  timestamp=mktime(date);
  print "Timestamp:", timestamp;
}'

输出:

Timestamp: 1700333400
awk 'BEGIN{
  timestamp=systime();
  formatted=sprintf("%Y-%m-%d %H:%M:%S", timestamp);
  print "Formatted time:", formatted;
}'

输出类似于:

Formatted time: 2023-09-04 01:23:43
awk 'BEGIN{
  timestamp=systime();
  formatted=strftime("%A, %B %d, %Y - %I:%M %p", timestamp);
  print "Formatted time:", formatted;
}'

输出类似于:

Formatted time: Sunday, September 04, 2023 - 01:23 AM

从零学 python

【从零学习python 】92.使用Python的requests库发送HTTP请求和处理响应
【从零学习python 】91. 使用装饰器和字典管理请求路径的简洁Web应用
【从零学习python 】93.使用字典管理请求路径
【从零学习python 】89. 使用WSGI搭建简单高效的Web服务器
【从零学习python 】88. WSGI接口详解:实现简单高效的Web开发
【从零学习python 】87. 手动搭建HTTP服务器的Python实现及多线程并发处理
【从零学习python 】86. 深入了解HTTP协议及其在浏览器和服务器通信中的作用
【从零学习python 】85.Python进程池的并行计算技术应用
【从零学习python 】84.深入理解线程和进程
【从零学习python 】83. Python多进程编程与进程池的使用
【从零学习python 】82. 基于多线程的聊天程序实现
【从零学习python 】81.Python多线程通信与队列的应用
【从零学习python 】80.线程访问全局变量与线程安全问题
【从零学习python 】79. 线程访问全局变量与线程安全问题
【从零学习python 】78. 文件下载案例
【从零学习python 】77. TCP服务端编程及注意事项
【从零学习python 】76.服务器与客户端:网络通信的关键组成部分
【从零学习python 】75. TCP协议:可靠的面向连接的传输层通信协议
【从零学习python 】74. UDP网络程序:端口问题与绑定信息详解
【从零学习python 】73. UDP网络程序-发送数据
【从零学习python 】72. 深入理解Socket通信及创建套接字的方法
【从零学习python 】71. 网络端口及其作用
【从零学习python 】70.网络通信方式及其应用:从直接通信到路由器连接多个网络
【从零学习python 】69. 网络通信及IP地址分类解析
【从零学习python 】68. Python正则表达式中的贪婪和非贪婪模式
【从零学习python 】67.Python中的re模块:正则替换与高级匹配技术
【从零学习python 】66.深入了解正则表达式:模式匹配与文本处理的利器
【从零学习python 】65. Python正则表达式修饰符及其应用详解
【从零学习python 】64. Python正则表达式中re.compile方法的使用详解
【从零学习python 】63.正则表达式中的re.Match类及其属性和方法介绍
【从零学习python 】62. Python正则表达式:强大的字符串匹配工具
【从零学习python 】61.Python中的property属性详解和应用示例
【从零学习python 】60.探索生成器:迭代的灵活利器
【从零学习python 】59.迭代器:优化数据遍历的高效工具
【从零学习python 】58.Python中的自定义异常及引发异常的方法
【从零学习python 】57.Python中使用with关键字正确关闭资源
【从零学习python 】56. 异常处理在程序设计中的重要性与应用
【从零学习python 】55.Python中的序列化和反序列化,JSON与pickle模块的应用
【从零学习python 】54. 内存中写入数据
【从零学习python 】53. CSV文件和Python的CSV模块
【从零学习python 】52.文件的读写 - Python文件操作指南
【从零学习python 】51.文件的打开与关闭及其在Python中的应用
【从零学习python 】49. Python中对象相关的内置函数及其用法
【从零学习python 】48.Python中的继承与多继承详解
【从零学习python 】47. 面向对象编程中的继承概念及基本使用
【从零学习python 】46. Python中的__new__和__init__方法解析及单例设计模式
【从零学习python 】45.Python中的类方法和静态方法
【从零学习python 】44.面向对象编程中的私有属性和方法
【从零学习python 】43. Python面向对象编程中的实例属性和类属性
【从零学习python 】42.Python中的内置属性和方法
【从零学习python 】41.python魔法方法(二)
【从零学习python 】40.python魔法方法(一)
【从零学习python 】39.面向对象基本语法及应用示例
【从零学习python 】38.Python包的使用及导入方式
【从零学习python 】37.Python自定义模块的使用和注意事项
【从零学习python 】36.Python中使用pip进行第三方包管理的方法与技巧
【从零学习python 】35. Python常见系统模块及其用法
【从零学习python 】34.Python模块的导入和使用方法详解
【从零学习python 】33.装饰器的作用(二)
【从零学习python 】32.装饰器的作用(一)
【从零学习python 】31.深入理解Python中的高阶函数和闭包
【从零学习python 】30.深入理解递归函数和匿名函数
【从零学习python 】29. 「函数参数详解」——了解Python函数参数的不同用法
【从零学习python 】28. Python中的局部变量和全局变量
【从零学习python 】27. Python 函数的使用及嵌套调用
【从零学习python 】25.函数:提高代码编写效率的利器
【从零学习python 】24. Python中的字符串操作与遍历方法
【从零学习python 】23. Python中集合(set)的使用方法和常见操作
【从零学习python 】22. Python中的字典的增删改查及字典的变量
【从零学习python 】21.Python中的元组与字典
【从零学习python 】20. Python列表操作技巧及实例
【从零学习python 】19. 循环遍历列表和列表嵌套的应用
【从零学习python 】18. Python列表的基本操作详解(一)
【从零学习python 】17. Python字符串的format方法(二)
【从零学习python 】16. Python字符串的format方法(一)
【从零学习python 】15.深入了解字符串及字符集编码
【从零学习python 】14.Python字符串常见操作(二)
【从零学习python 】13.Python字符串常见操作(一)
【从零学习python 】12.Python字符串操作与应用
【从零学习python 】11.Python循环语句和控制流程
【从零学习python 】10.Python条件语句和if嵌套详解
【从零学习python 】09.Python 中的条件判断语句
【从零学习python 】08.Python了解位运算符, 运算符优先级
【从零学习python 】07.Python运算符详解:赋值、比较和逻辑运算符
【从零学习python 】06. Python中运用算数运算符进行计算和字符串拼接
【从零学习python 】05. Python中的输出和输入
【从零学习python 】04. Python编程基础:变量、数据类型与标识符
【从零学习python 】03. Python交互式编程及注释详解
【从零学习python 】02. 开发工具介绍
【从零学习python 】01. 安装配置python

Guess you like

Origin blog.csdn.net/qq_33681891/article/details/132662072