技术改变生活

博客园 首页 新随笔 联系 订阅 管理

#将passwd文件每行重复打印10遍;while循环,当i>10时,结束循环。

[root@localhost linshi]# awk   '{i=1;while(i<=10){print $0;i++}}'  passwd
root:x:0:0:root:/root:/bin/bash
root:x:0:0:root:/root:/bin/bash
root:x:0:0:root:/root:/bin/bash
root:x:0:0:root:/root:/bin/bash
root:x:0:0:root:/root:/bin/bash
root:x:0:0:root:/root:/bin/bash
root:x:0:0:root:/root:/bin/bash
root:x:0:0:root:/root:/bin/bash
root:x:0:0:root:/root:/bin/bash
root:x:0:0:root:/root:/bin/bash

#注释

[root@localhost linshi]# awk  -F:  '/^root/{i=1;while(i<=NF){print $i;i++}}'  passwd
root
x
0
0
root
/root
/bin/bash

 

#注释

[root@localhost linshi]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.0.170 test
52.87.94.70 registry-1.docker.io
[root@localhost linshi]# awk  -F:  '{i=1;while(i<=NF){print $i;i++}}' /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4


1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.0.170 test
52.87.94.70 registry-1.docker.io

 

#注释

[root@localhost linshi]# awk -F: '{i=1;while(i<=NF){printf "%-20s", $i;i++};print xxoo}' passwd

[root@localhost linshi]# awk -F: '{i=1;while(i<=NF){printf "%-20s", $i;i++};print }' passwd

 

 

awk中的for使用方式

posted on 2020-02-23 20:39  小阿峰  阅读(240)  评论(0编辑  收藏  举报