Computing.Net > Forums > Unix > How to disp the lines number

How to disp the lines number

Reply to Message Icon

Original Message
Name: thomas leung
Date: September 8, 2006 at 22:57:23 Pacific
Subject: How to disp the lines number
OS: Linux red hat 9
CPU/Ram: P4
Comment:

Sir,

I have 1 assignment need to do the following task:-
Write a script that finds each line in a file that contains a specified substring (not a
regrep). To test the script creates a file of at least 20 lines. Display the result in the following format:-
LIne Number: Line contents

Please advise the method / hints.

thanks!
regards,
thomas



Report Offensive Message For Removal


Response Number 1
Name: lchi2000g
Date: September 9, 2006 at 16:21:23 Pacific
Subject: How to disp the lines number
Reply: (edit)

grep -n stringtofind infile.txt

Luke Chi


Report Offensive Follow Up For Removal

Response Number 2
Name: thomas leung
Date: September 12, 2006 at 04:34:35 Pacific
Subject: How to disp the lines number
Reply: (edit)

Sorry! I forgot to write down 1 more criteria for my assignment:

The criteria is:
Do not apply "grep" to the whole file but loop over the file line by line.

thanks!
regards,
thomas


Report Offensive Follow Up For Removal

Response Number 3
Name: lchi2000g
Date: September 12, 2006 at 05:13:19 Pacific
Subject: How to disp the lines number
Reply: (edit)

==> input.txt:

S,101,TEX.10000003,G,1,02,N
S,101,TEX.10000004,G,1,02,N
S,101,TEX.10000005,G,1,02,N
S,101,TEXXX.10000002,G,1,02,N
S,101,TEX.10000001,G,1,02,N
S,101,YYTEX.00000004,G,1,02,N
S,101,TEX.10000106,G,1,02,N

==> script: 1.sh

$ cat 1.sh
#!/bin/ksh

cat input.txt | while read LINE
do
echo $LINE | grep -n TEXXX -
done

==> run:

$ ./1.sh

1:S,101,TEXXX.10000002,G,1,02,N


Luke Chi


Report Offensive Follow Up For Removal

Response Number 4
Name: thomas leung
Date: September 12, 2006 at 06:58:02 Pacific
Subject: How to disp the lines number
Reply: (edit)

Luke Chi,
I have done it.
Thanks to your sript.

regards,
thomas


Report Offensive Follow Up For Removal

Response Number 5
Name: lchi2000g
Date: September 12, 2006 at 08:39:36 Pacific
Subject: How to disp the lines number
Reply: (edit)

You're welcome

Luke Chi


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: How to disp the lines number 

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software