sed 和awk的执行方式

sed

测试案例:

    在有cat的行末开始追加<---,直到有dog的行结束  

[root@L shells]# cat catDog.txt 
snake
snake
pig
bird
dog
cat 
snake  
pig
bird
snake
cat
bird
dog 
bird
tiger
snake
bird
cat
lion
ji
sdf  

一.命令

[root@L shells]# sed '/cat/,/dog/s/$/<---/' catDog.txt 
snake
snake
pig
bird
dog
cat <---
snake  <---
pig<---
bird<---
snake<---
cat<---
bird<---
dog <---
bird
tiger
snake
bird
cat<---
lion<---
ji<---
sdf<---

 

二.sed脚本

[root@L shells]# cat sed.txt
/cat/,/dog/s/$/<---/
[root@L shells]# sed -f sed.txt catDog.txt 
snake
snake
pig
bird
dog
cat <---
snake  <---
pig<---
bird<---
snake<---
cat<---
bird<---
dog <---
bird
tiger
snake
bird
cat<---
lion<---
ji<---
sdf<---

 

awk

  测试案例 :

    把文本按行倒序排列

[root@L shells]# cat zancun.txt 
11111
22222
33333
44444
55555

一.命令 

[root@L shells]# awk '{line[NR]=$0}END{for(i=NR;i>0;i--){print line[i]}}' zancun.txt
55555
44444
33333
22222
11111

二.awk脚本

[root@L shells]# cat awk.txt 
{line[NR]=$0}END{for(i=NR;i>0;i--){print line[i]}}
[root@L shells]# awk -f awk.txt zancun.txt 
55555
44444
33333
22222
11111
[root@room9pc01 ~]# cat awk.txt
BEGIN{FS=":";printf "%-30s %-10s %-30s \n","User","UID","Home"}
{printf "%-30s %-10s %-30s \n",$1,$3,$5}  # %10s   占10个位置    -左对齐 
END{printf ""Total",NR"lines"}
[root@room9pc01 ~]# awk -f awk.txt /etc/passwd
User                           UID        Home                           
root                           0          root                           
bin                            1          bin                            
daemon                         2          daemon                         
adm                            3          adm                            
lp                             4          lp                             
sync                           5          sync                           
shutdown                       6          shutdown                       
halt                           7          halt                           
mail                           8          mail                           
operator                       11         operator                       
games                          12         games                          
ftp                            14         FTP User                       
nobody                         99         Nobody                         
avahi-autoipd                  170        Avahi IPv4LL Stack             
systemd-bus-proxy              999        systemd Bus Proxy              
systemd-network                998        systemd Network Management     
dbus                           81         System message bus             
polkitd                        997        User for polkitd               
abrt                           173                                       
tss                            59         Account used by the trousers package to sandbox the tcsd daemon 
unbound                        996        Unbound DNS resolver           
usbmuxd                        113        usbmuxd user                   
colord                         995        User for colord                
saslauth                       994        Saslauthd user                 
libstoragemgmt                 993        daemon account for libstoragemgmt 
geoclue                        992        User for geoclue               
rpc                            32         Rpcbind Daemon                 
setroubleshoot                 991                                       
rtkit                          172        RealtimeKit                    
qemu                           107        qemu user                      
rpcuser                        29         RPC Service User               
nfsnobody                      65534      Anonymous NFS User             
radvd                          75         radvd user                     
chrony                         990                                       
pulse                          171        PulseAudio System Daemon       
gdm                            42                                        
gnome-initial-setup            989                                       
avahi                          70         Avahi mDNS/DNS-SD Stack        
postfix                        89                                        
sshd                           74         Privilege-separated SSH        
ntp                            38                                        
tcpdump                        72                                        
Student                        1000                                      
apache                         48         Apache                         
mysql                          27         MariaDB Server                 
Total 45lines

 

  

三.shell脚本 

[root@L shells]# cat zancun.txt 
11111
22222
33333
44444
55555
[root@L shells]# cat awk.sh
#!/usr/bin/awk -f 
#FileName:  :awk.sh
#Description:
#
#Author     :root
#Date       :2018-12-17 
BEGIN{}
{line[NR]=$0}
END{
    for(i=NR;i>0;i--){
        print line[i]
    }
}

[root@L shells]# ./awk.sh zancun.txt 
55555
44444
33333
22222
11111

 

[root@room9pc01 ~]# cat awk.awk
#!/usr/bin/awk -f
BEGIN{
    FS=":";
    printf "%-30s %-10s %-30s \n","User","UID","Home"
}
{
    printf "%-30s %-10s %-30s \n",$1,$3,$5
}  # %10s   占10个位置    -左对齐 
END{
    print "Total",NR"lines"
}
[root@room9pc01 ~]# ./awk.awk /etc/passwd
User                           UID        Home                           
root                           0          root                           
bin                            1          bin                            
daemon                         2          daemon                         
adm                            3          adm                            
lp                             4          lp                             
sync                           5          sync                           
shutdown                       6          shutdown                       
halt                           7          halt                           
mail                           8          mail                           
operator                       11         operator                       
games                          12         games                          
ftp                            14         FTP User                       
nobody                         99         Nobody                         
avahi-autoipd                  170        Avahi IPv4LL Stack             
systemd-bus-proxy              999        systemd Bus Proxy              
systemd-network                998        systemd Network Management     
dbus                           81         System message bus             
polkitd                        997        User for polkitd               
abrt                           173                                       
tss                            59         Account used by the trousers package to sandbox the tcsd daemon 
unbound                        996        Unbound DNS resolver           
usbmuxd                        113        usbmuxd user                   
colord                         995        User for colord                
saslauth                       994        Saslauthd user                 
libstoragemgmt                 993        daemon account for libstoragemgmt 
geoclue                        992        User for geoclue               
rpc                            32         Rpcbind Daemon                 
setroubleshoot                 991                                       
rtkit                          172        RealtimeKit                    
qemu                           107        qemu user                      
rpcuser                        29         RPC Service User               
nfsnobody                      65534      Anonymous NFS User             
radvd                          75         radvd user                     
chrony                         990                                       
pulse                          171        PulseAudio System Daemon       
gdm                            42                                        
gnome-initial-setup            989                                       
avahi                          70         Avahi mDNS/DNS-SD Stack        
postfix                        89                                        
sshd                           74         Privilege-separated SSH        
ntp                            38                                        
tcpdump                        72                                        
Student                        1000                                      
apache                         48         Apache                         
mysql                          27         MariaDB Server                 
Total 45lines

 

posted @ 2018-12-17 15:16  Ray_chen  阅读(380)  评论(0编辑  收藏  举报