Presentation is loading. Please wait.

Presentation is loading. Please wait.

UNIX operacinės sistemos Simonas Kareiva 2013 m. rudens semestras.

Similar presentations


Presentation on theme: "UNIX operacinės sistemos Simonas Kareiva 2013 m. rudens semestras."— Presentation transcript:

1 UNIX operacinės sistemos Simonas Kareiva 2013 m. rudens semestras

2 Paskaita #4 awk, sed ir kiti pagalbiniai įrankiai Darbas su masyvais

3 Paskaita #4 – I dalis UNIX komandinės eilutės įrankių apžvalga: cut diff awk sed expand grep strings tr uniq zcat

4 cut > echo aa:bb cc dd:ee | cut -d : -f 2 bb cc dd > echo aa:bb cc dd:ee | cut -d " " -f 2 cc > echo aa:bb cc dd:ee | cut -d " " -f 1,2 aa:bb cc > echo aa:bb cc dd:ee | cut -d " " -f 1,2,3 aa:bb cc dd:ee > cut -d : -f 1,5 /etc/passwd | tail -3 _installer:Installer _atsserver:ATS Server _unknown:Unknown User

5 diff > echo "pirmas\nantras" > 1.txt > echo "pirmas\ntrecias" > 2.txt > diff 1.txt 2.txt 2c2 < antras --- > trecias > diff 1.txt 2.txt > diff.txt > patch -p0 1.txt < diff.txt patching file 1.txt > cat 1.txt pirmas trecias

6 expand > printf "a\tb\n" a b > printf "a\tb\n" | hexdump 0000000 0961 0a62 0000004 > printf "a\tb\n" | expand | hexdump 0000000 2061 2020 2020 2020 0a62 000000a

7 grep > grep ^a /etc/passwd alias:*:81:81:User &:/var/qmail/alias:/nonexistent > grep ^c /etc/passwd cyrus:*:60:60:the cyrus mail server:/nonexistent:/sbin/nologin courier:*:465:465:Courier Mail System:/var/spool/courier:/sbin/nologin > grep -c nologin$ /etc/passwd 29 > tail -f httpd-error.log | grep denied [Sun Oct 4 19:18:12 2009] [error] [client 78.60.155.45] (13)Permission denied: cannot read directory for multi: /users/eduardas/www/ > grep -E '(error|failure)' /var/log/console | grep -vc connect_to 1945

8 strings > strings /var/log/wtmp | grep -A4 simonask simonask 88.223.26.58 Jttypa Jttypc Jttypa > strings /bin/sync | grep error $FreeBSD: src/lib/libc/i386/sys/cerror.S,v 1.10 1999/08/27 23:59:38 peter Exp $ error: Authentication error Exec format error Input/output error Undefined error: 0

9 tr > cat bug.lt | tr e 3 W3 ar3 us3r group of BSD syst3ms. Th3 group was 3stablish3d in th3 summ3r of 2002. It happ3n3d in small city of North Lithuania. > cat bug.lt | tr '[a-z]' '[A-Z]' WE ARE USER GROUP OF BSD SYSTEMS. THE GROUP WAS ESTABLISHED IN THE SUMMER OF 2002. IT HAPPENED IN SMALL CITY OF NORTH LITHUANIA.

10 uniq > grep DNS ppp.log | awk '{ print $10 }' | sort | uniq 194.176.32.142 194.176.32.163 > grep Illegal cache.log | tail -1 2009/10/04 19:37:34| urlParse: Illegal character in hostname 'www,youtube.lt’ > grep Illegal cache.log | cut -d " " -f 8 | sort | uniq -c | sort - rn | head 760 '[2a01:138:a001:201:' 57 '2a01%3a138%3aa001%3a201%3a%3a24' 57 '2a01%3a138%3aa001%3a201%3a%3a23' 57 '2a01%3a138%3aa001%3a201%3a%3a22' 57 '2a01%3a138%3aa001%3a201%3a%3a21' 12 '%20ssl.google-analytics.com' 7 'www,youtube,com' 4 'evi � ka-pc' 3 'www,youtube.lt' 3 'vista.%20liedm.lt’

11 zcat > cat > test2.sh #!/bin/sh echo date exit 0; > gzip test2.sh > cat test2.sh.Z ;??Jtest2.shSV?O???/??JM??WHI,I?J??,Q0???d?I> > zcat test2.sh.Z #!/bin/sh echo date exit 0;

12 sed > echo diena | sed s/diena/naktis/ naktis > echo "123 abc" | sed 's/[0-9]*/& &/' 123 123 abc > echo kaire desine | sed 's/\([a-z]*\) \([a-z]*\)/\2 \1/' desine kaire > sed '/start/,/stop/ s/#.*//' ?

13 sed Palyginkite: sed 's/\([a-zA-Z]*\) \([a-zA-Z]*\) /\1 /' sed 's/[a-zA-Z]* //2' sed 's/./&:/20' sed 's/^..................../&:/' sed -n 's/jonas/&/p' <file grep jonas file sed 'y/abcdef/ABCDEF/’ tr abcdef ABCDEF

14 sed #!/bin/sh sed -n ' /begin/,/end/ { s/#.*// s/[ ^I]*$// /^$/ d p } ’

15 sed #!/bin/sh # Tokiu budu isvalome eilutes kurios prasideda # # (komentarus) tarp zodziu ‘begin’ ir ‘end’ sed -n ' /begin/,/end/ { s/#.*// s/[ ^I]*$// /^$/ d p } '

16 sed (advanced) > cat cat.txt This is my cat my cat's name is betty This is my dog my dog's name is frank > sed 'N;s/\n / /;P;D;' cat.txt This is my cat my cat's name is betty This is my dog my dog's name is frank N - pridedame sekančią eilutę prie darbinio buferio s - keitimas /\n / - naujos eilutės ir tarpo atitikimas / / - keičiamas į vieną tarpą P - atspausdina pirmą buferio eilutę D ištrina pirmą buferio eilutę ir tęsia darbą

17 awk > ls -l -rwxrwxrwx@ 1 simonask staff 7497216 Rgs 14 15:59 UNIX_1.pps > ls -l | awk ' BEGIN { print ”Byla\t\tSavininkas" } { print $9, "\t\t", $3} END { print " - DONE -" } ' BylaSavininkas UNIX_1.pps simonask - DONE - > date Sekmadienis, 2009 m. spalio 4 d. 20:21:58 EEST > date | awk '{print $1}' Sekmadienis, > date | awk '{print $2}' 2009

18 awk BEGIN { print ”ivesk skaiciu"; } { print ”Skaiciaus ", $1, ” kvadratas yra ", $1*$1; print ”ivesk dar viena skaiciu"; } END { print ”Ate" }

19 awk awk '{print $2, $1}' skaityk.txt awk '{print NR "\t" $0}’ skaityk.txt awk '{l=length();s=int((79-l)/2); printf "%"(s+l)"s\n",$0}' Palyginkite: head -10 awk 'NR < 11’ awk '!/paieska/' skaityk.txt grep –v paieska skaityk.txt

20 užduotys ls -d *.sh | sed 's/\(.*\).sh$/cp "&" \1/' | sh ____________________________________________________________ STR="`sed <<<"$STR" "s/\\\\(.\\\\)/\\\\1('\\\\\\\\s*\\\\\\\\+\\\\\\\\s*')?/g;s/( '\\\\\\\\s\\\\*\\\\\\\\+\\\\\\\\s\\\\*')?$//"`" ____________________________________________________________ find ${1-.} -type d -print | env LC_ALL='C' sort -if | sed-e "s,^${1-.},," \ -e "/^$/d" \ -e "s,[^/]*/\([^/]*\)$,\`-----\1," \ -e "s,[^/]*/,|,g"

21 Spec. kintamieji $0 pačios komandos pavadinimas. $# pateiktų argumentų skaičius $? prieš tai dirbusio proceso išėjimo būsena $$ šio interpretatoriaus proceso numeris $! paskutinio foninio PID numeris $- dabartinio interpretatoriaus paleisties parametrai $* visi parametrai, pradedant nuo $1. $@kaip ir $*, tik naudojamas kabutėse

22 /bin/sh masyvai area[11]=23 area[13]=37 area[51]=UFOs echo ${area[11]}

23 Operacijos su masyvais area[5]=`expr ${area[11]} + ${area[13]}` area[6]=`expr ${area[11]} + ${area[51]}` Kur klaida?

24 Kiti būdai area2=( nulis vienas du trys keturi ) echo ${area2[0]} area3=([17]=septyniolika [24]=dvidesimtketuri) array1=( `cat "$filename" | tr '\n' ' '`) read -a colors base64_charset=( {A..Z} {a..z} {0..9} + / = )

25 Tipo nėra string=abcABC123ABCabc echo ${string[@]} # abcABC123ABCabc echo ${string[*]} # abcABC123ABCabc echo ${string[0]} # abcABC123ABCabc echo ${string[1]} # echo ${#string[@]} # 1

26 Prisiminkime ir pritaikykime array= ( nulis vienas du trys keturi penki ) # Elementai: 0 1 2 3 4 5 echo ${array[0]} # nulis echo ${array:0} # nulis echo ${array:1} # ulis echo ${#array[0]} # 5 echo ${#array} echo ${#array[1]} # 6 echo ${#array[*]} # 6 echo ${#array[@]} # 6

27 Operacijos su eilutėmis arrayZ=( one two three four five five ) echo ${arrayZ[@]:0} # one two three four five five echo ${arrayZ[@]:1} # two three four five five echo ${arrayZ[@]:1:2} # two three echo ${arrayZ[@]#f*r} # one two three five five echo ${arrayZ[@]##t*e} # one two four five five echo ${arrayZ[@]%h*e} # one two t four five five echo ${arrayZ[@]%t*e} # one two four five five

28 Elementų keitimas (replacement) echo ${arrayZ[@]%t*e} # one two four five five echo ${arrayZ[@]//iv/YY} # one two three four fYYe fYYe echo ${arrayZ[@]//fi/} # one two three four ve ve echo ${arrayZ[@]/#fi/XY}# one two three four XYve Xyve echo ${arrayZ[@]/%ve/ZZ} # one two three four fiZZ fiZZ echo ${arrayZ[@]/%o/XX} # one twXX three four five five

29 Skaidrė #5 – naudingi scenarijai Praktiniai pavyzdžiai kaip išgauti naudos iš scenarijų Kai kuriuos dažnai naudoja ir dėstytojas Atkreipkite dėmesį, jog naudojami įvairūs išoriniai įrankiai

30 singleton #!/bin/bash echo "argument: $1" RESULT=`/bin/ps xauwww | /bin/grep -v "$0" | /bin/grep "^$USER.*$1$"` if [ -z "$RESULT" ] ; then LC_MESSAGES=en_US $* else echo "Sorry, already running as:" echo " $RESULT” echo "singleton reports that $1 is already started” fi #end

31 mysqltcpdump # tcpdump -i eth0 -s 0 -l -w - dst port 3306 | strings | perl -e ' while(<>) { chomp; next if /^[^ ]+[ ]*$/; if(/^(SELECT|UPDATE|DELETE|INSERT|SET|COMMIT|ROLLBACK|CRE ATE|DROP|ALTER)/i) { if (defined $q) { print "$qn"; } $q=$_; } else { $_ =~ s/^[ t]+//; $q.=" $_"; } }'

32 wipeout #!/bin/bash time="2003-04-25 00:00" reffile=wipeout.ref.$RANDOM touch -d "$time" $reffile echo echo Deletes all dirs that are older than $time echo find. -type d -maxdepth 1 -path './*' ! -newer $reffile | while read dir; do echo rm -rf "$dir" rm -rf "$dir" done rm -f $reffile

33 ts2date !#/bin/bash #EPOCH=$1 EPOCH=1000000000 DATE=$(perl -e “require ‘ctime.pl’; print &ctime($EPOCH);”) echo $DATE DATE=$(perl -e “print scalar(localtime($EPOCH))”) echo $DATE DATE=$(echo $EPOCH|awk ‘{print strftime(”%c”,$1)}’) echo $DATE

34 mrename ls -d *.php3 | \ sed 's/\(.*\).php3$/mv "&" "\1.php"/' | \ sh

35 #!/usr/bin/perl -w # camel code use strict; $_='ev al("seek\040D ATA,0, 0;");foreach(1..3) { ;}my @camel1hump;my$camel; my$Camel ;while( ){$_=sprintf("%-6 9s",$_);my@dromedary 1=split(//);if(defined($ _= )){@camel1hum p=split(//);}while(@dromeda ry1){my$camel1hump=0 ;my$CAMEL=3;if(defined($_=shif t(@dromedary1 ))&&/\S/){$camel1hump+=1<<$CAMEL;} $CAMEL--;if(d efined($_=shift(@dromedary1))&&/\S/){ $camel1hump+=1 <<$CAMEL;}$CAMEL--;if(defined($_=shift( @camel1hump))&&/\S/){$camel1hump+=1<<$CAMEL;}$CAMEL--;if( defined($_=shift(@camel1hump))&&/\S/){$camel1hump+=1<<$CAME L;;}$camel.=(split(//,"\040..m`{/J\047\134}L^7FX"))[$camel1h ump];}$camel.="\n";}@camel1hump=split(/\n/,$camel);foreach(@ camel1hump){chomp;$Camel=$_;y/LJF7\173\175`\047/\061\062\063\ 064\065\066\067\070/;y/12345678/JL7F\175\173\047`/;$_=reverse; print"$_\040$Camel\n";}foreach(@camel1hump){chomp;$Camel=$_;y /LJF7\173\175`\047/12345678/;y/12345678/JL7F\175\173\0 47`/; $_=reverse;print"\040$_$Camel\n";}';;s/\s*//g;;eval; eval ("seek\040DATA,0,0;");undef$/;$_= ;s/\s*//g;( );;s ;^.*_;;;map{eval"print\"$_\"";}/.{4}/g; __DATA__ \124 \1 50\145\040\165\163\145\040\157\1 46\040\1 41\0 40\143\141 \155\145\1 54\040\1 51\155\ 141 \147\145\0 40\151\156 \040\141 \163\16 3\ 157\143\ 151\141\16 4\151\1 57\156 \040\167 \151\164\1 50\040\ 120\1 45\162\ 154\040\15 1\163\ 040\14 1\040\1 64\162\1 41\144 \145\ 155\14 1\162\ 153\04 0\157 \146\ 040\11 7\047\ 122\1 45\15 1\154\1 54\171 \040 \046\ 012\101\16 3\16 3\15 7\143\15 1\14 1\16 4\145\163 \054 \040 \111\156\14 3\056 \040\ 125\163\145\14 4\040\ 167\1 51\164\1 50\0 40\160\ 145\162 \155\151 \163\163 \151\1 57\156\056


Download ppt "UNIX operacinės sistemos Simonas Kareiva 2013 m. rudens semestras."

Similar presentations


Ads by Google