Basic concepts of linux

Day 7
Hello everyone welcome back !!!
Myself Swanik Santosh Gudekar and I am back with my blog. This is the seventh blog of Linux series. Today I attended the seventh session conducted by Master Pranav Jambare at Dr. Babasaheb Ambedkar Technological University.
Points Covered In This Session :
Filtration
Shell Scripting
IF Else Statement
Filtration :
Filtration is used to process and manipulate data using varieties of commands.
Filtration is an important tool for data process and analysis in Linux.
Filtration allows us to extract specific data, remove unwanted information or modify the output to suit your need.
Some commonly used command are as follow :
1) Sort :
Sort command is used to sort lines of text in a specified file or from the standard input in an ascending/descending order.
The sorted output can be written to a file or displayed on the standard output.
#Syntax: #To sort the data in alphabetical order: sort <filename> #To sort the data in reverse alphabetical order: sort -r <filename> #To sort the numbers given: sort -n <filename> #To sort the given number in reverse order: sort -nr <filename> #To check whether the files are sorted or not: sort -c <filename> #To sort and remove duplicate entries: sort -u <filename> #Months sorted sort -M <filename>
2) Tail :
Tail is a command which prints the last few lines of a certain file, then terminates.
If the number of lines is not specified then by default prints the last 10 lines.
#Syntax #You will get as it is output tail <filename> #Here -n stands for number and print last entries tail -n <filename> #Example: #Last two line will get printed: tail -2 <filename> #It will simply show the output of two files tail <file1> <file2> #To merge two files together tail -q <file1> <file2> #To get the dynamic output tail -f <filename>
3) Head :
- Displays the first n lines of the specified text files. If the number of lines is not specified then by default prints the first 10 lines.
#Syntax:
#You will get the output as it is
head <filename>
#Here -n stands for number and print First entries
head -n <filename>
#Example:
#First two line will get printed:
head -2 <filename>
#To merge two files together
head -q <file1> <file2>
4) WC (word count) :
- wc command gives the number of lines, words and characters in the data.
#Syntax
#Counts the number of lines in the file
wc -l <filename>
#Counts the number of characters in the file
wc -m <filename>
#Counts number of bytes of content of the file
wc -c <filename>
#Counts number of words in the file
wc -w <filename>
5) uniq :
- Removes duplicate lines. uniq has a limitation that it can only remove continuous duplicate lines(although this can be fixed by the use of piping).
#Syntax
#Counts the number of occurences of lines/words in the file
uniq -c <filename>
#To print only repeated lines in the file
uniq -d <filename>
#To print the words that occur more than once in a individual line or repeated with occurence
uniq -D <filename>
#To print unique lines in the file
uniq -u <filename>
6) comm :
- Compare two sorted files line by line and prints the lines that are unique to each file .
#Syntax
#It compares the content of two file and print the common content between two files
comm <file1> <file2>
7) diff :
- Compare the contents of two files and prints the differences between them.
#Syntax:
#It compares the content of two file and print the differences between two files
diff <file1> <file2>
8) grep :
- grep is used to search a particular information from a text file.
#Syntax
#To search a given or exact word
grep <Word> <filename>
#To search a specific word - case insensative
grep <Word> -i <filename>
#Prints the occurence of words in the lines
grep <Word> -o <filename>
#Prints the output after the result
grep <Word> -A <filename>
#To print output before the result
grep <Word> -B <filename>
#To print output after and before the result
grep <Word> -C <filename>
#Prints the count of occurence of line or word in the file
grep -c <Word> <filename>
#It highlights the exact word specified
grep -w <Word> <filename>
#Find or highlight multiple words specified
grep -e <Word> -e <Word> <filename>
#Prints the matched lines with line numbers
grep -n <Word> <filename>
9) awk :
#Prints the whole text or print all lines
awk '{print}' <filename>
#Only lines with the word will get printed other lines won't get printed
awk '/word/' '{print}' <filename>
#Prints the first and third word of each line
awk '{print $1, $4}' <filename>
#Prints first and last word of each line
awk '{print $1,$NF}' <filename>
#The pattern will print the match lines from 2 to 4
awk 'NR==2, NR==4' '{print $NR, $0}' <filename>
10) cut :
#Syntax
#Prints the 3rd field.
#Where -d = Deluminator, -f = fields
cut -d" " -f3 <filename>
11) sed :
sed stands for stream editor. It allows us to apply search and replace operations on our data effectively.
sed is quite an advanced filter and all its options can be seen on its man page.
#Syntax
#The second occurence of the word is to be replaced
sed 's/word to be replaced/replaced with/2' <filename>
#All occurences of the word is to be replaced
sed 's/word to be replaced/replaced with/g' <filename>
#The word is to be replaced at the third line
sed '3 s/word to be replaced/replaced with/ ' <filename>
#To delete the last line
sed '$d' <filename>
#Deletes the lines rages from 1 to 3 and other lines are printed
sed '1,3d' <filename>
#Delete entries with specified word
sed '/word/d' <filename>
Shell Scripting :
A shell script is a computer program designed to be run by a Unix shell, a command line interpreter.
It is called a shell script because it combines a sequence of commands, that would otherwise have to be typed into the keyboard one at a time, into a single script.
The various dialects of shell scripts are considered to be scripting languages.
A shell script is usually created for command sequences that a user need to use repeatedly to save time.
Like other programs, the shell script can contain parameters, comments and subcommands that the shell must follow.
Users initiate the sequence of commands in the shell script by simply entering the file name on a command line.
Shabang :
On Linux, a shabang (#!) is a special line at the beginning of a script that tells the operating system which interpreter to use when executing the script.
Shabang line is important because it allows you to run scripts written in any language.
#!/bin/bash
Declaring the variable :
Temporarily :
SYNTAX : <variable> name>=data/value
Permanently :
SYNTAX : vim /etc/bashrc
<variable> name>=<content>
Creating the script in Linux :
- vim linux.sh
To run the script :
- bash linux.sh
IF Else Statement :
1) simple if :
SYNTAX :
#Syntax
if [expression]
then
statement
fi
#Example
if [ ${A} -gt ${B} ]
then
echo "${A} is greater than ${B}"
fi
2) IF Else :
SYNTAX :
#Syntax
if [expression]
then
statement1
else
statement2
fi
#Example
if [ ${A} -gt ${B} ]
then
echo "${A} is greater than ${B}"
else
echo "${B} is greater than ${A}"
fi
3) Else if ladder :
SYNTAX :
#Syntax
if [expression1]
then
Statement1
elif [expression2]
then
Statement2
elif [expression3]
then
Statement3
else
Statement4
fi
#Example
#!/bin/bash
echo "Enter your age"
read age
if [ ${age} -gt 0 -a ${age} -lt 15 ]
then
echo "You are a kid"
elif [ ${age} -gt 15 -a ${age} -lt 25 ]
then
echo "You are a teenager"
elif [ ${age} -gt 25 -a ${age} -lt 50 ]
then
echo "You are an adult"
elif [ ${age} -gt 50 -a ${age} -lt 100 ]
then
echo "You are a Senior citizen"
else
echo "You are dead or Please enter the valid age"
fi
4) Nested if :
SYNTAX :
#Syntax
if [expression]
then
Statement
if [expression]
then
Statement
else
Statement
fi
else
Statement
fi
#Example
#!/bin/bash
echo "Enter your first name"
read firstname
echo "Enter your Surname"
read surname
if [ ${firstname} == Sarthak ]
then
echo "First name is matched"
if [ ${surname} == Shinde ]
then
echo "First name and Surname both matched"
else
echo "First name matched but surname mismatched"
fi
else
echo "First name doesn't matched stopped execution"
fi
These were the topics covered by Master Pranav Jambare in the seventh session of Linux. Thankyou for reading my blog !!



