awk字符串函数(printf格式化输出) -- shell

awk有许多强大的字符串函数

gsub(r,s)                              在整个$0中,用s代替r

gsub(r,s,t)                           在整个t中,用s代替r #替换字符串

index(s,t)                             返回s中字符串t的第一位置 #未用过

length(s)                               返回s长度 #c语言strlen

match(s,r)                             测试s是否包含匹配r的字符串 #c语言strcmp

split(s,a,fs)                         使用分隔符fs将字符串s划分为指定序列a

sprint(fmt,exp)                       输出经过fmt格式化后的exp

substr(s,p)                            返回字符串s中从p开始的后缀部分

substr(s,p,n)                         返回字符串s中从p开始的长度为n的后缀部分 #用截取第一个字符串和获取某一段字符串

 

printf

-                                      左对齐

Width                                  域的步长,用0表示0步长

.prec                                  最大字符串长度,或小数点右面的位数

%c                                     ASCII字符

%d                                     整型

%e                                     科学计数法

%f                                     浮点型 #使用小数点后2位%.2f (用于除法后)

%g                                     awk决定哪种浮点数转换e或者f

%o                                     八进制

%s                                     字符串

%x                                     十六进制


例一:

[root@localhost shell]# awk -F ":" 'gsub(/502/,213213213) {print$0}' passwd

test:x:213213213:213213213::/home/test:/bin/bash

[root@localhost shell]#

 

例二:

[root@localhost shell]# awk -F ":" 'gsub(/502/,213213213,$3) {print$0}' passwd

test x 213213213 502  /home/test /bin/bash

[root@localhost shell]#

 

例三:

[root@localhost shell]# awk 'BEGIN {print index("test","t")}' passwd

1

[root@localhost shell]#

 

例四:

[root@localhost shell]# awk -F ":" '{if($1=="test"){print$0,length($1)}}' passwd

test:x:502:502::/home/test:/bin/bash 4

[root@localhost shell]#

  

例五:

[root@localhost shell]# awk -F ":" '{if($1=="test") {print substr($1,1,3)}}' passwd

tes

[root@localhost shell]#

 

例六:

[root@localhost shell]# awk 'BEGIN {STR="a few good men"} END{print substr(STR,7)}' passwd

good men

[root@localhost shell]#

 

例七:

[root@localhost shell]# STR=test.txt

[root@localhost shell]# echo $STR

test.txt

[root@localhost shell]# echo $STR | awk '{print substr($0,1,4)}'

test

[root@localhost shell]# echo $STR | awk '{print substr($0,6)}'

txt

[root@localhost shell]#

 

 

例一:

 

[root@localhost shell]# awk -F ":" '{print $1,$3,$7}' passwd

rpcuser 29 /sbin/nologin

nfsnobody 65534 /sbin/nologin

sshd 74 /sbin/nologin

haldaemon 68 /sbin/nologin

avahi-autoipd 100 /sbin/nologin

xfs 43 /sbin/nologin

gdm 42 /sbin/nologin

mysql 500 /sbin/nologin

mysql1 501 /sbin/nologin

test 502 /bin/bash

test1 503 /bin/bash

 
[root@localhost shell]# awk -F ":" '{printf " %-15s %-15s %s\n",$1,$3,$7}' passwd
 rpcuser         29              /sbin/nologin
 nfsnobody       65534           /sbin/nologin
 sshd            74              /sbin/nologin
 haldaemon       68              /sbin/nologin
 avahi-autoipd   100             /sbin/nologin
 xfs             43              /sbin/nologin
 gdm             42              /sbin/nologin
 mysql           500             /sbin/nologin
 mysql1          501             /sbin/nologin
 test            502             /bin/bash
 test1           503             /bin/bash
[root@localhost shell]#
 

使用awk进行字符串拼接可以通过以下方式实现: 1. 使用printf函数进行字符串拼接。可以使用printf函数的格式化字符串来指定要拼接的内容和格式。例如,假设我们有一个文件file.txt,其包含name和age两列数据,我们可以使用以下命令将它们拼接在一起: ```shell awk '{printf "%s %s\n", $1, $2}' file.txt ``` 这将输出name和age两列数据以空格分隔的形式。 2. 使用变量进行字符串拼接。在awk,可以使用变量来保存和拼接字符串。例如,假设我们有一个文件file.txt,其包含name和age两列数据,我们可以使用以下命令将它们拼接在一起: ```shell awk '{name = name $1 " "; age = age $2 " "} END {print name; print age}' file.txt ``` 这将分别将name和age列的数据拼接在一起,并在最后打印出来。 请注意,以上示例的命令是基于给定的引用内容\[1\]和问题进行的。具体的实现方式可能会根据实际情况有所不同。 #### 引用[.reference_title] - *1* [shell awk命令字符串拼接详解](https://blog.csdn.net/weixin_31959079/article/details/114469578)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [awk行列转换(字符串拼接)](https://blog.csdn.net/weixin_56752399/article/details/116102286)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值