




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Class: UNIX Operating Sys Fundamental; Subject: Computer Science; University: University of Alabama - Birmingham; Term: Summer A 2005;
Typology: Study notes
1 / 8
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