tools and advanced commands
play

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


  1. Tools and Advanced Commands Emanuele Valea <valea@lirmm.fr> LIRMM CNRS / Université de Montpellier

  2. Outline • Advanced commands: • find • grep • tar, gzip, gunzip • Filters: • sort • cut • sed

  3. Outline • Advanced commands: • find • grep • tar, gzip, gunzip • Filters: • Sort • cut • sed

  4. Find • find <dir> [-opt] • Some options: • -name pattern Caveat: put the pattern between ‘...’ when using regular expressions

  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 \{} \;

  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 !

  7. Find: advanced use • Use parenthesis to build complex expression • Example: find . \( -name core -o -size +2000 \)

  8. Outline • Advanced commands: • find • grep • tar, gzip, gunzip • Filters: • Sort • cut • sed

  9. grep • In order to found string occurrences in a list of files, the following command can be used: • grep [-options] pattern files

  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

  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

  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

  13. Recursive search in the directories • find . - name ‘*’ - exec grep ‘pattern’ \{} \; -print • rgrep

  14. Outline • Advanced commands: • find • grep • tar, gzip, gunzip • Filters: • Sort • cut • sed

  15. tar • tar [option] files • create a file archive (tarfile) and add or extract files

  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

  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

  18. gzip • gzip [opt] file • Compress a file • Options: • -1 fastes • -9 max compression

  19. gunzip • gunzip file • Decompress a file

  20. Outline • Advanced commands: • find • grep • tar, gzip, gunzip • Filters : • sort • cut • sed

  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

  22. Outline • Advanced commands: • find • grep • tar, gzip, gunzip • Filters: • sort • cut • sed

  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

  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]

  25. Outline • Advanced commands: • find • grep • tar, gzip, gunzip • Filters: • sort • cut • sed

  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

  27. Outline • Advanced commands: • find • grep • tar, gzip, gunzip • Filters: • sort • cut • sed

  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

  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

  30. sed functions • p print current line • d delete current line • q end execution • y/<orig>/<subs>/ replace characters from orig to subs

  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

  32. sed: examples • sed ’1,3 d’ <filename> • sed ’3,$ d’ <filename> • sed - n ’/^example/ p’ <filename> • sed -f sedfile filename

  33. sed: examples 1,1 { s/^/Begin:/ s/$/ -- End/ } • sed ’1,3 d’ filename /\/\*.*\*\// d • sed ’3,$ d’ filename • sed - n ’/^example/ p’ filename • sed -f sedfile filename

  34. sed: examples 1,1 { s/^/Begin:/ s/$/ -- End/ } • sed ’1,3 d’ filename /\/\*.*\*\// d • sed ’3,$ d’ filename • sed - n ’/^example/ p’ filename • sed -f sedfile filename One Two Three A B C /* Comment */

  35. sed: examples 1,1 { s/^/Begin:/ s/$/ -- End/ } • sed ’1,3 d’ filename /\/\*.*\*\// d • sed ’3,$ d’ filename • sed - n ’/^example/ p’ filename • sed -f sedfile filename Begin: One Two Three -- End A B C One Two Three A B C /* Comment */

  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"

  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/\/[^\/]*$//"

  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)

Recommend


More recommend