Tools and Advanced Commands Emanuele Valea <valea@lirmm.fr> - - PowerPoint PPT Presentation

tools and advanced commands
SMART_READER_LITE
LIVE PREVIEW

Tools and Advanced Commands Emanuele Valea <valea@lirmm.fr> - - PowerPoint PPT Presentation

Tools and Advanced Commands Emanuele Valea <valea@lirmm.fr> LIRMM CNRS / Universit de Montpellier Outline Advanced commands: find grep tar, gzip, gunzip Filters: sort cut sed Outline Advanced


slide-1
SLIDE 1

Tools and Advanced Commands

Emanuele Valea <valea@lirmm.fr> LIRMM CNRS / Université de Montpellier

slide-2
SLIDE 2

Outline

  • Advanced commands:
  • find
  • grep
  • tar, gzip, gunzip
  • Filters:
  • sort
  • cut
  • sed
slide-3
SLIDE 3

Outline

  • Advanced commands:
  • find
  • grep
  • tar, gzip, gunzip
  • Filters:
  • Sort
  • cut
  • sed
slide-4
SLIDE 4

Find

  • find <dir> [-opt]
  • Some options:
  • -name pattern

Caveat: put the pattern between ‘...’ when using regular expressions

slide-5
SLIDE 5

Find

  • It’s possible to execute a command that operates on

all the found files:

  • exec command \;
  • in command you can use \{} to select the current

file

  • Example:

find . -name core -exec rm \{} \;

slide-6
SLIDE 6

Find: advanced use

  • The search expression can be composed by more

than one condition in order to build a complex logic condition

  • Logic Operators:
  • AND: specify the conditions one after the other
  • OR: use -o
  • NOT: use !
slide-7
SLIDE 7

Find: advanced use

  • Use parenthesis to build complex expression
  • Example:

find . \( -name core -o -size +2000 \)

slide-8
SLIDE 8

Outline

  • Advanced commands:
  • find
  • grep
  • tar, gzip, gunzip
  • Filters:
  • Sort
  • cut
  • sed
slide-9
SLIDE 9

grep

  • In order to found string occurrences in a list of files,

the following command can be used:

  • grep [-options] pattern files
slide-10
SLIDE 10

grep

  • Options:
  • -c: prints only a count of the lines that contain the

pattern

  • -i: ignores upper/lower case distinction during

comparisons.

  • -l: prints only the names of files with matching lines,

separated by NEWLINE characters

  • -n: precedes each line by its line number in the file

(first line is 1)

  • -v: prints all lines except those that contain the pattern
slide-11
SLIDE 11

Regular Expression in grep

  • The search pattern can be normal string or regular

expression

  • Some characters correspond to special ones (unless

those preceded by \)

  • .

Any character

  • ^

begin of line

  • $

end of line

slide-12
SLIDE 12

Regular Expression in grep (cont)

  • *

repetition (0 or plus)

  • +

repetition (1 or plus)

  • [ ]

Any character inside ‘[ ]’

  • [^ ]

Any character except those inside the ‘[ ]’

  • \<

begin of the word

  • \>

end of the word

  • ^

beginning of the line

  • $

end of the line

slide-13
SLIDE 13

Recursive search in the directories

  • find . -name ‘*’ -exec grep ‘pattern’ \{} \; -print
  • rgrep
slide-14
SLIDE 14

Outline

  • Advanced commands:
  • find
  • grep
  • tar, gzip, gunzip
  • Filters:
  • Sort
  • cut
  • sed
slide-15
SLIDE 15

tar

  • tar [option] files
  • create a file archive (tarfile) and add or extract files
slide-16
SLIDE 16

tar - Creation

  • Options to create a tarfile:
  • -c

create a new tarfile

  • -f file

specify the tarfile name

  • -v

verbose

  • Example:
  • tar -cvf /tmp/valea.tar /home/valea
slide-17
SLIDE 17

tar - Extract

  • Options to extract from a tarfile:
  • -x

Extract files from the tarfile

  • -t

Check the tarfile

  • -f file

Specify the tarfile name

  • -v

Verbose

  • Example:
  • tar -tvf /tmp/valea.tar
  • tar -xvf /tmp/valea.tar
slide-18
SLIDE 18

gzip

  • gzip [opt] file
  • Compress a file
  • Options:
  • -1 fastes
  • -9 max compression
slide-19
SLIDE 19

gunzip

  • gunzip file
  • Decompress a file
slide-20
SLIDE 20

Outline

  • Advanced commands:
  • find
  • grep
  • tar, gzip, gunzip
  • Filters:
  • sort
  • cut
  • sed
slide-21
SLIDE 21

Filter

  • A filter is a program that receives input data from

stdin and generate output data on stdout

  • Filters are very useful with I/O redirection and with

pipe

  • Examples:
  • more, less
  • head, tail
slide-22
SLIDE 22

Outline

  • Advanced commands:
  • find
  • grep
  • tar, gzip, gunzip
  • Filters:
  • sort
  • cut
  • sed
slide-23
SLIDE 23

Data Sort

  • sort [-options] [-k begin[,end]] [file ...]
  • Options:
  • -b Ignores leading blank characters
  • -n Considers the string as number
  • -d Considers only the characters
  • -f Ignores upper/lower case distinction during

comparisons

slide-24
SLIDE 24

Data Sort

  • sort [-options] [-k begin[,end]] [file ...]
  • -o Specifies the name of an output file to be used

instead of the standard output

  • -r Reverses the sense of comparisons
  • -t‘car’

field separator

  • -k “begin” and “end” specify the field to be considered

during sorting, in the format:

  • n-field[.n-character]
slide-25
SLIDE 25

Outline

  • Advanced commands:
  • find
  • grep
  • tar, gzip, gunzip
  • Filters:
  • sort
  • cut
  • sed
slide-26
SLIDE 26

cut

  • It cuts out selected portions of each line of a file
  • cut -f list [-d delim] [file ...]
  • It shows the list specifies fields, separated in the input by

the field delimiter character (-d option)

  • Example: Extract users' login names and shells from

the system passwd file:

  • cut -d : -f 1,7 /etc/passwd
slide-27
SLIDE 27

Outline

  • Advanced commands:
  • find
  • grep
  • tar, gzip, gunzip
  • Filters:
  • sort
  • cut
  • sed
slide-28
SLIDE 28

sed - Stream text EDitor

  • The sed utility reads the specified files, or the

standard input if no files are specified, modifying the input as specified by a list of commands. The input is then written to the standard output

  • sed [-n] script [file …]
  • sed [-n][-e script][-f script_file][file …]
  • -n does not write to stdout
slide-29
SLIDE 29

sed - Stream text EDitor

  • Script syntax:
  • [address[,address]] function [args]
  • address: line number or regular expression
  • function: command to be executed to the address
  • args: function arguments
slide-30
SLIDE 30

sed functions

  • p

print current line

  • d

delete current line

  • q

end execution

  • y/<orig>/<subs>/ replace characters from orig to subs
slide-31
SLIDE 31

sed functions

  • s/<regexp>/<replace>/<flags>

replace patterns that match with regexp with replace

  • flags:
  • num

replace only num occurrences

  • g

replace all the occurrences

  • p

print the replaced

slide-32
SLIDE 32

sed: examples

  • sed ’1,3 d’ <filename>
  • sed ’3,$ d’ <filename>
  • sed -n ’/^example/ p’ <filename>
  • sed -f sedfile filename
slide-33
SLIDE 33

sed: examples

  • sed ’1,3 d’ filename
  • sed ’3,$ d’ filename
  • sed -n ’/^example/ p’ filename
  • sed -f sedfile filename

1,1 { s/^/Begin:/ s/$/ -- End/ } /\/\*.*\*\// d

slide-34
SLIDE 34

sed: examples

  • sed ’1,3 d’ filename
  • sed ’3,$ d’ filename
  • sed -n ’/^example/ p’ filename
  • sed -f sedfile filename

1,1 { s/^/Begin:/ s/$/ -- End/ } /\/\*.*\*\// d One Two Three A B C /* Comment */

slide-35
SLIDE 35

sed: examples

  • sed ’1,3 d’ filename
  • sed ’3,$ d’ filename
  • sed -n ’/^example/ p’ filename
  • sed -f sedfile filename

1,1 { s/^/Begin:/ s/$/ -- End/ } /\/\*.*\*\// d One Two Three A B C /* Comment */ Begin: One Two Three -- End A B C

slide-36
SLIDE 36

Sed: examples

  • Print all the lines of a file unless comments (comments

begin with '#') cat $FILE | sed -e '/^#/d‘

  • Print all the lines that begin with a number

cat $FILE | sed -n "/^[0-9]/ p“

  • Print all the lines that DO NOT begin with a number

cat $FILE | sed "/^[0-9]/ d“

  • Remove all the empty lines

cat $FILE | sed -e "/^$/d"

slide-37
SLIDE 37

Sed: examples

  • Remove all the lines composed by spaces

cat $FILE | sed -e "/^[ ][ ]*$/d“

  • Remove the filename path
  • echo "/usr/bin/example" | sed -e "s/.*\///“
  • Print only the path

echo "/usr/bin/example" | sed -e "s/\/[^\/]*$//"

slide-38
SLIDE 38

Sed: examples

  • Print the 'n’th line of a file (n = 5)

sed -n -e ”5,5p" $FILE

  • Print lines file (from 2 to 5)

sed -n -e "2,5p" $FILE

  • Replace examples:

sed "s/\"/ /g".. Replace all the '"' with space sed "s/,/ /g".. Replace all the ',' with space sed ’s/bin//'.. Replace the first 'bin' with nothing (delete)