UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old January 13th, 2005, 12:08 PM
omnSweetness omnSweetness is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 6 omnSweetness User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Bourne Shell scripting help

I am trying list all the comments (with line numbers) in a given file.

For simplicity, all coments start with // and there is a space before and after the slashes. My problem is not listing the comments with the line numbers, it is that I do not want to list the entire line, just whatever is after the // token. For example if I have:

x = 0; // x is equal to zero

if use grep -n " //" $2 , then it lists the whole line:

1: x = 0; // x is equal to zero

Instead I would like it to list everything after the comment like this:

1: x is equal to zero

I am assuming that you can use regualr expressions but that is honestly something I am having trouble grasping. If anyone can help me, I would be very grateful.

Thank you in advance.

-Sweetness

Reply With Quote
  #2  
Old January 13th, 2005, 02:30 PM
omnSweetness omnSweetness is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 6 omnSweetness User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
awk

I realize now that I will need to use awk but have no clue where to get started. I am trying to understand what is out there on the web, but am still confused as to how I can solve my problem with awk.

-Sweetness

Reply With Quote
  #3  
Old January 16th, 2005, 05:19 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,083 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 19 h 49 m 57 sec
Reputation Power: 9
no need of awk for that.
to numbering all lines in filename use
grep -n . filename
or
nl -ba filename (see man pages)
then pipe the output to sed, eg
grep -n . filename¦sed -n 's/^\([0-9]*[0-9]:\)\(.*\/\)\(.*\)/\1 \3/p'
awk is a fantastic tool, not appropriated in this case
you also could convice 'sed' to count lines, this is an other chaper.

Last edited by guggach : January 16th, 2005 at 05:21 AM. Reason: changes

Reply With Quote
  #4  
Old January 18th, 2005, 06:06 AM
zlutovsky zlutovsky is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Prague, Czech Rep.
Posts: 116 zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 h 34 sec
Reputation Power: 6
Quote:
Originally Posted by guggach
no need of awk for that.
to numbering all lines in filename use
grep -n . filename
or
nl -ba filename (see man pages)
then pipe the output to sed, eg
grep -n . filename¦sed -n 's/^\([0-9]*[0-9]:\)\(.*\/\)\(.*\)/\1 \3/p'
awk is a fantastic tool, not appropriated in this case
you also could convice 'sed' to count lines, this is an other chaper.



In awk it is quite straitforward:

awk '
{
if (match($0, "//"))
printf "%5d. %s\n", NR, substr($0, RSTART)
}
'

Try it.

Regards zlutovsky

Reply With Quote
  #5  
Old January 18th, 2005, 06:52 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,083 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 19 h 49 m 57 sec
Reputation Power: 9
yes zlutovsky

Quote:
awk it is more straitforward:


awk '/\/\//{ print what-you-want; }'

and stupid for this job, you should learn how awk works.

Last edited by guggach : January 18th, 2005 at 06:55 AM.

Reply With Quote
  #6  
Old January 18th, 2005, 10:24 PM
omnSweetness omnSweetness is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 6 omnSweetness User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thank you

Thanx for all the help guys.

Out of ignorance why would awk be stupid for this job?

I got it to work with the awk using:

awk 'match($0, "//") { printf("line %d : %s\n", FNR, substr($0, RSTART+2)); }' $file


Again, thankl you all for the help.

-Sweetness

Reply With Quote
  #7  
Old January 19th, 2005, 03:15 AM
zlutovsky zlutovsky is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Prague, Czech Rep.
Posts: 116 zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 h 34 sec
Reputation Power: 6
Quote:
Originally Posted by guggach
yes zlutovsky



awk '/\/\//{ print what-you-want; }'

and stupid for this job, you should learn how awk works.



But dear guggach, have you tried my solution? I assure you, I know awk quite well and before publishing my suggestions, I test them on the computer. Do it too, probatum est.

Regards zlutovsky

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Bourne Shell scripting help


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway