|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
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 |
|
#4
|
|||
|
|||
|
Quote:
In awk it is quite straitforward: awk ' { if (match($0, "//")) printf "%5d. %s\n", NR, substr($0, RSTART) } ' Try it. Regards zlutovsky |
|
#5
|
|||
|
|||
|
yes zlutovsky
Quote:
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. |
|
#6
|
|||
|
|||
|
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 |
|
#7
|
|||
|
|||
|
Quote:
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 |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > Bourne Shell scripting help |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|