Linux and Shell Programming

0 of 30 lessons complete (0%)

Module 6 Introduction to Shell

Program 2: Pattern Matching

Step 1: Create a File

For Patterns, a file is needed with text, so create a file in the same folder where you are creating your script.

Filename: file1

India extended their winning streak with a commanding eight-wicket victory over Afghanistan in the World Cup on Wednesday.

In pursuit of a target of 273 runs, Rohit Sharma unleashed a barrage of sixes, smashing a blistering 131 off just 84 balls. This remarkable innings not only secured India's victory but also etched Sharma's name in the history books as he became the player with the most centuries in World Cup history. Alongside Sharma, Ishan Kishan (47), Virat Kohli (55 not out), and Shreyas Iyer (25 not out) made valuable contributions to ensure a comfortable win for the hosts, reaching the target in just 35 overs.

The opening partnership between Sharma and Kishan was a pivotal moment in the match, as their 156-run stand laid a solid foundation for India's successful chase.

Step 2: Write your script

Filename: p1.sh

$vi p1.sh
#!/bin/sh
echo "Enter the pattern to be searched: "
read pname
echo "Enter the file to be used: "
read fname
echo "Searching for $pname in $fname "
grep $pname $fname
echo "Selected lines are shown above"

Step 3: Run your Script

//Give permission to execute file
$chmod 744 p1.sh

//verify permission    
$ls -l p1.sh       

//Run Your Script
$./p1.sh

Step 4: Output

Enter the Pattern to be searched: India
Enter the file to be used: file1
Searching India in file1
<Results will be displayed>

Selected lines are shown above

Hurray you have done it…