




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





6/16/2005 3 Department of Com
puter and Informati
on Sciences
One option, use “psps” command
Use “whichwhich” command
$ which bash /usr/bin/bash
$ ps PID TTY TIME CMD 2665 pts/2 0:00 bash
6/16/2005 4 Department of Com
puter and Informati
on Sciences
Few points about the Shell (II)Few points about the Shell (II)
Using an environment variable – PATHPATH
Use “envenv” (also “printenvprintenv” on Linux) to display all the environment variables and their corresponding values
$ echo $PATH /usr/bin:/bin:/usr/sbin:/sbin:/hf/local/bin
6/16/2005 5 Department of Com
puter and Informati
on Sciences
For bash, type: PS1=PS1=’’somepromptsomeprompt’’
In general, type: variable=valuevariable=value
Include these changes in the file .bash_profile or .bashrc (for bash shell)
Few points about the Shell (III)Few points about the Shell (III)
puri@blazer1:~[501]$ PS1='[hostname:date] $ ' [blazer1:Sun Feb 13 19:14:06 CST 2005] $
$ echo $PATH /usr/bin:/bin:/usr/sbin:/sbin:/hf/local/bin $ PATH=$PATH:$HOME/bin $ echo $PATH /usr/bin:/bin:/usr/sbin:/sbin:/hf/local/bin:/mz/mb/puri/bin
6/16/2005 6 Department of Com
puter and Informati
on Sciences
cutcut
default delimiter is a tab other delimiters use – –d
By fields: cutcut – –f1,4 myfilef1,4 myfile By character position: cutcut – –c5c5--10 myfile10 myfile
cutcut – –f5,6f5,6 – –d: /etc/passwdd: /etc/passwd ^ lsls^ – –l | cutl | cut^ – –c55c55––
6/16/2005 10 Department of Com
puter and Informati
on Sciences
sort (I)sort (I)
Dictionary order: sortsort – –d myfiled myfile Ignore case: sortsort – –f myfilef myfile Numerical value: sortsort – –n myfilen myfile Reverse sort: sort – –r myfiler myfile
6/16/2005 11 Department of Com
puter and Informati
on Sciences
sort (II)sort (II)
sortsort – –k n filek n file
sort +nsort +n^ – –m filem file^ (start after n delimiters and stop after m delimiters)
sort +nsort +n – –m +pm +p – –q fileq file (field range n-m for primary key, p- q for secondary key)
6/16/2005 12 Department of Com
puter and Informati
on Sciences
joinjoin
Usage: join
joinjoin – –o 2.2 1.2 1.1 file1 file2o 2.2 1.2 1.1 file1 file
joinjoin^ – –j1 nj1 n^ – –j2 m file1 file2j2 m file1 file
6/16/2005 13 Department of Com
puter and Informati
on Sciences
sedsed
Replace all instances of a specific string: sedsed ’’s/abc/ABC/gs/abc/ABC/g’’ myfilemyfile Search specific string and then make a replacement: sedsed^ ’’/xyz/s/abc/ABC/g/xyz/s/abc/ABC/g’’^ myfilemyfile Delete lines: sedsed ’’/abc/d/abc/d’’ myfilemyfile
6/16/2005 14 Department of Com
puter and Informati
on Sciences
trtr
tr a A < myfiletr a A < myfile ^ tr abc XYZ < myfiletr abc XYZ < myfile^ or^ cat myfile | tr abc XYZcat myfile | tr abc XYZ trtr – –dd ’’xyzxyz’’ < myfile< myfile cat /etc/cat /etc/passwdpasswd | tr| tr ’’::’’ ’’ ’’ trtr ’’\nn’’ ’’ ’’ < myfile< myfile
6/16/2005 15 Department of Com
puter and Informati
on Sciences
teetee
lsls^ – –l | tee myfiles | wcl | tee myfiles | wc^ - -ll
6/16/2005 19 Department of Com
puter and Informati
on Sciences
Shell Scripts (II)Shell Scripts (II)
6/16/2005 20 Department of Com
puter and Informati
on Sciences
Creating a complex scriptCreating a complex script
tr -d '?."!:,;' < myfile.in | tr 'A-Z' 'a-z' | tr ' \t' '\n\n'
| sed '/^$/d'
| sort | uniq -c | sort -rn
| tee words.out | wc -l
6/16/2005 21 Department of Com
puter and Informati
on Sciences
AlgorithmAlgorithm
6/16/2005 22 Department of Com
puter and Informati
on Sciences
Implementation (I)Implementation (I)
trtr – –dd ’’?.?.””!:,();!:,();’’ < file< file
trtr^ ’’AA--ZZ’’^ ’’aa--zz’’^ < file< file
replace space and tab with a new line trtr ’’ \tt’’ ’’\nn\nn’’ < file< file
sedsed ’’/^$/d/^$/d’’ filefile search for lines starting with ^ and ending $ with to text in between, and then delete those lines
6/16/2005 23 Department of Com
puter and Informati
on Sciences
Implementation (II)Implementation (II)
sort filesort file
uniquniq – –c filec file
sortsort – –rn filern file (-n numerical sort, -r reverse sort order)
| tee words.outtee words.out
wcwc - -ll
6/16/2005 24 Department of Com
puter and Informati
on Sciences
SolutionSolution
tr -d '?."!:,;' < myfile.in \
| tr 'A-Z' 'a-z' \
| tr ' \t' '\n\n' \
| sed '/^$/d' \
| sort | uniq -c \
| sort -rn \
| tee words.out | wc -l
continuation character – no new line