INDEX

A

abc.sh, 23

AddSubtract1.sh, 129–130

AlignColumns1.sh, 118–119

aligned-data2.csv, 139

aligned-data.txt, 141

array-from-file.sh, 25–26

array-function.sh, 26–27

array-loops1.sh, 27

Arrays, 24–27

array1.sh, 25

awk command

built-in variables control, 116–117

character sets, 126–127

code sample, 147–149

count occurrences, 135–136

Fields2.sh, 135

Fields3.sh, 137

FixedFieldCount1.sh, 136–137

for loop in, 120–121

Loop.sh in, 120–121

metacharacters, 126–127

multiline records in, 146–147

numeric functions in, 130–132

one-line, 132–133

onlywords.sh, 144–145

printing lines using conditional logic, 127–128

printing the words in, 135

short, 133–134

split() function, 171–172

splitting filenames with, 128

string2date2.sh, 149

test. txt as, 118

VariableColumns.txt, 137

while loop statement, 119–120

working, 117–118

B

Backslash character, 13, 81

“Back tick”, 21–22

bash commands, 4, 55, 104

Bash pipe symbol, 16, 49

bash shell, 3

BEGIN{} block, 117–118, 170

Bourne shell, 2–3

command substitution of, 21–22

Bourne, Stephen R., 2

break statement, for loop in awk command, 121

bunzip2 command, 54–55

bz files, 55

C

cat command, 8, 36, 46

and pipe symbol, 10–11

cd command, 5

Character

backslash, 13, 81

classes, 32–33, 73, 102–103

control, 103–104

dash, 13

intermediate, 72

sets, 126–127

slashes, 97, 100

CheckLogUpdates.sh, 167–168

checkNewUser() function, 60

checkrows.sh, 136

checkuser.sh, 59, 60

chmod command, 12, 23

cmp command, 45

ColumnCount1.sh, 123

columnCount variable, 138

columns.txt, 122–123

columns2.txt, 118

joining alternate lines in, 125

columns4.txt, 71–73, 95, 102–103, 143–144

columns5.txt, 78–79

Command substitution (“back tick”), 21–22

CommandSubst.sh, 22

Comma separated values (CSV), 56

continue statement, for loop in awk command, 121–122

Control characters, 103–104

controlchars.txt file, 103–104

controlm.csv dataset, 50

controlm.sh, 50–51

cp command, 29

cpio command, 53–54

csh shell script, 3

CSV. See Comma separated values

C-type shells, 3

currRow, 142

Customers.txt text file, 165

cut command, 20, 30–31, 75, 164

D

Dash character, 13

datacolumns1.sh dataset, 57–58

datacolumns1.txt dataset, 56, 57

data1.csv, 135

data2.csv, 135–136

Datasets

adding values from multiple, 175–176

aligning columns in, 138–140

contents of, 171

counting words in, 104, 142–144

datacolumns1.sh, 57–58

datacolumns1.txt, 56, 57

deleting alternate lines in, 122

delim1.txt, 99

displaying only “pure” words in, 144–146

with egrep command, 86–88

even rows in, 141–142

finding empty lines in, 79–80

in fixed number of columns, 137

merging lines in, 122–126

with multiple delimiters, 99

multiple rows in, 139–140

scanning diagonal elements in, 172–175

uneven.sh, 58–59

uneven.txt, 58

working with, 100–104

data.txt file, 133–134

data4.txt with sed command, 107–114

dates2.csv, 148–149

def string, 86

deletelines.sh, 122

delim1.csv, 147

delimiter1.sh, 99

delimiter1.txt, 99

delim1.sh, 147

delim1.txt, 99

diagonal.txt, 172

diff command, 45

digits.sh, 124

digits.txt, 124

divisors() function, 157

Divisors2.sh, 156–158

E

echo command, 19, 20–21, 31, 46, 57

EchoCut.sh, 21

egrep command, 85–88, 105

employee.txt, using awk command, 146

END block, 136, 143

env command, 14

Environment variables, 14–16

Error messages, 85

Euclidean algorithm, 154

Executable files, 16–17

Execution time, 66

exp(x) function, 130

F

factorial() function, 62, 63

Factorial.sh, 61–62

Factorial2.sh, 62–63

Factorial3.sh, 63–64

factvalues, 64

fgrep command, 85–88, 88

fib() function, 153

Fibonacci numbers, calculation of, 152–153

Fibonacci.sh, 152–153

Fields2.sh, 135

Fields3.sh, using awk command, 137

File compression commands

bunzip2 command, 54–55

cpio command, 53–54

gunzip command, 54

gzip command, 54

tar command, 52–53

zip command, 55

File contents, 7–8

File ownership, 12

find command, 51–52, 54, 82

findkey.sh, 84

findsvg.sh, 84

FixedFieldCount1.sh, 136–137

fold command, 11–12, 41

for loop, 98

in awk command, 120–121

with break statement, 121

next and continue statement, 121–122

-F switch sets, 117

Function

divisors(), 157

exp(x), 130

fib(), 153

gcd(), 156

gsub(), 170, 182

int(x), 130

items(), 27

lcm(), 156

log(x), 131

rand(), 131–132

sin(x), 131

split(), 171–172, 182

sqrt(x), 132

srand(x), 132

time(), 132

G

gawk command, 116

gcd() function, 156

GCD of two positive integers, 153–154

gcd.sh, 153–154

genetics.txt, 171–172

Global Regular Expression Print (grep), 66–67

grep command, 66–67, 99, 138, 158

backslash character and, 81

character classes and, 73

-c option in, 74–75

egrep command, 85–88

fgrep command, 85–88

intermediate characters, 72

metacharacters and, 67, 68

multiple matches in, 81

simulating relational data with, 164–167

useful options for, 69–73

using back references in, 77–79

and xargs command, 81–84

gsub() function, 170, 182

gunzip command, 54

gzip command, 54

H

head command, 9–10, 75–77

Hidden files, 12–13

history command, 5

HOME environment variable, 14

HOSTNAME environment variable, 14

I

if statement, 57

Intermediate characters, 72

Internal field separator (IFS), 24, 56

int(x) function, 130

Invisible files. see Hidden files

items() function, 27

Iterative solutions, for factorial values, 62–64

J

jar command, 161

join command, 40, 165

JoinLines.sh, 89–90, 125

JoinLines2.sh, 125–126

K

Korn shell, 3

L

lcm() function, 156

lcm.sh, 155–156

linear-combo.sh, 180–181

linepairs.csv, 35, 122

lines.txt, 68

Linux operating system, 2

Log directory, 74

Logfile, checking updates in, 167–169

log(x) function, 131

LOGNAME environment variable, 14

longfile.txt, 75

Loop.sh in awk command, 120–121

Lowest common multiple (LCM) of two positive integers, 155–156

ls command, 6–7

M

Mac OS X operating system, 2, 3

man command, 4

MasterOrders.sh, 165–166

MasterOrders.txt text file, 165

MatchAlpha1.sh, 127

Metacharacters, 31–32, 67, 68

and character sets, 126–127

in egrep expressions, 86

mixed-data.csv, 138

mixed-data2.csv, 139

mixed-data.sh, 138

mixed-data2.sh, 139–140

mixednumbers.txt, 129

mkdir command, 19, 34

more command, 10

Multiline records processing, 169–170

multiline.txt, 169–170

Multiple commands, 33–34

mv command, 13, 98

mykeys.txt, 84, 85

mylogfile.txt, 168

myzip.sh, 158–159

N

namepairs.csv, 36

names.txt, using paste command, 25, 29–30

nawk command, 116

Nested loops, 27–28

nestedloops.sh, 27–28

nocontrol1.txt file, 104

node_modules directory, 83

numbers.txt, 171

numbers.txt file, 92

Numeric functions, in awk command, 130–132

O

od command, 45–46

One-line awk command, 132–133

One-line sed command, 107–114

One Space.txt, 13

P

paste command, 29–30

PATH environment variable, 15

Patterns1.sh, 126

Pipe symbol, 10–11, 33–34

POSIX character, 47

Postfix arithmetic operators, 129–130

Prime divisors, 156–158

printf command, 20, 118–119

Printing lines, 101–102

using conditional logic, 127–128

Problematic filenames, 13

products.txt, 127–128

PurchaseOrders.txt text file, 165

pwd command, 4

Q

quotes3.csv, 147

R

rainfall1.sh, 175–176

rainfall12.sh, 176–177

rand() function, 131–132

Recursion, 61–62

Recursion-based solutions, 151

Regular expressions, 31, 67, 78

RemoveColumn.sh, 140–141

RemoveColumns.txt, 126

reversecolumns.sh, 35, 36–37

rowCount variable, 140

S

Scanning diagonal elements, in datasets, 172–175

searchstrings.sh, 159–160

sed command, 48, 49, 91–92

back references in, 104–105

character classes and, 102–103

data4.txt with, 107–114

delimiter1.sh, 99

execution cycle, 92

matching string using, 92–93

one-line, 107–114

“pure” words in, 105–107

search and replace with, 96–98

substituting string using, 93–96

useful switches in, 99–100

world with, 109

Semicolon, to separate commands, 19–20

Shebang, 18

SHELL environment variable, 15

Shell scripts, 17–19

abc.sh, 23

AlignColumns1.sh, 118

checkrows2.sh, 136

diagonal.sh, 172–173

Divisors2.sh, 156–158

Fibonacci.sh, 152–153

function, 59–61

gcd.sh, 153–154

genetics.sh, 172

joinlines.sh, 30

lcm.sh, 155–156

linear-combo.sh, 180–181

multiline.sh, 170

rainfall1.sh, 175–176

rainfall12.sh, 176–177

recursion and, 61–62

setting environment variables in, 22–24

sourcing/dotting, 23–24

sumrows.sh, 171

WordCounts1.sh, 142–143

WordCounts2.sh, 143

sin(x) function, 131

skuprices.txt, 162

skusold.txt, 80

skutotals.sh, 161–162

skuvalues.txt, 80

Slashes character, 97, 100

sort command, 42–44, 66, 106, 145

split command, 41–42

SplitFilename2.sh, 128

split() function, 130, 171–172, 182

SplitName1.sh, 31

sqrt(x) function, 132

srand(x) function, 132

sub-rows-cols.sh, 141–142

sub-rows-cols.txt, 141

T

Tablet string, 128

tail command, 9–10, 44, 75

tar command, 52–53

tee command, 52

test1.csv, 88–89

testing.tar file, 53

testing.tar.gz, 53

test.sh, 18

test4.txt file

doubled-spaced lines, 101–102

as single line, 123–124

Thompson, Ken, 2

time() function, 132

Torvalds, Linus, 2

tr command, 46–49

U

uneven.sh dataset, 58–59

uneven.txt dataset, 58

uniq command, 44, 106, 145

Unix

operating system, 2–3

pipe symbol, 49

unzip command, 161

update2.sh, 98

USER environment variable, 15

V

VariableColumns.txt, 140

W

wc command, 7

whatis command, 17

where command, 17

whereis command, 17

which command, 16

while loop statement, 119–120

Whitespaces, 20

WordCountInFile.sh file, 104

WordCounts1.sh, 142–143

WordCounts2.sh, 143

X

xargs command, 52, 59, 73, 81–85

Z

zcat command, 55

zip command, 55

zipfiles, 55, 83–84, 158–164