Linux 中awk命令自定义函数

 

001、

[root@PC1 test]# echo a | awk 'function my_length(str) {return length(str)}; {text = "Hello"; print "Length of text:", my_length(text)}'
Length of text: 5

 

 。

 

002、

(base) [b20223040323@admin2 test6]$ ls
a.txt
(base) [b20223040323@admin2 test6]$ cat a.txt
01 02 03
04 05 06
07 08 09
10 11 12
(base) [b20223040323@admin2 test6]$ awk 'function squre(x) {return x * x} {OFS = "\t"; print $1, squre($1)}' a.txt     ## 自定义函数
01      1
04      16
07      49
10      100

 。

 

posted @ 2025-02-03 00:49  小鲨鱼2018  阅读(14)  评论(0)    收藏  举报