GREP
The g/RE/p stands for globally search for the regular expression (RE) and print
out the line. The grep command in Unix/Linux is a powerful tool used for searching and
manipulating text patterns within files. Its name is derived from the ed (editor) command
g/re/p (globally search for a regular expression and print matching lines), which reflects its
core functionality. Grep is widely used by programmers, system administrators, and users
alike for its efficiency and versatility in handling text data.
Syntax of grep Command in Unix/Linux:
The basic syntax of the `grep` command is as follows:
                        grep [options] pattern [files]
Here,
[options]: These are command-line flags that modify the behaviour of grep.
[pattern]: This is the regular expression you want to search for.
[file]: This is the name of the file(s) you want to search within. You can specify multiple files
for simultaneous searching.
Options Available in grep Command:
Options                             Description
                                    This prints only a count of the lines that match a
-c                                  pattern
                                    Display the matched lines, but do not display the
-h                                  filenames.
–i                                  Ignores, case for matching
-n                                  Display the matched lines and their line numbers.
                                    This prints out all the lines that do not matches the
-v                                  pattern
Example of grep Command in Linux
1. Case insensitive search
       The -i option enables to search for a string case insensitively in the given file. It
matches the words like “UNIX”, “Unix”, “unix”.
       grep -i "UNix" geekfile.txt
Output:
2. Displaying the Count of Number of Matches Using grep
       We can find the number of lines that matches the given string/pattern.
       grep -c "unix" geekfile.txt
Output:
3. Checking for the Whole Words in a File Using grep
        By default, grep matches the given string/pattern even if it is found as a substring in a
file. The -w option to grep makes it match only the whole words.
        grep -w "unix" geekfile.txt
Output:
Conclusion
        The grep command in Linux which is a powerful text-search tool that uses regular
expressions to find patterns or text within files. It offers various options like case
insensitivity, counting matches, and listing file names.
LOCAL VARIABLE AND ITS SCOPE:
        There are three main types of variables are present in shell scripting. They are – Shell
Variable, Global Variable, and Local Variable.
        Shell variables are special types of variables. They are created and maintained by
Linux Shell itself. These variables are required by the shell to function properly.
        A global variable is a variable with global scope. It is accessible throughout the
program. Global variables are declared outside any block of code or function.
Local Variable
        A local variable is a special type of variable which has its scope only within a specific
function or block of code. Local variables can override the same variable name in the larger
scope.
Example 1: Limited Scope
        This example depicts that a local variable written inside a function has its scope
limited only within the function.
Shell Script:
        xyz@localhost~/desktop: $ cat local.sh
       #! /bin/sh
       getNUM(){
          NUM=100 #local variable
          echo "$NUM - inside function"
       }
      echo "$NUM - outside function"
      getNUM
Output:
      xyz@localhost~/desktop: $ sh local.sh
      - outside function
      100 - inside function
      xyz@localhost~/desktop: $
      In this example, NUM is a local variable because it is within the getNUM() function.
When we are accessing the value of it from the function itself, then we are getting it, but
when we are trying to access the same from outside the function, it is not visible.
Example 2: Overrides Global Variable
       This example depicts that a local variable can override the global variable in its scope.
Shell Script:
       xyz@localhost~/desktop: $ cat local1.sh
       #!/bin/sh
       NUM=200 #global variable
       getNUM(){
          NUM=100 #local variable
          echo "$NUM - inside function"
       }
       echo "$NUM - outside function"
       getNUM
Output:
       xyz@localhost~/desktop: $ sh local.sh
       200 - outside function
       100 - inside function
       xyz@localhost~/desktop: $
       Here, there is a global variable and a local variable within the getNUM() function
having the same name. The local variable within its scope i.e. the getNUM() function,
override it but outside its scope, the global variable is accessed.
Example 3: Recursion and Local Variable
       Local variables make recursion possible. Each function call in recursion creates its
own set of local variables.
       This example depicts a shell program to print n to 0 using recursion.
Shell Script:
       xyz@localhost~/desktop: $ cat local.sh
       #!/bin/sh
       printNUM(){
          if [ $1 -lt 0 ];then
          exit
          fi
          echo $1
          printNUM $(($1-1))
       }
       printNUM 5
Output:
       xyz@localhost~/desktop: $ sh local.sh
       5
       4
       3
       2
       1
       0
       xyz@localhost~/desktop: $
       Here, positional argument $1 is acting as a local variable, making the recursion
possible.
TEXT FILTERING TOOLS
       Filters commands in Linux are implemented to filter out some text or text in a file by
providing the input as stdin (standard input) and having the output as stdout (standard
output).
Head command
         It is the complementary of Tail command. The head command, as the name implies,
print the top N number of data of the given input. By default, it prints the first 10 lines of the
specified files. If more than one file name is provided then data from each file is preceded by
its file name.
Syntax:
head [OPTION]... [FILE]...
Options
Example
        Let us consider two files having name state.txt and capital.txt contains all the names
of the Indian states and capitals respectively.
$ cat state.txt
Andhra Pradesh
Arunachal Pradesh
Assam
Bihar
Chhattisgarh
Goa
Gujarat
Haryana
Himachal Pradesh
Jammu and Kashmir
Jharkhand
Karnataka
Kerala
Madhya Pradesh
Maharashtra
Manipur
Meghalaya
Mizoram
Nagaland
Odisha
Punjab
Rajasthan
Sikkim
Tamil Nadu
Telangana
Tripura
Uttar Pradesh
Uttarakhand
West Bengal
$ cat capital.txt
Hyderabad
Itanagar
Dispur
Patna
Raipur
Panaji
Gandhinagar
Chandigarh
Shimla
Srinagar
Without any option, it displays only the first 10 lines of the file specified.
$ head state.txt
Andhra Pradesh
Arunachal Pradesh
Assam
Bihar
Chhattisgarh
Goa
Gujarat
Haryana
Himachal Pradesh
Jammu and Kashmir
Tail command
         It is the complementary of head command. The tail command, as the name implies,
prints the last N number of data of the given input. By default, it prints the last 10 lines of the
specified files. If more than one file name is provided then data from each file is preceded by
its file name.
Syntax of Tail Command in Linux
tail [OPTION]... [FILE]...
Example:
tail state.txt
Here we will only get names of last 10 states after using tail command.
DIFF COMMAND
        diff stands for difference. The diff command is a versatile utility that is pre-installed
on most Linux distributions. Its primary purpose is to compare the contents of two files and
display the differences between them. The command provides a comprehensive way to
highlight changes, additions, and deletions in a clear and readable format. This command is
used to display the differences in the files by comparing the files line by line.
Basic Syntax of diff Command
        The basic syntax of the diff command is as follows:
diff [OPTION]... FILE1 FILE2
Here, `FILE1` and `FILE2` are the two files you want to compare.
The `OPTION` flag allows you to customize the behavior of the `diff` command.
Comparing Two Files
Compare files line by line in Linux.
Lets say we have two files with names a.txt and b.txt containing 5 Indian states.
cat a.txt
cat b.txt
Now, applying diff command without any option we get the following output:
diff a.txt b.txt
CUT COMMAND
The cut command in linux is a command for cutting out the sections from each line of files
and writing the result to standard output. It can be used to cut parts of a line by byte position,
character, and field. The cut command slices a line and extracts the text. It is necessary to
specify an option with a command otherwise it gives an error. If more than one file name is
provided then data from each file is not preceded by its file name.
Syntax of cut Command
The basic syntax of the cut command is:
cut OPTION... [FILE]...
Let us consider two files having name state.txt and capital.txt contains 5 names of the Indian
states and capitals respectively.
$ cat state.txt
Andhra Pradesh
Arunachal Pradesh
Assam
Bihar
Chhattisgarh
PASTE COMMAND
Paste command is one of the useful commands in Unix or Linux operating system. It is used
to join files horizontally (parallel merging) by outputting lines consisting of lines from each
file specified, separated by tab as delimiter, to the standard output. When no file is specified,
or put dash (“-“) instead of file name, paste reads from standard input and gives output as it is
until a interrupt command [Ctrl-c] is given.
Syntax:
paste [OPTION]... [FILES]...
Let us consider three files having name state, capital and number. state and capital file
contains 5 names of the Indian states and capitals respectively. number file contains 5
numbers.
$ cat state
Arunachal Pradesh
Assam
Andhra Pradesh
Bihar
Chhattisgrah
$ cat capital
Itanagar
Dispur
Hyderabad
Patna
Raipur
        $ paste number state capital
        1    Arunachal Pradesh       Itanagar
        2    Assam Dispur
        3    Andhra Pradesh Hyderabad
        4    Bihar Patna
        5    Chhattisgrah Raipur
JOIN COMMAND
The ‘join’ command is used to merge lines from two files based on a key field that is present
in both files. This key can be any column of data, separated by whitespace or other
delimiters. The default behavior of ‘join’ is to use the first field from each file as the key for
joining.
Syntax:
$join [OPTION] FILE1 FILE2
Join Command Example in Linux
Let us assume there are two files ‘file1.txt’ and ‘file2.txt’ and we want to combine the
contents of these two files.
Displaying the contents of first file:
        $cat file1.txt
        1 AAYUSH
        2 APAAR
        3 HEMANT
        4 KARTIK
Displaying contents of second file:
        $cat file2.txt
        1 101
        2 102
        3 103
        4 104
Using join command:
        $join file1.txt file2.txt
        1 AAYUSH 101
        2 APAAR 102
        3 HEMANT 103
        4 KARTIK 104
UNIQ COMMAND
       The uniq command in Linux is a command-line utility that reports or filters out the
repeated lines in a file. In simple words, uniq is the tool that helps to detect the adjacent
duplicate lines and also deletes the duplicate lines. uniq filters out the adjacent matching lines
from the input file(that is required as an argument) and writes the filtered data to the output
file.
Syntax of uniq Command
The basic syntax of the `uniq` command is:
uniq [OPTIONS] [INPUT_FILE [OUTPUT_FILE]]
EXAMPLE
       $cat kt.txt
       I love music.
       I love music.
       I love music.
       I love music of Kartik.
       I love music of Kartik.
       Thanks.
$ uniq kt.txt
COMM COMMAND
The ‘comm’ command is used for line-by-line comparison of two sorted files. It reads two
files as input and generates a three-column output by default:
   •   Column 1: Lines unique to the first file.
   •   Column 2: Lines unique to the second file.
   •   Column 3: Lines common to both files.
Syntax:
$comm [OPTION]... FILE1 FILE2
Example of the ‘comm’ Command
Let us suppose there are two sorted files file1.txt and file2.txt and now we will use comm
command to compare these two.
Displaying contents of file1
       $cat file1.txt
       Apaar
       Ayush Rajput
       Deepak
       Hemant
       Displaying contents of file2
       $cat file2.txt
       Apaar
       Hemant
       Lucky
       Pranjal Thakral
       Now, run comm command as:
       $comm file1.txt file2.txt
       Output:
                 Apaar
       Ayush Rajput
       Deepak
                 Hemant
            Lucky
            Pranjal Thakral
TR COMMAND
The tr command is a UNIX command-line utility for translating or deleting characters. It
supports a range of transformations including uppercase to lowercase, squeezing repeating
characters, deleting specific characters, and basic find and replace. It can be used with UNIX
pipes to support more complex translation. tr stands for translate.
Syntax :
$ tr [OPTION] SET1 [SET2]
How to convert lower case characters to upper case. To convert characters from lower
case to upper case, you can either specify a range of characters or use the predefined
character classes.
       $ cat greekfile
Output:
       WELCOME TO
       GeeksforGeeks
       $ cat greekfile | tr [a-z] [A-Z]
       Output:
       WELCOME TO
       GEEKSFORGEEKS
Wc Command In Linux With Examples
wc stands for word count. As the name implies, it is mainly used for counting purpose.
   •    It is used to find out number of lines, word count, byte and characters count in the
        files specified in the file arguments.
    • By default it displays four-columnar output.
    • First column shows number of lines present in a file specified, second column shows
        number of words present in the file, third column shows number of characters present
        in file and fourth column itself is the file name which are given as argument.
Syntax:
wc [OPTION]... [FILE]...
        Let us consider two files having name state.txt and capital.txt containing 5 names of
the Indian states and capitals respectively.
        $ cat state.txt
        Andhra Pradesh
        Arunachal Pradesh
        Assam
        Bihar
        Chhattisgarh
       $ cat capital.txt
       Hyderabad
       Itanagar
       Dispur
       Patna
       Raipur
Passing only one file name in the argument.
       $ wc state.txt
        5 7 58 state.txt
       $ wc capital.txt
        5 5 39 capital.txt
Passing more than one file name in the argument.
       $ wc state.txt capital.txt
         5 7 58 state.txt
         5 5 39 capital.txt
        10 12 97 total
       When more than file name is specified in argument then command will display four-
columnar output for all individual files plus one extra row displaying total number of lines,
words and characters of all the files specified in argument, followed by keyword total.
Options: 1. -l: This option prints the number of lines present in a file. With this option wc
command displays two-columnar output, 1st column shows number of lines present in a file
and 2nd itself represent the file name.
       With one file name
       $ wc -l state.txt
       5 state.txt
       With more than one file name
       $ wc -l state.txt capital.txt
         5 state.txt
         5 capital.txt
        10 total
2. -w: This option prints the number of words present in a file. With this option wc
command displays two-columnar output, 1st column shows number of words present in a file
and 2nd is the file name.
        With one file name
        $ wc -w state.txt
        7 state.txt
       With more than one file name
       $ wc -w state.txt capital.txt
         7 state.txt
         5 capital.txt
        12 total
3. -c: This option displays count of bytes present in a file. With this option it display two-
columnar output, 1st column shows number of bytes present in a file and 2nd is the file name.
        With one file name
        $ wc -c state.txt
        58 state.txt
       With more than one file name
       $ wc -c state.txt capital.txt
        58 state.txt
        39 capital.txt
        97 total
4. -m: Using -m option ‘wc’ command displays count of characters from a file.
       With one file name
       $ wc -m state.txt
       56 state.txt
       With more than one file name
       $ wc -m state.txt capital.txt
        58 state.txt
        39 capital.txt
        97 total
6. –version: This option is used to display the version of wc which is currently running on
your system.
       $ wc --version
       wc (GNU coreutils) 8.26
       Packaged by Cygwin (8.26-1)
       Copyright (C) 2016 Free Software Foundation, Inc.
SORT command is used to sort a file, arranging the records in a particular order. By default,
the sort command sorts file assuming the contents are ASCII. Using options in the sort
command can also be used to sort numerically.
    • SORT command sorts the contents of a text file, line by line.
    • sort is a standard command-line program that prints the lines of its input or
        concatenation of all files listed in its argument list in sorted order.
   •     The sort command is a command-line utility for sorting lines of text files. It supports
         sorting alphabetically, in reverse order, by number, by month, and can also remove
         duplicates.
    • The sort command can also sort by items not at the beginning of the line, ignore case
         sensitivity, and return whether a file is sorted or not. Sorting is done based on one or
         more sort keys extracted from each line of input.
    • By default, the entire input is taken as the sort key. Blank space is the default field
         separator.
The sort command follows these features as stated below:
    1. Lines starting with a number will appear before lines starting with a letter.
    2. Lines starting with a letter that appears earlier in the alphabet will appear before lines
         starting with a letter that appears later in the alphabet.
    3. Lines starting with a uppercase letter will appear before lines starting with the same
         letter in lowercase.
Syntax of sort Command in Linux
Here,
‘options‘ refer to the various flags and parameters that can be used to customize the sorting
behavior, and ‘file’ is the name of the file to be sorted.
If no file is specified, ‘sort‘reads from the standard input.
How to Sort Lines in Text Files in Linux Using sort Command
Let’s start with a simple example. Consider a file named ‘file.txt’ with the following content:
cat file.txt
Here we used cat command to display the content inside the file name ‘file.txt’.
To sort the lines alphabetically, you can use the following command:
sort file.txt
How to Sort Lines in Text Files with Uppercase and Lowercase Using sort Command
       Sort function with mix file i.e. uppercase and lower case: When we have a mix file
with both uppercase and lowercase letters then first the upper case letters would be sorted
following with the lower case letters.
Example: If we have a text file which has both Uppercase and lowercase characters.
cat mix.txt
Here we used cat command to display the content inside the file name ‘mix.txt’.
To sort the lines alphabetically which contain uppercase and lowercase letters, you can use
the following command:
sort mix.txt
How to do Numeric Sorting in Lines in Text Files Using sort Command
         By default, ‘sort’ treats numbers as text and sorts them accordingly. If you want to
perform numeric sorting, use the ‘-n’ option:
cat file1.txt
         Here we used cat command to display the content inside the file name ‘file1.txt’.
Now we will sort the numeric data using sort command as follows.
sort file1.txt
How to Sort Lines in Reverse Order in Linux File Using sort Command
        To sort in reverse order, you can use the ‘-r’ option:
Example: If we want to sort lines in reverse order in Linux file we can use sort command
with -r option, suppose we have a file name “example.txt” as shown below.
cat example.txt
         Now sorting lines in reverse order using `-r` option in sort command
sort -r example.txt
How to sort Specific Fileds in Linux Files Using sort Command
       Imagine you have a file named ’employee_data.txt’ that contains information about
employees in a tab-separated format. Each line represents a record with details such as
employee ID, name, department, and salary, separated by tabs. You want to organize this data
based on the department and display the results in alphabetical order.
       For Example: If our file name is “employee_data.txt” and content inside it is as
follows.
cat employee_data.txt
        To achieve this, you can use the ‘sort’ command
sort -k3 employee_data.txt
Explanation:
        -k3: This option indicates that the sorting should be done based on the third column
(Department).
In this example, the employee records are now sorted alphabetically based on the
‘Department’ column. The ‘sort’ command, with the custom delimiter, allows you to
efficiently organize and analyze tab-separated data, making it a valuable tool for managing
structured information in various scenarios.
Array Basics in Shell Scripting
Understanding Arrays in Shell Scripting
        An array is a structured arrangement of similar data elements. Within shell scripting,
an array is a variable that holds multiple values, whether they are of the same type or
different types. It’s important to note that in shell scripting, everything is treated as a string.
Arrays adhere to a zero-based index, which signifies that indexing initiates from 0.
How to Declare Array in Shell Scripting?
        Arrays can be declared in a shell script using various approaches:
1. Indirect Declaration
        In this method, you assign a value to a specific index of the array variable. There’s no
need to declare the array beforehand.
ARRAYNAME[INDEXNR]=value
2. Explicit Declaration
        With explicit declaration, you first declare the array and then assign values to it.
declare -a ARRAYNAME
3. Compound Assignment
        This method involves declaring the array along with its initial set of values. You can
later add additional values to the array.
ARRAYNAME=(value1 value2 .... valueN)
Alternatively, you can use index numbers to assign values explicitly:
ARRAYNAME=([1]=10 [2]=20 [3]=30)
Printing Array Values in Shell Script:
To display array elements, you have several options:
Here is a `array_test.sh`script explaining multiple options. (You can create script with
any name)
#!/bin/bash
# To declare a static Array
arr=(“Jayesh” “Shivang” “1” “Vipul” “Nishant” “2”)
# To print all elements of the array
echo “All elements of the array:”
echo “${arr[@]}”
echo “${arr[*]}”
# To print the first element
echo “The first element:”
echo “${arr[0]}”
# To print a selected index element
selected_index=3
echo “Selected index element at index $selected_index:”
echo “${arr[$selected_index]}”
# To print elements from a particular index
echo “Elements from a particular index:”
echo “${arr[@]:2}” # Prints elements starting from index 2
echo “${arr[*]:2}” # Prints elements starting from index 2
# To print elements in a range
echo “Elements in a range:”
echo “${arr[@]:1:3}” # Prints elements from index 1 to 3
echo “${arr[*]:1:3}” # Prints elements from index 1 to 3
Explanation:
    1. Array Declaration: An array named arr is declared, containing six elements. These
        elements are strings: “prakhar”, “ankit”, “1”, “rishabh”, “manish”, and “abhinav”.
    2. Printing All Elements:
            • echo "All elements of the array:": A message is printed to indicate that all
                elements of the array are being displayed.
            • ${arr[@]}: This syntax is used to print each element of the array separately. It
                displays all elements in the array.
            • ${arr[*]}: Similar to the previous line, this syntax prints all elements of the
                array as a single string.
    3. Printing the First Element:
            • echo "The first element:": A message is printed to indicate that the first
                element of the array is being displayed.
            • ${arr[0]}: This syntax retrieves and displays the first element of the array. In
                Bash, array indexing starts at 0.
    4. Printing a Selected Index Element:
            • selected_index=3: A variable named selected_index is assigned the value 3.
                This variable represents the desired index in the array.
            • echo "Selected index element at index $selected_index:": A message is printed
                to indicate the selected index.
            • ${arr[$selected_index]}: Using the value stored in selected_index, this syntax
                retrieves and displays the element at the specified index (index 3 in this case).
    5. Printing Elements from a Particular Index:
            • echo "Elements from a particular index:": A message is printed to indicate that
                elements from a specific index are being displayed.
            • ${arr[@]:2}: This syntax extracts and displays all elements starting from
                index 2 in the array. It prints each element separately.
            • ${arr[*]:2}: Similar to the previous line, this syntax displays the elements as a
                single string, starting from index 2.
    6. Printing Elements in a Range:
            • echo "Elements in a range:": A message is printed to indicate that elements
                within a specified range are being displayed.
          •   ${arr[@]:1:3}: This syntax extracts and displays elements starting from index
              1 up to index 3 (inclusive). It prints each element separately.
          •   ${arr[*]:1:3}: Similar to the previous line, this syntax displays the extracted
              elements as a single string.
Here is a `array_test2.sh` script with few other examples. (you can create script with
any name)
#!/bin/bash
# Declare a static Array
arr=(“Jayesh” “Shivang” “1” “rishabh” “Vipul” “Nishtan”)
# Count the length of a particular element in the array
element_length=${#arr[2]}
echo “Length of element at index 2: $element_length”
# Count the length of the entire array
array_length=${#arr[@]}
echo “Length of the array: $array_length”
# Search in the array
search_result=$(echo “${arr[@]}” | grep -c “Jayesh”)
echo “Search result for ‘Jayesh’: $search_result”
# Search and replace in the array
replaced_element=$(echo “${arr[@]/Shivang/SHIVANG}”)
echo “Array after search & replace: ${replaced_element[*]}”
# Delete an element in the array (index 3)
unset arr[3]
echo “Array after deletion: ${arr[*]}”
Explanation:
    • Array Declaration:
           o An array named arr is declared, containing six elements.
           o These elements are strings: “Jayesh”, “Shivang”, “1”, “rishabh”, “Vipul”, and
               “Nishtan”.
    • Printing Element Length:
           o The length of the element at index 2 of the array is calculated
               using ${#arr[2]}.
           o  This length is stored in the variable element_length.
   •   Printing Array Length:
          o The length of the entire array is calculated using ${#arr[@]}.
          o This length is stored in the variable array_length.
   •   Searching in the Array:
          o The grep command is used to search for occurrences of the string “Jayesh” in
              the array ${arr[@]}.
          o The -c flag is used to count the number of occurrences.
          o The count is stored in the variable search_result.
   •   Searching and Replacing in the Array:
          o A search and replace operation is performed on the array ${arr[@]}.
          o The string “Shivang” is replaced with “SHIVANG”.
          o The updated array is stored in the variable replaced_element.
   •   Deleting an Element from the Array:
          o The unset command is used to delete the element at index 3 (which is
              “rishabh”) from the array ${arr[@]}.
   •   Printing All Elements:
          o The message “All elements of the array:” is echoed.
          o ${arr[@]} syntax is used to print each element of the array separately. This
              displays all elements in the array.
Conclusion
In this article we discussed about utility of arrays in shell scripting. It underlines how arrays
efficiently handle a multitude of values, replacing numerous variables. The concept of array
declaration is explained through three methods: indirect, explicit, and compound assignment.
The article further illustrates printing array elements using practical script examples. This
discussion not only demystifies array usage but also underscores its significance in managing
and manipulating data within shell scripts.