Powered By Blogger

Aug 6, 2024

Operating System Lab Practical-05

 

Practical-5

Title : Shell Script programming using grep, awk, sed


____________________________________________________________________


Shell Script programming using grep :

Suppose you have a directory structure like this:


/home/cse40/lab5


|-- file1.txt

|-- file2.txt

|-- lab5subdir

    |-- file3.txt


And the content of the files are:

- `file1.txt`: "Hello world"

- `file2.txt`: "This is a test"

- `file3.txt`: "Hello again"


Now, if you want to search for the word "Hello" in the `/lab5`, you can use the script as follows:

$touch grep_script.sh


#!/bin/bash

# Check if the correct number of arguments is provided

if [ "$#" -ne 2 ]; then

    echo "Usage: $0 <pattern> <directory>"

    exit 1

fi

pattern=$1

directory=$2

# Search for the pattern in the specified directory and its subdirectories

grep -r "$pattern" "$directory"


1. Save the script in a file named `grep_script.sh`.

2. Make the script executable: $chmod +x grep_script.sh

3. Run the script with the pattern "Hello" and the directory `/lab5`:

      ./grep_script.sh "Hello" /home/cse40/lab5

   

Expected Output:

/home/cse40/lab5/file1.txt:Hello world

/home/cse40/lab5/lab5subdir/file3.txt:Hello again


This output shows the lines containing the word "Hello" along with the file names where the pattern was found.


Shell Script programming using awk :

 

Awk is a scripting language used for manipulating data and generating reports. The awk command programming language requires no compiling and allows the user to use variables, numeric functions, string functions, and logical operators.


Here is a simple `awk` script example to extract and print specific columns from a file:

$touch awk_script.sh

$nano awk_script.sh


#!/bin/bash

# Check if the correct number of arguments is provided

if [ "$#" -ne 3 ]; then

    echo "Usage: $0 <column1> <column2> <file>"

    exit 1

fi

column1=$1

column2=$2

file=$3

# Use awk to print the specified columns

awk -v col1="$column1" -v col2="$column2" '{print $col1, $col2}' "$file"


Suppose you have a file `data.txt` with the following content:

1 Raj 25

2 Sunil 30

3 Vinit 22

4 Rahul 28


If you want to extract and print the first and third columns (ID and age), you can use the script as follows:


1.    Save the script as `awk_script.sh`.

2. Make the script executable: $chmod +x awk_script.sh

3. Create the `data.txt` file:

      echo -e "1 Raj 25\n2 Sunil 30\n3 Vinit 22\n4 Rahul 28" > data.txt

4. Run the script:  ./awk_script.sh 1 3 data.txt

   

Expected Output:

1 25

2 30

3 22

4 28

This output shows the first and third columns of each line from the `data.txt` file. 




Shell Script programming using sed :


SED command in UNIX stands for stream editor and it can perform lots of functions on file like searching, find and replace, insertion or deletion. Though most common use of SED command in UNIX is for substitution or for find and replace. By using SED you can edit files even without opening them, which is much quicker way to find and replace something in file, than first opening that file in VI Editor and then changing it.

  • SED is a powerful text stream editor. Can do insertion, deletion, search and replace(substitution).

  • SED command in unix supports regular expression which allows it perform complex pattern matching.



$touch awk_script.sh

$nano awk_script.sh



#!/bin/bash

# Check if the correct number of arguments is provided

if [ "$#" -ne 3 ]; then

    echo "Usage: $0 <pattern> <replacement> <file>"

    exit 1

fi

pattern=$1

replacement=$2

file=$3

# Use sed to replace the pattern with the replacement string in the specified file

sed -i "s/$pattern/$replacement/g" "$file"



Suppose you have a file `example.txt` with the following content:

Hello world

This is a test

Hello again


If you want to replace the word "Hello" with "Hi", you can use the script as follows:


1. Save the script as “sed_script.sh”.

2.Make the script executable:$ chmod +x sed_script.sh

3. Create the `example.txt` file: 

    echo -e "Hello world\nThis is a test\nHello again" > example.txt

4. Run the script:

   ./sed_script.sh "Hello" "Hi" example.txt

   

Expected Output in “example.txt”:


Hi world

This is a test

Hi again


This script replaces all occurrences of the word "Hello" with "Hi" in the `example.txt` file.



No comments:

Post a Comment

Featured Post

Data Analysis

    What is data analysis and its significance?   Data analysis is the process of collecting, transforming, and organizing data to dr...

Popular Posts