incremental and selective line numbering
I'm really close to getting this right, I think, but is one nuance eluding me, so I'm going to ask here.
I'm trying to number lines in a file -- but ONLY certain lines. Example: Code:
bash$ cat file.txt Code:
bash$ cat file.txt | sed '/\<important/!d' | awk '{gsub(/\<important/,(x +=1));print}' Any ideas? I'm not married to sed or awk, so if it's a perl command or something, that works too. |
Hi,
Tell if next 'perl' script can be useful to you. Code:
$ cat infile Birei |
that's a great perl script to know about but unfortunately no that will not work, because (and I should have mentioned this in my first post) the important files happen at irregular intervals. Sometimes they are 3 lines apart, other times they will be 24 lines apart, and another time they might be 8 lines apart, and so on.
My advantage is that the important lines always will have a very easy-to-match regex in them. Finding them is easy; getting them numbered the way I want..that's the hard part! |
Code:
awk '/important/{c = ++_}!/important/{c = ""}{printf "%3s ", c}1' file |
DONE!
Quote:
|
You're welcome! So waiting for you in Bologna! ;)
|
Hi,
My script was incorrect. I missunderstood your question. This should do the job, although colucix already got it. Code:
$ perl -ne 'BEGIN { $i = 0 } if ( /important/ ) { printf "%4d %s", ++$i, $_ } else { printf " %s", $_ }' infile Birei |
All times are GMT -5. The time now is 11:48 PM. |