Академический Документы
Профессиональный Документы
Культура Документы
bartolomeo.montrucchio@polito.it maurizio.rebaudengo@polito.it
Original copyright from Giorgio Di Natale, Stefano Di Carlo and Fabio Maino
Outline
Some commands
Filters
File search
-name pattern Warning: use single quotes if you are using regular expressions -type [b c d l]
It is possible to execute a command on all found files specifying: -exec command \; In command, use \{} for the current file example: find . -name core -exec rm \{} \;
AND: conditions must be listed one after the other OR: use -o NOT: use !
Use ( ) to build complex expressions Example: find . \( -name core -o -size 10b \) find . \( -name test -o ! \( -size 10c \) \) \ is to protect ( from the shell expansion; 10c means 10 byte, while 10b means 10 blocks
grep
In order to search a string in a set of files, it is possible to use: grep [-options] pattern files
grep
Options:
-c counts lines which contain the pattern -i case unsensitive -l shows only names of the files containing the pattern (usually also the line is shown) -n shows the line number -v shows only the lines not containing the pattern
Operating systems - Tools 8
Patterns in grep can be strings or regular expressions Some characters have a special meaning (if there is not a preceding \)
. ^ $
* repetition (zero or more times) + repetition (one or more times) [] any character among those in parentheses [^ ] a character excluding those in parentheses \< beginning of the word \> end of the word
10
In order to do a recursive search in a directory tree: find . -name * -exec grep pattern \{} \; Please note that you should use and not ` (using ` launches the shell)
11
tar
12
-c -f file -v
Example:
13
-x -t -f file -v
extracts files from tarfile tests tarfile name of the tarfile verbose
Examples:
gzip
-1 -9
15
gunzip
16
Filters
A filter is a program receiving data from stdin and producing results on stdout Filters are very useful with I/O redirection and with pipes Examples:
17
Data sorting
-b ignores beginning blanks -d considers only blanks and alphanumeric characters -f case unsensitive
18
Data sorting
-o file writes results on file (default is on stdout) -r reverse ordering -tcar field separator Eg. sort k2.2,2.3 filename orders filename on a field based on characters from 2 to 3 of the second field of the file
19
It is a filter for processing the file as given by script options sed [-n] script [file ] sed [-n][-e script][-f script_file][file ]
Filters stdin as specified in script -n does not repeat stdin on stdout; prints only what is requested from script
20
Script syntax:
[address[,address]] function [args] address: line number or a regular expression function: command on the pattern match args: args of function
21
sed functions
p prints current line d deletes current line q quits y/orig/subs/ modifies orig in subs
22
sed functions
num g p
replaces only num occurrences replaces all the occurrences prints the line if it has replaced something
23
sed: examples
sed 1,3 d filename (deletes from line 1 to 3) sed 3,$ d filename (deletes from line 3 to the end of file) sed -n /^pluto/ p filename (prints only if starts with pluto, because of -n) sed -f sedfile filename
24
1,1
25
1,1
26
{ [only for the first line] s/^/Begin:/ sed: esempi s/$/ -- End/ } sed 1,3 d filename /\/\*.*\*\// d [deletes remarks]
1,1
Awk
28
Basic principle
For each line of the open file, patterns are searched When a match is found, an action is done Syntax is similar to C language
29
Input file
Input file is organized in records (file line) A record is organized in fields (words of the line)
30
Input file
Variables:
record separator field separator (blank) whole record first field n-th field
31
Execution
It is possible to run awk both from command line and from a script Command line:
awk command < inputFile > outputFile awk -f scriptFile < inputFile > outputFile
Script:
32
Command structure
Command structure
If there is not a pattern, action is always executed If there is not an action, the current line is printed
34
Examples
prints all lines in which the first word is Ciao prints the first field of each line of the file
35
Patterns
regular expressions / expr / comparison operators $1 == Ciao operators for intervals ( cond1, cond2 )
36
Regular expressions
escape sequence beginning of the line end of the line one character one of the characters sequence of characters all characters with the exception of
Operating systems - Tools 37
Regular expressions
one|two can be one or two * 0 or more times of the preceding character + 1 or more times of the preceding character ? [AB]? can be A or B or the null string
38
Regular expressions
()
39
40
Comparison operators
equal less than greater than less or equal greater or equal not equal equal as a regular expression not equal as a regular expression
Operating systems - Tools
41
Comparison operators
&& || !
42
$1 == Bob { print Bob stuff; } $1 !~ /[Mm]aggio/ { print Not may; } ($1 == Bob) && ($2 ~ /[mM]*xy?RR$/) { print First field is Bob. Second is wrong; }
43
Interval operators
cond1, cond2
Pattern is true only when first condition is true and remains true until second condition becomes true It works only with nawk o gawk
44
Interval operators
Test ciao 1 number one 2 number two CIAO End 3 number three
Operating systems - Tools
45
Special patterns
BEGIN: run before opening input file END: run after having read all input file
46
Example
Actions
Variables
A field, with a $ ($0, $n) Predefined User defined (declaration is not requested)
49
Predefined variables
ENVIRON
Environment variables (it is an array). E.g.: ENVIRON[PATH] FS field separator (in input) IGNORECASE 0 means case sensitive, 1 no NF number of fields NR number of records already read
Operating systems - Tools 50
Examples
3
AA BB CC
CC
print $NF;
Operating systems - Tools
$NF
51
String operators
Union:
Putting strings one after the other: awk '{x=Hello"; y= everybody"; print x,y;} < /etc/hosts Hello everybody Hello everybody Hello everybody (because /etc/hosts ha three lines)
52
String functions
substitutes in target the first occurrence (all with gsub) of the substring satisfying regular expression reg con string lenght of the string s gives position in string of the first substring satisfying regular expression reg
Operating systems - Tools 53
length (s)
String functions
as in C splits string into the elements of array vec given delimiter delim. Returns the number of elements in vec.
Arrays
55
Arrays
Example:
day[january] = 31; day[february] = 28; january in day ==> gives true delete day[march]
56
Multidimensional arrays
57
Operators
x+y, x-y, x*y, x/y x^y, x%y ++x, x++, --x, x-sin(x), cos(x) rand( ) (random number among 0 and 1) systime( ) (seconds starting from January 1st 1970)
Operating systems - Tools 58
If
59
Loop
do { instruction } while (condition) for (init; cond; op) { instruction } for (i in array) { instruction }
60
Loop (cont)
61
Advance input
exit
is like the end of the file (if END pattern exists, it is run) reads a new line and copies it in $0. Returns 1 if the reading has been successful.
getline
62
Functions
Work only with nawk or gawk function myFunc (parameters) { ... return x; }
63
Shell interaction
system (shellCommand);
runs a shell and executes specified command; e.g. system(ls la) print Ciao > filename.txt; print myVar | more;
using redirection
64
Example
Bookmark file:
+|category url1|description url2|description ...
Example
<HTML><HEAD><TITLE>My bookmarks</TITLE></HEAD> <BODY> <H1>University</H1><BR><UL> <LI> <A HREF="http://www.polito.it"> Politecnico di Torino</A></LI> <LI> <A HREF="http://www.unito.it"> Universit di Torino</A></LI> </UL> <H1>Music</H1><BR><UL> <LI> <A HREF="http://www.mp3.com"> Mp3 web site</A></LI> </UL> </BODY> Operating systems - Tools 66 </HTML>
Header
BEGIN { FS = |; firstTime = 1; printf (<HTML><HEAD><TITLE>); printf (My bookmarks</TITLE></HEAD>); printf (\n<BODY>\n); }
Operating systems - Tools 67
Title
$1 == + { if (firstTime == 0) { print </UL>; } else { firstTime = 0; } print <H1> $2 </H1><BR><UL>; }
Operating systems - Tools 68