SlideShare a Scribd company logo
1 of 13
S e d & A w k
Какво е sed?
command line stream editor
Какво е awk?
pattern scanning and processing language
Какво са регулярните изрази?
как те могат да помогнат на администратора
Мариян Маринов <mm@yuhu.biz>
S e d & A w k
Азбуката на регулярните изрази
^ - съгласува началото на реда
$ - съглавува края на реда
[group] – съгласува с един символ от група
(group) – групира като отделна променлива
 - escape-ва стандартен RE знак
. - съгласува се с всеки знак
| - или
qantifiers
? - съгласува 0 или 1 пъти
* - съглавува 0 или повече пъти
+ - съгласува 1 или повече пъти
{n} – съгласува точно n пъти
{n,m} – съгласува между n и m пъти
Мариян Маринов <mm@yuhu.biz>
S e d & A w k
Азбуката на регулярните изрази
{n,} - минимум n пъти
{,n} – максимум n пъти
променливи
$0 - целият string
$1 – първата група от ()
$n – n-тата група от ()
опции
g - global (продължава да съгласува след първото
съвпадение)
i - case insensitive
Мариян Маринов <mm@yuhu.biz>
S e d & A w k
За какво може да се използва sed?
Pattern matching в sed
/^$/ - празен ред
s/(.*)(.)/1/ - групиране и подмяна
s/.*/(&)/ - промяна на целия ред
Мариян Маринов <mm@yuhu.biz>
S e d & A w k
Основни команди на sed
15icola - обхват команда текст
обхват на командите:
всички редове
114 - само един ред
3,12 - от трети до 12 ред
/RE/ - регулярен израз
/RE/,/RE/ - от, до регулярен израз
/RE/,$ - от регулярен израз до края
Мариян Маринов <mm@yuhu.biz>
S e d & A w k
Основни команди на sed
изтриване:
/daemon/D - изтрива цял ред
въвеждане:
1itata - въвежда tata над първият
ред
1аtata - въвежда tata след първият
ред
извеждане:
/pts/p - извежда всеки ред наМариян Маринов <mm@yuhu.biz>
S e d & A w k
Основни команди на sed
подмяна:
s/x*/y/i
s/.*/(&)/
s/(.*)(.)(.)/23/ 1-9
/field/s/[d|l]$/G
s/n//g
sed '{s/dsa/ok/};{s/lkl/duh/}' test
sed -e 's/dsa/ok/' -e 's/lkl/duh/' test
Мариян Маринов <mm@yuhu.biz>
S e d & A w k
Подценяването на Awk
в системната админитстрация
ps ax|awk '{print $1}'
обхват на командите:
{} - всички линии
/RE/ {} - всички линии съвпадащи с
регуляреният израз
/RE1/,/RE2/ {} - всички линии попаднали между
първото съвпадение на RE1 и първото съвпадение
на RE2
$2 ~ /RE/ {} - всички линии чието второто поле
съвпада с регулярния израз
Мариян Маринов <mm@yuhu.biz>
S e d & A w k
Заместител на grep
ps aux|grep 'username'|awk '{print $2}'
ps aux|awk '/username/ {print $2}'
Извеждане на информация с дадена
логика
tail -f /var/log/exim_mainlog|awk '/SMTP/ {
if ($5 != “212.5.11.3”)
print “Connection from: “$5
}'
tail -f /var/log/exim_mainlog|awk '$5 !~ /212.5.11.3/ {
print “Connection from: “$5
}'
Мариян Маринов <mm@yuhu.biz>
S e d & A w k
Оператори:
|| && ~ !~
!=, ==, <, <<, <=, =>, >>, >,
+ - * / % ++ --
Вградени променливи:
FILENAME - current filename
FS - field separator
NF - number of fields
NR - current line number
OFMT - output format for numbers
OFS - output field separator
RS - record separator
$0 - entire input record
$n - nth field in the current record
Мариян Маринов <mm@yuhu.biz>
S e d & A w k
Вградени променливи:
ARGC - number of arguments on the command
line
ARGV - an array containing the list of cmd
arguments
CONVFMT - string conversion format for numbers
('%.6g)
ENVIRON - an array containing the list of
environment variable
FNR - current line field number
RT - the text matched by RS
Мариян Маринов <mm@yuhu.biz>
`
S e d & A w k
Обработване на информацията с awk
BEGIN {}
{}
END {}
Прости примери с awk:
/pattern/ { ++x }
END { print “The pattern have been seen”$x”times.”}
{ total += $2}
END { print “Total column count is: “$total}
{ for (i=NF; i>=1;i--) print $i }
Мариян Маринов <mm@yuhu.biz>
`
S e d & A w k
Escape последователности
a
b
f
n
r
t
Мариян Маринов <mm@yuhu.biz>

More Related Content

Viewers also liked

Arellano cilia taller 2
Arellano cilia taller 2Arellano cilia taller 2
Arellano cilia taller 2Cilia Arellano
 
Welcome to khehranala resort
Welcome to khehranala resortWelcome to khehranala resort
Welcome to khehranala resortangelochintu
 
Varnish in action pbc10
Varnish in action pbc10Varnish in action pbc10
Varnish in action pbc10Combell NV
 
The IMPACT of microlearning
The IMPACT of microlearningThe IMPACT of microlearning
The IMPACT of microlearningcfaisonjr
 
Linux containers-namespaces(Dec 2014)
Linux containers-namespaces(Dec 2014)Linux containers-namespaces(Dec 2014)
Linux containers-namespaces(Dec 2014)Ralf Dannert
 
The case for real time collaboration
The case for real time collaborationThe case for real time collaboration
The case for real time collaborationSei Mani
 
Social Media Strategy for Tata Docomo
Social Media Strategy for Tata DocomoSocial Media Strategy for Tata Docomo
Social Media Strategy for Tata DocomoAakriti Agarwal
 

Viewers also liked (13)

Arellano cilia taller 2
Arellano cilia taller 2Arellano cilia taller 2
Arellano cilia taller 2
 
Welcome to khehranala resort
Welcome to khehranala resortWelcome to khehranala resort
Welcome to khehranala resort
 
Varnish in action pbc10
Varnish in action pbc10Varnish in action pbc10
Varnish in action pbc10
 
Graphs in R
Graphs in RGraphs in R
Graphs in R
 
Proxy arp
Proxy arpProxy arp
Proxy arp
 
Leben im Netz
Leben im NetzLeben im Netz
Leben im Netz
 
Hypothesis testing in R
Hypothesis testing in RHypothesis testing in R
Hypothesis testing in R
 
The IMPACT of microlearning
The IMPACT of microlearningThe IMPACT of microlearning
The IMPACT of microlearning
 
Linux containers-namespaces(Dec 2014)
Linux containers-namespaces(Dec 2014)Linux containers-namespaces(Dec 2014)
Linux containers-namespaces(Dec 2014)
 
The case for real time collaboration
The case for real time collaborationThe case for real time collaboration
The case for real time collaboration
 
Ftp connector
Ftp connectorFtp connector
Ftp connector
 
Social Media Strategy for Tata Docomo
Social Media Strategy for Tata DocomoSocial Media Strategy for Tata Docomo
Social Media Strategy for Tata Docomo
 
Galera replication
Galera replicationGalera replication
Galera replication
 

More from Marian Marinov

Dev.bg DevOps March 2024 Monitoring & Logging
Dev.bg DevOps March 2024 Monitoring & LoggingDev.bg DevOps March 2024 Monitoring & Logging
Dev.bg DevOps March 2024 Monitoring & LoggingMarian Marinov
 
Basic presentation of cryptography mechanisms
Basic presentation of cryptography mechanismsBasic presentation of cryptography mechanisms
Basic presentation of cryptography mechanismsMarian Marinov
 
Microservices: Benefits, drawbacks and are they for me?
Microservices: Benefits, drawbacks and are they for me?Microservices: Benefits, drawbacks and are they for me?
Microservices: Benefits, drawbacks and are they for me?Marian Marinov
 
Introduction and replication to DragonflyDB
Introduction and replication to DragonflyDBIntroduction and replication to DragonflyDB
Introduction and replication to DragonflyDBMarian Marinov
 
Message Queuing - Gearman, Mosquitto, Kafka and RabbitMQ
Message Queuing - Gearman, Mosquitto, Kafka and RabbitMQMessage Queuing - Gearman, Mosquitto, Kafka and RabbitMQ
Message Queuing - Gearman, Mosquitto, Kafka and RabbitMQMarian Marinov
 
How to successfully migrate to DevOps .pdf
How to successfully migrate to DevOps .pdfHow to successfully migrate to DevOps .pdf
How to successfully migrate to DevOps .pdfMarian Marinov
 
How to survive in the work from home era
How to survive in the work from home eraHow to survive in the work from home era
How to survive in the work from home eraMarian Marinov
 
Improve your storage with bcachefs
Improve your storage with bcachefsImprove your storage with bcachefs
Improve your storage with bcachefsMarian Marinov
 
Control your service resources with systemd
 Control your service resources with systemd  Control your service resources with systemd
Control your service resources with systemd Marian Marinov
 
Comparison of-foss-distributed-storage
Comparison of-foss-distributed-storageComparison of-foss-distributed-storage
Comparison of-foss-distributed-storageMarian Marinov
 
Защо и как да обогатяваме знанията си?
Защо и как да обогатяваме знанията си?Защо и как да обогатяваме знанията си?
Защо и как да обогатяваме знанията си?Marian Marinov
 
Securing your MySQL server
Securing your MySQL serverSecuring your MySQL server
Securing your MySQL serverMarian Marinov
 
DoS and DDoS mitigations with eBPF, XDP and DPDK
DoS and DDoS mitigations with eBPF, XDP and DPDKDoS and DDoS mitigations with eBPF, XDP and DPDK
DoS and DDoS mitigations with eBPF, XDP and DPDKMarian Marinov
 
Challenges with high density networks
Challenges with high density networksChallenges with high density networks
Challenges with high density networksMarian Marinov
 
SiteGround building automation
SiteGround building automationSiteGround building automation
SiteGround building automationMarian Marinov
 
Preventing cpu side channel attacks with kernel tracking
Preventing cpu side channel attacks with kernel trackingPreventing cpu side channel attacks with kernel tracking
Preventing cpu side channel attacks with kernel trackingMarian Marinov
 
Managing a lot of servers
Managing a lot of serversManaging a lot of servers
Managing a lot of serversMarian Marinov
 
Let's Encrypt failures
Let's Encrypt failuresLet's Encrypt failures
Let's Encrypt failuresMarian Marinov
 

More from Marian Marinov (20)

Dev.bg DevOps March 2024 Monitoring & Logging
Dev.bg DevOps March 2024 Monitoring & LoggingDev.bg DevOps March 2024 Monitoring & Logging
Dev.bg DevOps March 2024 Monitoring & Logging
 
Basic presentation of cryptography mechanisms
Basic presentation of cryptography mechanismsBasic presentation of cryptography mechanisms
Basic presentation of cryptography mechanisms
 
Microservices: Benefits, drawbacks and are they for me?
Microservices: Benefits, drawbacks and are they for me?Microservices: Benefits, drawbacks and are they for me?
Microservices: Benefits, drawbacks and are they for me?
 
Introduction and replication to DragonflyDB
Introduction and replication to DragonflyDBIntroduction and replication to DragonflyDB
Introduction and replication to DragonflyDB
 
Message Queuing - Gearman, Mosquitto, Kafka and RabbitMQ
Message Queuing - Gearman, Mosquitto, Kafka and RabbitMQMessage Queuing - Gearman, Mosquitto, Kafka and RabbitMQ
Message Queuing - Gearman, Mosquitto, Kafka and RabbitMQ
 
How to successfully migrate to DevOps .pdf
How to successfully migrate to DevOps .pdfHow to successfully migrate to DevOps .pdf
How to successfully migrate to DevOps .pdf
 
How to survive in the work from home era
How to survive in the work from home eraHow to survive in the work from home era
How to survive in the work from home era
 
Managing sysadmins
Managing sysadminsManaging sysadmins
Managing sysadmins
 
Improve your storage with bcachefs
Improve your storage with bcachefsImprove your storage with bcachefs
Improve your storage with bcachefs
 
Control your service resources with systemd
 Control your service resources with systemd  Control your service resources with systemd
Control your service resources with systemd
 
Comparison of-foss-distributed-storage
Comparison of-foss-distributed-storageComparison of-foss-distributed-storage
Comparison of-foss-distributed-storage
 
Защо и как да обогатяваме знанията си?
Защо и как да обогатяваме знанията си?Защо и как да обогатяваме знанията си?
Защо и как да обогатяваме знанията си?
 
Securing your MySQL server
Securing your MySQL serverSecuring your MySQL server
Securing your MySQL server
 
Sysadmin vs. dev ops
Sysadmin vs. dev opsSysadmin vs. dev ops
Sysadmin vs. dev ops
 
DoS and DDoS mitigations with eBPF, XDP and DPDK
DoS and DDoS mitigations with eBPF, XDP and DPDKDoS and DDoS mitigations with eBPF, XDP and DPDK
DoS and DDoS mitigations with eBPF, XDP and DPDK
 
Challenges with high density networks
Challenges with high density networksChallenges with high density networks
Challenges with high density networks
 
SiteGround building automation
SiteGround building automationSiteGround building automation
SiteGround building automation
 
Preventing cpu side channel attacks with kernel tracking
Preventing cpu side channel attacks with kernel trackingPreventing cpu side channel attacks with kernel tracking
Preventing cpu side channel attacks with kernel tracking
 
Managing a lot of servers
Managing a lot of serversManaging a lot of servers
Managing a lot of servers
 
Let's Encrypt failures
Let's Encrypt failuresLet's Encrypt failures
Let's Encrypt failures
 

Sed and awk

  • 1. S e d & A w k Какво е sed? command line stream editor Какво е awk? pattern scanning and processing language Какво са регулярните изрази? как те могат да помогнат на администратора Мариян Маринов <mm@yuhu.biz>
  • 2. S e d & A w k Азбуката на регулярните изрази ^ - съгласува началото на реда $ - съглавува края на реда [group] – съгласува с един символ от група (group) – групира като отделна променлива - escape-ва стандартен RE знак . - съгласува се с всеки знак | - или qantifiers ? - съгласува 0 или 1 пъти * - съглавува 0 или повече пъти + - съгласува 1 или повече пъти {n} – съгласува точно n пъти {n,m} – съгласува между n и m пъти Мариян Маринов <mm@yuhu.biz>
  • 3. S e d & A w k Азбуката на регулярните изрази {n,} - минимум n пъти {,n} – максимум n пъти променливи $0 - целият string $1 – първата група от () $n – n-тата група от () опции g - global (продължава да съгласува след първото съвпадение) i - case insensitive Мариян Маринов <mm@yuhu.biz>
  • 4. S e d & A w k За какво може да се използва sed? Pattern matching в sed /^$/ - празен ред s/(.*)(.)/1/ - групиране и подмяна s/.*/(&)/ - промяна на целия ред Мариян Маринов <mm@yuhu.biz>
  • 5. S e d & A w k Основни команди на sed 15icola - обхват команда текст обхват на командите: всички редове 114 - само един ред 3,12 - от трети до 12 ред /RE/ - регулярен израз /RE/,/RE/ - от, до регулярен израз /RE/,$ - от регулярен израз до края Мариян Маринов <mm@yuhu.biz>
  • 6. S e d & A w k Основни команди на sed изтриване: /daemon/D - изтрива цял ред въвеждане: 1itata - въвежда tata над първият ред 1аtata - въвежда tata след първият ред извеждане: /pts/p - извежда всеки ред наМариян Маринов <mm@yuhu.biz>
  • 7. S e d & A w k Основни команди на sed подмяна: s/x*/y/i s/.*/(&)/ s/(.*)(.)(.)/23/ 1-9 /field/s/[d|l]$/G s/n//g sed '{s/dsa/ok/};{s/lkl/duh/}' test sed -e 's/dsa/ok/' -e 's/lkl/duh/' test Мариян Маринов <mm@yuhu.biz>
  • 8. S e d & A w k Подценяването на Awk в системната админитстрация ps ax|awk '{print $1}' обхват на командите: {} - всички линии /RE/ {} - всички линии съвпадащи с регуляреният израз /RE1/,/RE2/ {} - всички линии попаднали между първото съвпадение на RE1 и първото съвпадение на RE2 $2 ~ /RE/ {} - всички линии чието второто поле съвпада с регулярния израз Мариян Маринов <mm@yuhu.biz>
  • 9. S e d & A w k Заместител на grep ps aux|grep 'username'|awk '{print $2}' ps aux|awk '/username/ {print $2}' Извеждане на информация с дадена логика tail -f /var/log/exim_mainlog|awk '/SMTP/ { if ($5 != “212.5.11.3”) print “Connection from: “$5 }' tail -f /var/log/exim_mainlog|awk '$5 !~ /212.5.11.3/ { print “Connection from: “$5 }' Мариян Маринов <mm@yuhu.biz>
  • 10. S e d & A w k Оператори: || && ~ !~ !=, ==, <, <<, <=, =>, >>, >, + - * / % ++ -- Вградени променливи: FILENAME - current filename FS - field separator NF - number of fields NR - current line number OFMT - output format for numbers OFS - output field separator RS - record separator $0 - entire input record $n - nth field in the current record Мариян Маринов <mm@yuhu.biz>
  • 11. S e d & A w k Вградени променливи: ARGC - number of arguments on the command line ARGV - an array containing the list of cmd arguments CONVFMT - string conversion format for numbers ('%.6g) ENVIRON - an array containing the list of environment variable FNR - current line field number RT - the text matched by RS Мариян Маринов <mm@yuhu.biz>
  • 12. ` S e d & A w k Обработване на информацията с awk BEGIN {} {} END {} Прости примери с awk: /pattern/ { ++x } END { print “The pattern have been seen”$x”times.”} { total += $2} END { print “Total column count is: “$total} { for (i=NF; i>=1;i--) print $i } Мариян Маринов <mm@yuhu.biz>
  • 13. ` S e d & A w k Escape последователности a b f n r t Мариян Маринов <mm@yuhu.biz>