1 / 34

Basics of the Unix/Linux Environment

Basics of the Unix/Linux Environment. Manipulating & Printing Files; Regular Expressions. CERI Printers. Long Building 3892_grad -- B & W printer in grad area 3892_hpcolor -- Color printer in grad area 3892_hpxlfp -- Poster printer in grad area House 3

denali
Télécharger la présentation

Basics of the Unix/Linux Environment

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Basics of the Unix/Linux Environment Manipulating & Printing Files; Regular Expressions

  2. CERI Printers • Long Building • 3892_grad -- B & W printer in grad area • 3892_hpcolor -- Color printer in grad area • 3892_hpxlfp -- Poster printer in grad area • House 3 • 3876_langston -- B & W printer near Steve’s office • 3876_hpcolor -- Color printer near conference room • 3876_grad – B & W duplex printer in Sun lab

  3. House 2 • 3890_hpcolor – Color printer in copier room • 3890_copy – B & W printer in copier room • House 1 • 3904_tek -- Color printer • 3904_hallway -- B & W printer

  4. Printing Commands • lpr: submit files for printing %lpr -P3892_grad file.txt

  5. lpq: show printer queue status • useful to find out if other jobs are before yours %lpq -P3892_grad 3892_grad is ready and printing Rank Owner Job File(s) Total Size active hdeshon 146 junk.pdf 108544 bytes Identifies the job

  6. lprm: cancel print job %lprm -P3892_grad 146

  7. lpstat: printer status information • useful for finding out printer names on Macs, which are not necessarily the same as on the Unix system %lpstat –a _3876langston accepting requests since Wed Aug 27 13:11:36 2008 hp_color_LaserJet_4600 accepting requests since Mon Aug 4 11:50:47 2008

  8. More useful commands • wc: word or line count %wc suma1.hrdpicks 37753 253998 3561084 suma1.hrdpicks Reports number of lines, words (separator=space), and characters in the file.

  9. cmp: compare files %cmp hw1.txt hw1a.txt hw1.txt hw1a.txt differ: char 175, line 12 No output if the same, else reports byte and line numbers at which the first difference occurred (starts at 1).

  10. diff: difference files %diff hw1.txt hw1a.txt 12c12 < 2) [2] Create a directory in your account for this course - you might call it something like ESCI7205. --- > 2) [2*] Create a directory in your account for this course - you might call it something like ESCI7205. 14c14 Sometimes useful (if files completely different is mess). < for file 1; > for file 2. (if have extra lines, will re-synch, afterwards.)

  11. sort: alphabetical or numeric sort function • sort numerically %sort –ndirList | head –n3 799 799 812 • sort numerically and remove duplicates %sort –n -udirList 799 812 825

  12. sort alphabetically • % more samgps.dat • PELD -33.14318 -70.67493 CAP [5] 1993 1997 1998 1999 2002 CHILE OKRT • COGO -31.15343 -70.97526 CAP [3] 1993 1996 2002 CHILE OKRT • MORA -30.20823 -70.78971 CAP [3] 1993 1996 2002 CHILE OKRT • MOR2 -30.20823 -70.78971 CAP [?] CHILE OKRT • . . . • % sort samgps.dat • ABAC -24.433 -66.217 SAGA [-] ARGENTINA NORT • ABEL -25.667 -65.483 SAGA [-] ARGENTINA NORT • ACOL -30.78337 -66.21338 CAP [3] 1993 1997 2000 ARGENTINA OKRT • ACPM -33.447181 -70.537434 CAP2 [c] continuous (2005-) CHILE • ADLS -26.08449 -67.4191 CAP [2] 1993 1997 ARGENTINA OKRT • AGAL -24.317 -66.467 SAGA [-] ARGENTINA NORT • . . . • Alphabetically with numbers • %sort dirList | head –n3 • 1045 1045 comes before 950 becomes 1 comes before 9 • 1046 • 1113

  13. sort by column or position %head –n3 SUMA.NEW.loc 1 1 1918 9 22 9 54 49.29 -1.698 98.298 15.0 0.0 0.0 ehb FEQ x 2 2 1929 8 1 5 1 49.48 9.599 92.802 30.0 0.0 0.0 ehb FEQ x 3 3 1929 12 9 6 49 57.79 4.003 94.545 20.0 0.0 0.0 ehb FEQ x %sort –k9 –n SUMA.NEW.loc 23 23 1943 6 8 20 42 42.66 -2.938 101.747 30.0 0.0 0.0 ehb FEQ x 376 376 1975 4 13 14 23 5.61 -2.511 99.975 30.0 4.9 0.0 ehb FEQ x 465 465 1977 7 29 21 6 0.7 -2.466 99.870 30.0 5.1 0.0 ehb FEQ d • sort using a different separator (default is white space) %sort -n -t"," -k9 SUMA.NEW.loc.csv 1,1,1918,9,22,9,54,49.29,,,-1.698,,,98.298,,,15.0,,0.0,,0.0,ehb,FEQ,,x 10,10,1935,11,25,10,3,7.39,,,,5.886,,,93.737,,,35.0,,0.0,,0.0,ehb,FEQ,,x 100,100,1964,11,7,0,50,7.03,,,,1.801,,,99.483,,,15.0,,5.0,,0.0,ehb,FEQ,,x

  14. Read the man page to see what else it will do. • NAME • sort - sort, merge, or sequence check text files • SYNOPSIS • /usr/bin/sort [-bcdfimMnru] [-kkeydef] [-o output] [-S kmem] [-t char] [-T directory] [ -y [kmem]] [-zrecsz] [+pos1 [-pos2]] [file...] • /usr/xpg4/bin/sort [-bcdfimMnru] [-kkeydef] [-o output] [-S kmem] [-t char] [-T directory] [ -y [kmem]] [-zrecsz] [+pos1 [-pos2]] [file...]

  15. From now on, you will be expected to read the man pages for all the commands we have used or will use to see how to use them and what they will do.

  16. Time • cal: displays a calendar • default is current month • will also display the year • ok way to figure out julian day using the –j flag %cal September 2009 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 % cal -j September 2009 Su Mo Tu We Th Fr Sa 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273

  17. date: displays date and time %date Wed Aug 27 17:12:01 CDT 2008 %date –u –r 10 Thu Jan 1 00:00:10 UTC 1970

  18. Basic Math • bc: basic math calculator • +, -, *, /, %, ^, sqrt • also test Boolean expressions and >,<,!=, etc. • quit or CNTL-D to exit • expr: evaluate the expression • more powerful, command line calculator for integer math and string comparison • units: unit conversion

  19. Job Control • top: lists all processes currently running • ps: process status, another way to display process identification numbers (PID) %ps –uhdeshon UID PID TTY TIME CMD 501 13255 ttys003 0:00.03 ssh -Y hdeshon@enigma 501 13328 ttys006 0:00.13 gs -sDEVICE=x11 workinglocs.ps 0 13381 ttys006 0:00.00 ps -uhdeshon • kill: allows you to hard kill processes by PID %kill -9 13328

  20. CNTL-Z: suspends the current job • bg: resume job but runs it in the background • this functionality can be set on the command line by adding an & to the end of the command/script • fg: resume job and runs it in the foreground • jobs: lists all jobs running in the background, including their PIDs

  21. Finding/Searching • find: search for files Say I want to see if I have a file “Volcanoes.dat” in my “dem” and “bin” subdirectories. %find ~/dem -name Volcanoes.dat /gaia/home/rsmalley/dem/Volcanoes.dat % find ~/bin -name "*olcanoes*" % OK, that’s nice, but not yet too useful (I could have cd’d into dem and done an ls, no need for a new command).

  22. To make this really useful, we need a way to search for patterns in the filenames (or within files). Enter Regular Expressions. A regular expression is a set of characters that specify a pattern.

  23. Regular Expressions • If you master regular expressions, changing and search for text becomes easy just from the command line. • Regular expressions are accepted input for grep, sed, awk, perl, vim and other unix commands. • Much like learning the shells, it is all about syntax & we’ll just scratch the surface here. • http://www.regular-expressions.info

  24. We are going to have two kinds of special characters, or metacharacters. • Those that mean something special to the shell (such as the “$” on a shell or environment variable or the “/” in a path). • Those that are used to specify a pattern. -------------------- • And will need a way to “turn off”, or escape, the special meaning.

  25. RE examples using grep • grep: search for a pattern • HIGHLY useful & it is worth your time to sit down with the man page %grep DEQ SUMA.NEW.loc 31 31 1953 7 7 4 7 52.01 0.892 100.368 236.8 0.0 0.0 ehb DEQ d 32 32 1953 11 13 16 17 13.21 3.964 96.037 62.7 0.0 0.0 ehb DEQ d

  26. Basic RE symbols • . : Matches a single character %grep B…M SUMA.NEW.loc 799 1988 4 3 14 27 10.65 4.738 94.411 30.0 5.8 5.8 ehb BEQ Md 840 1990 1 22 17 26 12.96 3.913 96.063 46.0 6.0 5.9 ehb BEQ Md

  27. * : Matches zero or more instances of thepreceding character %grep 'MC10*' dblocsat2.pf 23 MC1000 P 1208598406.00526 -1.000 d -1.00 -1.00 n -1.00 -1.00 n 26 MC1000 S 1208598407.86593 -1.000 d -1.00 -1.00 n -1.00 -1.00 n % find ~/dem -name *olcanoes* find: No match. WHY?

  28. ^ : Represents the beginning of a line file example IND.pha # 1918 9 22 9 54 49.29 -1.698 98.298 15.0 0.0 0 0 -0.05 1 COC 274.71 1 P MAN 346.71 1 P ZKW 450.71 1 P # 1926 6 28 3 23 26.82 -0.128 101.514 15.0 0.0 0 0 1.17 2 COC 303.18 1 P %grep ^# IND.pha # 1918 9 22 9 54 49.29 -1.698 98.298 15.0 0.0 0 0 -0.05 1 # 1926 6 28 3 23 26.82 -0.128 101.514 15.0 0.0 0 0 1.17 2 or %grep –c ^# IND.phathe –c flag counts the number of matches 8329

  29. $ : Represents the end of the line file example IND.pha # 1918 9 22 9 54 49.29 -1.698 98.298 15.0 0.0 0 0 -0.05 1 COC 274.71 1 P MAN 346.71 1 P ZKW 450.71 1 P # 1926 6 28 3 23 26.82 -0.128 101.514 15.0 0.0 0 0 1.17 2 COC 303.18 1 P %grep ‘P_*$’ IND.pha | head –n2 COC 274.71 1 P______ MAN 346.71 1 P______ or %grep –c ‘P_*$’ IND.phathe –c flag counts the number of matches 831857

  30. \ : Tells the shell you are looking for a symbol %grep '\*' suma.stations | head –n2 * AGD +11.529000 +042.824000 * AIS -37.797000 +077.569000 • [ ] : Matches members of the sets/ranges within the brackets %grep '[DB]EQ' SUMA.NEW.loc 3478 2005 7 4 16 7 35.23 10.301 93.576 29.9 4.9 0.0 ehb DEQ Md 3480 2005 7 5 1 52 4.16 1.822 97.068 30.0 6.2 6.8 ehb BEQ Md 3481 2005 7 5 7 57 27.19 2.244 94.978 15.7 5.1 4.5 ehb DEQ Md

  31. Non-printable characters • The following syntax works with a range of commands and programs that recognize regular expressions (sed, awk, perl, printf, etc) • \t : for a tab character • \r : for carriage return • \n : for line feed or new line. • \s : for a white space

  32. sed: powerful command line text editor • awk: powerful pattern-directed scanning and processing language • so powerful that we will devote a full week to it in the future

  33. Use find and grep in combination %find ~/src | grepsachdr.c /Users/hdeshon/src/sac_functions/getsachdr.c /Users/hdeshon/src/sac_functions/setsachdr.c

More Related