Bash Ch01
Bash Ch01
Bash Ch01
Basics
Wildcards
Recall what below stuff mean
1. * , ? , [ ] , [! ]
2. If the string doesnt match anything it will leave the string with the value
untouched
3. [a-zA-Z0-9-_]
4. [!a-zA-Z] matches any character that isnt a letter. To match ! itself, place
it after the first character in the set or precede it with backslash \ eg: [\!]
5. Brace Expansion {}
B{ar{d,n,k},ed}s
6. Difference between using [] and {}
Unix I/O
1. Takes sequence of characters (bytes)
2. Everything that accepts or produces data is treated as a file
3. Unix has a single way of accepting
a. Output as Standard Output
b. Input as Standard Input
c. Error as Standard Error or Standard Error Output
4. Commands usually take arguments. But lets say they dont take any
arguments then,
a. For input : command < filename
b. For output : command > filename
c. Both sides: cat < filename1 > fileName2
5. Pipe : cut -d: -f1 < /etc/passwd | sort | lp
Shell utilities
CAT
Syntax:
cat filename
-A, --show-all
equivalent to -vET
-b, --number-nonblank
number nonblank output lines
-e
equivalent to -vE
-E, --show-ends
display $ at end of each line
-n, --number
number all output lines
-s, --squeeze-blank
never more than one single blank line
-t
equivalent to -vT
-T, --show-tabs
display TAB characters as ^I
-u
(ignored)
-v, --show-nonprinting
use ^ and M- notation, except for LFD and TAB
GREP
1. grep search-string filenames
SORT
1 Basic command usage
$ sort
b
z
a
w
s
2. Sort numbers
In the following example, I filled a text file (sort.txt) with some random numbers.
$ cat sort.txt
8
2
6
1
5
3
Then I used the sort command with sort.txt as input file to the command.
$ sort sort.txt
1
2
3
5
6
8
3. Sorting words
In this example, the sort.txt file is filled with some words.
$ cat sort.txt
UK
Australia
Newzealand
Brazil
America
So we see that words were sorted according to dictionary ordering. Even the
words beginning with same alphabet were sorted according to succeeding
alphabets.
Bye
So the output suggests that the input was first sorted and then written to file.
2
4
8
9
So we see that the output was actually written to the file whose name was
supplied as input to sort through -o option.
7. Sort months
There is an interesting option -M through which the month names can be sorted.
Here is an example :
$ sort -M > sort.txt
DEC
JAN
FEB
2G
3M
1K
So we see that this time the sorting results were written in reverse sorted order.
$
1
4
7
9
sort -n sort.txt
apple
oranges
mangoes
grapes
CUT
Reference: http://www.computerhope.com/unix/ucut.htm
Default delimiter is tab
-f
cut -f 3 data.txt
cut -f 2-4 data.txt
cut -f 1-2,4-5 data.txt
cut -f 3- data.txt
-c
cut -c 3-12 data.txt
-b
cut -b 3-12 data.txt
Specifiying delimiter
cut -f 1 -d ':' /etc/passwd
cut -f 1,3 -d ':' /etc/passwd
cut -f 1,3 -d ':' --output-delimiter=' ' /etc/passwd
cut -f 1,3 -d ':' --output-delimiter=$'\t' /etc/passwd
SEED
1. Deleting lines in a file using sed command
1.1. Deleting the first line in the input file
bash-3.00# sed '1d' textfile.txt
This is line 2
This is line 3
This is line 4
This is line 5
1.2 Deleting the last line in the input file
bash-3.00# sed '$d' textfile.txt
This is line 1
This is line 2
This is line 3
This is line 4
bash-3.00#
1.3 Deleting arbitary lines in the input file
To delete the lines 2 through 4 in the above file, use the following sed
command
bash-3.00# sed '2,4d' textfile.txt
This is line 1
This is line 5
the 'd' flag is used to delete lines, though the single quote is not
mandatory in the above commands (you can simply use sed 1d textfile.txt
to delete the first line in the input file), its advisable to use it as a good
practice and will be useful when you use the -e option at times to execute
a series of sed commands inline.
The following tr command is used to convert the lower case to upper case
$ tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ
thegeekstuff
THEGEEKSTUFF
The following command will also convert lower case to upper case
$ tr [:lower:] [:upper:]
thegeekstuff
THEGEEKSTUFF
You can also use ranges in tr. The following command uses ranges to convert lower to upper
case.
$ tr a-z A-Z
thegeekstuff
THEGEEKSTUFF
You can also translate from and to a file. In this example we will translate braces in a file with
parenthesis.
$ tr '{}' '()' < inputfile > outputfile
The above command will read each character from inputfile, translate if it is a brace, and
write the output in outputfile.
3. Translate white-space to tabs
In Example 3, we see how to translate space with tabs. But if there are two are more spaces
present continuously, then the previous command will translate each spaces to a tab as
follows.
$ echo "This
This
is
is
for
Similarly you can convert multiple continuous spaces with a single space
$ echo "This is for testing" | tr -s [:space:] ' '
This is for testing
Also, if you like to delete lines from file, you can use sed d command.
6. Complement the sets using -c option
You can complement the SET1 using -c option. For example, to remove all characters except
digits, you can use the following.
$ echo "my username is 432234" | tr -cd [:digit:]
432234
The following command can be used to remove all non-printable characters from a file.
$ tr -cd [:print:] < file.txt
The below command will translate all newlines into spaces and make the result as a single
line.
$ tr -s '\n' ' ' < file.txt
Shortcuts
Cursor movement
By letter
a.
b.
c.
d.
a.
b.
c.
d.
By Word
By Line
a. CTRL + A : moves to the beginning of the line
b. CTRL + E : moves to the end of the line
c. CTRL + K : kill rest of the line from cursor point (including cursor
point)