sedとawkの実行方法
22485 ワード
sed
テストケース:
catのある行末から追加開始[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
snakecat
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
snakecat
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
","User","UID","Home"}
{printf "%-30s %-10s %-30s
",$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
","User","UID","Home"
}
{
printf "%-30s %-10s %-30s
",$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
転載先:https://www.cnblogs.com/ray-mmss/p/10131691.html
[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
snakecat
bird
dog
bird
tiger
snake
bird
cat
lion
ji
sdf
[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
snakecat
bird
dog
bird
tiger
snake
bird
cat
lion
ji
sdf
テストケース:
テキストを行順に並べ替える
[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
","User","UID","Home"}
{printf "%-30s %-10s %-30s
",$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
","User","UID","Home"
}
{
printf "%-30s %-10s %-30s
",$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
転載先:https://www.cnblogs.com/ray-mmss/p/10131691.html