Unix Shell - finding line no of a STRING in a text file using shell script.

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > January 2006 > finding line no of a STRING in a text file using shell script.





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author finding line no of a STRING in a text file using shell script.
Dattu

2006-01-20, 2:50 am

Hi Good morning to All,
I have the following requirement. Pl, Help me in solving this
problem.
The requirement is

I have to text file with 10 lines.

Ex :

[

Smoking, eatables will not be allowed inside the facility.
Mobile phones are not to be carried inside the facility.
Membership cards to be carried always and displayed on request by the
Gym admin staff.
Personal guests will not be allowed inside the Gym.
The music system will be handled by the Gym staff only. Personal
request will not be entertained.
On completion of workout, users are requested to replace the equipments
viz stability balls, medicine balls, dumbbells, plates, bars, in the
place allocated.
Any user misusing the Gym/ Gym equipment or not following any of the
above rules and regulations will have his/her membership terminated
from the Gym.
Any damage caused to the gym equipment will have to be reported
promptly to the Gym instructor/Admin staff. Damage to equipment caused
by willful negligence will incur costs being recovered from the member
responsible.
The above rules and regulations have been formulated to ensure your
safety, to protect the equipment from damage and to ensure optimum
functioning of the gym.
Keeping in mind the safety, employees are requested to intimate their
medical history if any and consult with the gym instructor for
instructions pertaining to workouts/exercises.

]

In the above text file How to find the line no that the string
"Membership" occurs.


Thanks in Advance,
Dattu

Chris F.A. Johnson

2006-01-20, 2:50 am

On 2006-01-20, Dattu wrote:
> Hi Good morning to All,
> I have the following requirement. Pl, Help me in solving this
> problem.
> The requirement is
>
> I have to text file with 10 lines.
>
> Ex :
>
> [
>
> Smoking, eatables will not be allowed inside the facility.
> Mobile phones are not to be carried inside the facility.
> Membership cards to be carried always and displayed on request by the
> Gym admin staff.
> Personal guests will not be allowed inside the Gym.
> The music system will be handled by the Gym staff only. Personal
> request will not be entertained.
> On completion of workout, users are requested to replace the equipments
> viz stability balls, medicine balls, dumbbells, plates, bars, in the
> place allocated.
> Any user misusing the Gym/ Gym equipment or not following any of the
> above rules and regulations will have his/her membership terminated
> from the Gym.
> Any damage caused to the gym equipment will have to be reported
> promptly to the Gym instructor/Admin staff. Damage to equipment caused
> by willful negligence will incur costs being recovered from the member
> responsible.
> The above rules and regulations have been formulated to ensure your
> safety, to protect the equipment from damage and to ensure optimum
> functioning of the gym.
> Keeping in mind the safety, employees are requested to intimate their
> medical history if any and consult with the gym instructor for
> instructions pertaining to workouts/exercises.
>
>]
>
> In the above text file How to find the line no that the string
> "Membership" occurs.


grep -n "Membership" FILENAME

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
romy

2006-01-20, 2:50 am


can do this also :

awk '/Membership/ { print NR }' filename

Stephane CHAZELAS

2006-01-20, 2:50 am

2006-01-19, 21:06(-08), Dattu:
[...]
> In the above text file How to find the line no that the string
> "Membership" occurs.

[...]

sed -n /membership/=

Or if you want to stop at the first matching line:

sed -n '/membership/{=;q;}'

--
Stéphane
Vinod

2006-01-20, 8:13 am

Hi,

You can achieve with the below oiptions:

cat -n <filename>|grep "Membership"

This will give you the line number and the line text.

Below is the syntax to get only the line number:
cat -n <filename>|grep "Membership"|awk '{print $1}'

Regards,
vinod.


Dattu wrote:
> Hi Good morning to All,
> I have the following requirement. Pl, Help me in solving this
> problem.
> The requirement is
>
> I have to text file with 10 lines.
>
> Ex :
>
> [
>
> Smoking, eatables will not be allowed inside the facility.
> Mobile phones are not to be carried inside the facility.
> Membership cards to be carried always and displayed on request by the
> Gym admin staff.
> Personal guests will not be allowed inside the Gym.
> The music system will be handled by the Gym staff only. Personal
> request will not be entertained.
> On completion of workout, users are requested to replace the equipments
> viz stability balls, medicine balls, dumbbells, plates, bars, in the
> place allocated.
> Any user misusing the Gym/ Gym equipment or not following any of the
> above rules and regulations will have his/her membership terminated
> from the Gym.
> Any damage caused to the gym equipment will have to be reported
> promptly to the Gym instructor/Admin staff. Damage to equipment caused
> by willful negligence will incur costs being recovered from the member
> responsible.
> The above rules and regulations have been formulated to ensure your
> safety, to protect the equipment from damage and to ensure optimum
> functioning of the gym.
> Keeping in mind the safety, employees are requested to intimate their
> medical history if any and consult with the gym instructor for
> instructions pertaining to workouts/exercises.
>
> ]
>
> In the above text file How to find the line no that the string
> "Membership" occurs.
>
>
> Thanks in Advance,
> Dattu


Ed Morton

2006-01-20, 6:03 pm

Vinod wrote:

> Hi,


Please don't top-post. See http://en.wikipedia.org/wiki/Top-posting.

> You can achieve with the below oiptions:
>
> cat -n <filename>|grep "Membership"


UUOC:

grep -n "Membership" file

or

awk '/Membership/{print NR":"$1; exit}'

if you want to stop at the first match.

> This will give you the line number and the line text.
>
> Below is the syntax to get only the line number:
> cat -n <filename>|grep "Membership"|awk '{print $1}'


UUOC and UUOG (useless use of grep):

awk '/Membership/{print NR}'

or

awk '/Membership/{print NR; exit}'

if you want to stop at the first match.

Regards,

Ed.

> Regards,
> vinod.
>
>
> Dattu wrote:
>
>
>

Xicheng

2006-01-20, 6:03 pm

Dattu wrote:
> Hi Good morning to All,
> I have the following requirement. Pl, Help me in solving this
> problem.
> The requirement is
> In the above text file How to find the line no that the string
> "Membership" occurs.

Another non-mainstreaming solution:

nl FILENAME | grep Membership

Xicheng

Dattu

2006-01-23, 7:52 am

Thanks a lot to ALL for your support ..

:-)

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com