awk 脚本学习

IFS 示例
$oldIFS="$IFS"				#保存旧值
$IFS=":"
$while read user password uid gid rest_of_line;do
if [ "$user" == "tom" ];then
echo "$user's ID is $uid"
fi
done < /etc/passwd
$IFS="$oldIFS"				#恢复旧值
awk -F: 'length($2)==0 {print $1}' /etc/shadow      #检测密码为空

printf '%-s\t %s\n'  $(cat test.txt)

符号释意
%ns输出占n个符,不够空格 ,加-为左对齐
%ni输出整数 有n个数字
%n.mf输出浮点数,n位,有m位小数
%d or %i整数
%e or %E浮点数
%s字符
%u正整数
[tom@tom-virtual-machine ~]$awk -F: '{printf "%18s  %-8s %5d\t %-20s\n", $1,"uid is",$3,$7}' /etc/passwd
              root  uid is       0       /bin/bash           
            daemon  uid is       1       /usr/sbin/nologin   
               bin  uid is       2       /usr/sbin/nologin   
               sys  uid is       3       /usr/sbin/nologin   
              sync  uid is       4       /bin/sync           

printf "\a"    发出警报声

awk '{printf $2 "\t" }'      加上制表符
awk 里print自带回车,printf则没有
awk  '/bash/ { print }' /etc/passwd  搜索有bash的行
awk '{print toupper($0)}' file      转化成大写
awk -f script.awk			#-f 脚本文件

script.awk脚本文件
在这里插入图片描述

[tom@ton ~]$./script.awk   
Writing my first awk executable script!
[tom@ton ~]$a2p script.awk 					#自动重写成Perl脚本
#!/usr/bin/perl
eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
    if $running_under_some_shell;
                        # this emulates #! processing on NIH machines.
                        # (remove #! line above if indigestible)
eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_0-9]+=)(.*)/ && shift;
                        # process any FOO=bar switches
printf "%s\n", 'Writing my first awk executable script!';
NF列数
$NF列数
NR行数
行分隔符列分隔符
数据截取RSFS
打印ORSOFS
[tom@tom-virtual-machine ~]$cat word.txt 
root:x 0 0 root /root /bin/bash
root:x 0 t6 root /root /bin/bash
tom:x 0 0u root /root /bin/bash
[tom@tom-virtual-machine ~]$awk -F '\\ ' '{print $1 $2}' word.txt         #\space 代表空格,\\space转义空格                     
root:x0
root:x0
tom:x0
[tom@tom-virtual-machine ~]$awk -F [:\ ] '{ if($4 ~ "[0-9]$") print NR $1 $2}' word.txt    #可选间隔符
1rootx
2rootx
[tom@tom-virtual-machine ~]$awk -F [:\ ] '{print NR $1 $2}' word.txt 
1rootx
2rootx
3tomx
[tom@tom-virtual-machine ~]$awk -F [:\ ] '{ if($4 ~ "[0-9]$") print NR $1 $2}' word.txt 
1rootx
2rootx
[tom@tom-virtual-machine ~]$awk -F : '{ if($7 !~ "nologin$") print $1; else print $3} \
BEGIN {print "-----------begin-----------",name = "tom"} \
{if($1 ~ name && $7 ~"/bin/bash") print "hello world"} END{print "------end------"}' /etc/passwd 
-----------begin----------- tom
root
...
tom
hello world
116
ftp
------end------

[tom@tom-virtual-machine ~]$awk 'BEGIN {ORS="**\n"}{print $1}' word.txt         
root:x**
root:x**
tom:x**

[tom@tom-virtual-machine ~]$awk 'BEGIN {RS=":";ORS="**\n"}{print $1}' word.txt  
root**
x**
x**
x**

[tom@tom-virtual-machine ~]$head -1 /etc/passwd
root:x:0:0:root:/root:/bin/bash
[tom@tom-virtual-machine ~]$head -1 /etc/passwd | awk 'BEGIN {FS=":";OFS="*"} {x=1;while(x<NF) {print $x,"tom";x++}}' 
root*tom
x*tom
0*tom
0*tom
root*tom
/root*tom

[tom@tom-virtual-machine ~]$head -1 /etc/passwd | awk 'BEGIN {FS=":";OFS="*"} {x=1;while(x<NF) {print $3,"tom";x++}}'  
0*tom
0*tom
0*tom
0*tom
0*tom
0*tom
[tom@tom-virtual-machine ~]$

打印第3列到最后一列

 new-host-808:/etc/sysconfig/network # head -1 /etc/passwd | \
 awk 'BEGIN {FS=":";ORS=" "} {for(i=3;i<=NF;i++) {print $i}} END{print "\n"}'
499 499 User for Avahi /run/avahi-daemon /bin/false 
awk 创建用户脚本
#!/usr/bin/awk -f
# Script to create a file suitable for use in the 'newusers'command,
#from a file consisting of user IDs and passwords in the form:
#first_name last_name password
#Copyright (c) KMSelf Sat Aug 25 20:47:38 PDT 2001
#Distributed under GNU GPL v 2, or at your option, any later version.
#This program is distributed WITHOUT ANY WARRANTY.
BEGIN{
#Assign starting UID, GID
if (ARGC > 2) {
startuid = ARGV[1]
delete ARGV[1]
}
else{
printf("Usage: newusers startUID file\n"\
"”where:\n" \
" startUID is the starting userid to add, and\n"\
" file is an input file in form: \n"\
" first_name last_name password\n"\
)
exit
}
infile = ARGV[1]
printf("Starting UID: %s\n\n", startuid )
}
/^#/ { next }    #遇到#开头的行,就跳过
{
++record
first = $1
last = $2
passwd = $3
user = substr( tolower( first ),1,1) tolower( last )
uid = startuid + record - 1
gid = uid
printf("%s:%s:%d:%d:%s %s,, /home/%s:/bin/bash\n",\
user,passwd, uid, gid, first,last,user \
)
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值