Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 5

Unix Commands

man get manual page on a UNIX command


example: man uniq
cut extract columns of data
example: cut -f -3,5,7-9 -d ' ' infile ! outfile
-f ",#-$ field
-c 35-## c%aracter
-d ':' delimiter &default is a ta'(
sort sort lines of a file &)arning: default delimiter is
*%ite space+c%aracter transition(
example: sort -nr infile , more
-n numeric sort
-r re-erse sort
-. 3,5 start .e/
wc count lines, *ords, and c%aracters in a file
example: *c -l infile
-l count lines
-* count *ords
-c count c%aracters
paste reattac% columns of data
example: paste infile infile" ! outfile"
cat concatenate files toget%er
example: cat infile infile" ! outfile"
-n num'er lines
--et s%o* non-printing c%aracters &good
for finding pro'lems(
uniq remo-e duplicate lines &normall/ from a sorted file(
example: sort infile , uniq -c ! outfile"
-c s%o* count of lines
-d onl/ s%o* duplicate lines
join perform a relational 0oin on t*o files
example: 0oin - -" 3 infile infile" ! outfile
- 1I234 0oin field of infile
-" 1I234 0oin field of infile"
cmp compare t*o files
example: cmp infile infile"
diff or diff3 compare " or 3 files - s%o* differences
example: diff infile infile" , more
example: diff3 infile infile" infile3 ! outfile
head extract lines from a file counting from t%e 'eginning
example: %ead -55 infile ! outfile
tail extract lines from a file counting from t%e end
example: tail 6" infile ! outfile
-n count from end of file &n is an integer(
6n count from 'eginning of file &n is an
integer(
dos2unix con-ert dos-'ased c%aracters to UNIX format &t%e
file is
o-er*ritten(7
example: dos"unix infile
tr translate c%aracters - example s%o*s replacement of
spaces
*it% ne*line c%aracter
example: tr 8 8 89:5";<8 = infile ! outfile
grep extract lines from a file 'ased on searc% strings and
regular expressions
example: grep '>asin' infile ! outfile"
example: grep -2 '5:"5,5:5' infile , more
sed searc% and replace parts of a file 'ased on regular
expressions
example: sed -e 's+#55+#5+g' infile ! outfile3
Regular Expressions
?egular expressions can 'e used *it% man/ programs including
ls, grep, sed,
-i, emacs, perl, etc7 >e a*are t%at eac% program %as
-ariations on usage7
ls examples:
ls 4ata;7txt
ls 4ata#95-9<7ps list ps files 'eginning *it% 4ata
num'ered #5-#9
sed examples: &t%ese are t%e regex part of t%e sed command
onl/(
s+#55+#5+g searc% for '#55' replace *it% '#5'
e-er/*%ere
s+99+-9999:755+g searc% for all '99' replace *it%
'-9999755'
s+>asin95-9<++g remo-e t%e *ord >asin
follo*ed '/ a single digit
s+@"+"XX+ searc% for '"' at t%e 'eginning
of a line,
insert XX
s+>asinA++ remo-e t%e *ord >asin if it is at
t%e end of
t%e line7
s+@>asinA++ remo-e t%e *ord >asin if it is t%e
onl/ *ord on
t%e line7
s+9cB<+55+g searc% for 'c' or 'B' replace *it%
55
#5,As+:&95-9<95-9<:(:7:&95-9<95-9<:(+:":7:+g
on lines #5 to t%e end of file,
searc% for t*o digits
follo*ed '/ a '7' follo*ed '/ t*o
digits7 replace
*it% t%e digit pairs re-ersed7
",As+,:&9@,<;:(,+,:8::8,+
on all lines except t%e first,
searc% for a comma,
follo*ed '/ an/ text, follo*ed '/
a comma7 replace
t%e found text surrounded '/
dou'le quotes7

s+:&95-9<95-9<:(::&95-9<95-9<:(::&95-9<95-9<95-9<95-
9<:(+Cear D :3, Eont% D :", 4a/ D :+
searc% for " digits, follo*ed '/ a
colon, follo*ed '/ " digits,
follo*ed '/ a colon, follo*ed '/ #
digits7 replace *it%
text plus -alues in a different
order7
Fipes, standard input, standard output:
Gtandard output, 8!8, places t%e results of a command into
t%e file named
after t%e 8!87 H ne* file *ill 'e *ritten &an old file *it%
t%e same name
*ill 'e remo-ed(7 In order to append to an existing file
use 8!!87
Fipes allo* /ou to connect multiple commands toget%er to
form a data stream7
1or example, to count t%e num'er of times t%e string 8Nile8
occurs in t%e
3rd column of a file run t%is:
cut -f 3 infile , sort , uniq -c , grep 'Nile'
or do t%is:
cut -f 3 infile , grep 'Nile' , *c -l
1rom a glo'al GIN Httri'utes data set &ta' delimited(:
- extract all Nort% Hmerican 'asins draining into t%e
Htlantic Jcean
- select onl/ columns ",3,#,5,,",3, and 7
- replace all missing data -alues &eit%er -99 or -999(
*it% -999975
- remo-e duplicate lines
- sort '/ t%e first column
- num'er all lines sequentiall/
- sa-e to a ne* file
grep 'Nort% Hmerica' GINHttri'utes7txt , grep 'Htlantic
Jcean' :
, cut -f "-5,-3,7 , sed -e 's+-99:,-999+-9999:75+g'
:
, sort , uniq , cat -n ! Ne*GINHttri'utes7txt

You might also like