Shell Scripting
Basic Shell Scripting
Basic Linux commands:
File descriptors
<, >, >>, 1>, 2>, 2>&1, &>
1> Success output
2> Error output
STD INPUT/OUTPUT options:
1>file_name 2>&1 -> Error out will be stored where you success output stores
&>file_name -> Error out will be stored where you success output stores
Tips:
1>file_name 2>&1 and &>file_name are same but different syntax
Read file contents
(vi,vim, nano, cat, less, )
a) how to set line number in vi editor
vi file_name
: set number
b) how to set line number with cat command
cat -n file_name
c) how to clear screen
clear
OR
ctrl + l
Read a file contents with conditions
more, tail, head, grep, awk, sed
a) more command
more +n : it will display the text from the specified n number of lines of the file
more +4 lines.txt
Example:
--- Original file ---
line1
line2
line3
line4
line5
line6
line7
line8
line9
line10
line11
line12
line13
line14
line15
line16
line17
line18
line19
line20
Output:
line4
line5
line6
line7
line8
line9
line10
line11
line12
line13
line14
line15
line16
line17
line18
line19
line20
line1
line2
--More--(9%)
b) head command
head -> by default head command display 10 lines from a file
head -n -> it displays the top n number of lines from a file
Command:
Example:
line1
line2
c) tail command examples
tail -n : it displays the last n number of lines from a file
Command:
tail -2 lines.txt
Example:
# tail -2 lines.txt ----------------------- display last 2 lines
line20
Read, view, and display the required range of lines from a specified file
a) If I want to display lines from 5 to 10 out of 20 lines file.
Command:
OR
awk 'NR>=5 && NR<=10 {print}' lines.txt
OR
sed -n '5,10p' lines.txt
Example:
line5
line6
line7
line8
line9
line10
# awk 'NR>=5 && NR<=10 {print}' lines.txt
line5
line6
line7
line8
line9
line10
root@ip-172-31-15-20:~# sed -n '5,10p' lines.txt
line5
line6
line7
line8
line9
line10
Grep command Examples
THE FIRST LINE HAS BEEN WRITTEN IN UPPRE CASE.
the second line has been written in lower case.
The Third Line Has Initial Letters Are In Upper Case.
One line above the line is empty.
Here is the details of the file.
And this is the last line of the file.
a) Print the line which matches the search string "above" from a single file.
Syntax:
grep "string/pattern" file
cat file_name |grep "string/pattern"
grep "above" file1.txt
cat file1.txt |grep "above"
Output:
# grep "above" file1.txt
One line above the line is empty.
One line above the line is empty.
b) Print the line which matches the search string "above" from more than one file.
Syntax:
grep "string/pattern" file/files
command:
grep "above" file1.txt file2.txt
output:
root@ip-172-31-15-20:~# grep "above" file1.txt file2.txt
file1.txt:One line above the line is empty.
file2.txt:One line above the line is empty.
c) Grep command options:
basic options : -i, -w, -v, -o, -n, -c, -A, -B, -C, -r, -l, -h
Advance options : -e, -f and E
-i ------> To ignore case for matching/searching
One line above the line is empty. ----------------------> result with ignore case sensitive
-w ------> To match a whole word or exact word
Command:
the second line has been written in lower case.
Three lines above the line is empty.
Here is the details of the file lines.
And this is the last line of the file.
Three lines above the line is empty.
And this is the last line of the file.
Comments
Post a Comment