LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-19-2011, 02:13 PM   #1
harkonen
Member
 
Registered: May 2007
Distribution: slackware
Posts: 46

Rep: Reputation: 16
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
1 important line
  blah blah blah
2 important line
  blah blah blah
3 important line
  etc etc etc
I seem to be able to get sed and awk to grab only the important lines and number them, but I can't re-integrate those lines back into the file; ergo, I get:

Code:
bash$ cat file.txt | sed '/\<important/!d' | awk '{gsub(/\<important/,(x +=1));print}'
1 important line
2 important line
3 important line
I can also get sed alone to print the actual line numbers on just the important lines, but they obviously are the real line numbers and I'd prefer them to be numbered 1-whatever, sequentially, regardless of where they actually fall in the document.

Any ideas? I'm not married to sed or awk, so if it's a perl command or something, that works too.
 
Old 02-19-2011, 03:08 PM   #2
Birei
LQ Newbie
 
Registered: Nov 2010
Posts: 17

Rep: Reputation: 6
Hi,

Tell if next 'perl' script can be useful to you.
Code:
$ cat infile
important line
blah blah blah
important line
blah blah blah
important line
etc etc etc
$ perl -ne 'BEGIN { $i = 0 } if ( /important/ ) { print ++$i, " ", $_ }' infile
1 important line
2 important line
3 important line
Regards,
Birei
 
Old 02-19-2011, 03:19 PM   #3
harkonen
Member
 
Registered: May 2007
Distribution: slackware
Posts: 46

Original Poster
Rep: Reputation: 16
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!
 
Old 02-19-2011, 03:39 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Code:
awk '/important/{c = ++_}!/important/{c = ""}{printf "%3s ", c}1' file
 
1 members found this post helpful.
Old 02-19-2011, 03:55 PM   #5
harkonen
Member
 
Registered: May 2007
Distribution: slackware
Posts: 46

Original Poster
Rep: Reputation: 16
DONE!

Quote:
Originally Posted by colucix View Post
Code:
awk '/important/{c = ++_}!/important/{c = ""}{printf "%3s ", c}1' file
If i'm ever in Bologna I'll buy you a coffee. This did it. Thanks a million!
 
Old 02-19-2011, 03:57 PM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You're welcome! So waiting for you in Bologna!
 
Old 02-19-2011, 04:01 PM   #7
Birei
LQ Newbie
 
Registered: Nov 2010
Posts: 17

Rep: Reputation: 6
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
Regards,
Birei
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
numbering the lines punj33 Programming 9 07-21-2009 06:39 PM
OpenOffice numbering Q dianat100 Linux - Software 2 11-06-2005 12:58 PM
line numbering in ASCII file rohr Programming 4 03-18-2005 09:14 AM
Inode numbering zaheer031 Linux - Newbie 1 10-20-2004 07:47 AM
Drive re-numbering naismith Linux - Hardware 1 03-19-2004 09:59 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration