Prasanna

pageicon Sunday Mar 16, 2008

Shell script: Matching the first occurrence of a string or a pattern in a file and exit

I was looking to solve the above problem, as lazy as ever googled for a solution, but couldn't find what I was looking for (though there were while loop solutions). If you need a single line shell script which matches the first occurrence of a string (or a pattern) from the input and exit, the following code could be useful.

Input file: input.txt

Start
Line: 1
Line: 2
Line: 3
Line: 4
Line: 5
End

To get output 1 (second string in second line Line: 1) from the above file and exit, use the below grep and awk combination, although this may not be the efficient solution.

cat input.txt | grep "Line:" | awk '{ print $2; exit }'
pageicon Wednesday Mar 05, 2008

Dhoni: The inspirational captain

Finally India won something in Australia thanks to the young and energetic side led by MS Dhoni, as before every series there were lot of critics questioning the controversial sacking of former Indian captains Rahul Dravid and Sourav Ganguly, but Dhoni's men vindicated everything by lifting the trophy against all odds. The pensive mood of the Australian camp says it all!

There were lots of gains for India from this series which would do a world of good for the team, the greatest finds being the talented youngster Rohit Sharma, rookie (can we call them anymore?) pacers Ishant Sharma and Pravin Kumar, more than anything is the maturing captain in MS Dhoni. What more inspired by youngsters, sachin started to play as if he is still 17 years old, giving India an explosive start only he is capable of. These things coupled with the fact that the Under 19 team lifted the World Cup means Indian Cricket is really in Cloud Nine.

Now it remains to be seen whether the former captains would ever get a recall in the ODI squad as players (BTW there were lot of Under 19 players in queue as well), therefore the best thing for them is to learn the fact that their only stronghold is their test place, which nobody can replace for the time being (atleast as long our test captain is Anil Kumble :)).