Unix help to find blank lines in a file and print numbers on that line | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Unix help to find blank lines in a file and print numbers on that line

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-09-2009
Lucky Ali Lucky Ali is offline
Registered User
 
Join Date: Oct 2009
Last Activity: 31 January 2014, 7:06 PM EST
Posts: 175
Thanks: 18
Thanked 0 Times in 0 Posts
Unix help to find blank lines in a file and print numbers on that line

Hi,
I would like to know how to solve one of my problems using expert unix commands.

I have a file with occasional blank lines;
for example;

dertu
frthu
fghtu

frtty
frtgy
frgtui

frgtu
ghrye
frhutp
frjuf

I need to edit the file so that the file looks like this;
1
dertu
frthu
fghtu
2
frtty
frtgy
frgtui
3
frgtu
ghrye
frhutp
frjuf
************
That is: I want to grep or find the blank line and sequentially number the blank lines and print that number in the blank space.

Hope you guys understood what I wanted.

Please let me know the best way for doing this;

Lucky Ali
Sponsored Links
    #2  
Old 10-09-2009
Scott's Avatar
Scott Scott is online now Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 21 August 2014, 1:57 PM EDT
Location: Zürich
Posts: 8,472
Thanks: 330
Thanked 1,046 Times in 913 Posts
Hi.


Code:
awk '
/^$/ { print ++C; next} 1
' input_file


Last edited by Scott; 10-09-2009 at 04:19 PM.. Reason: removed statement "If I understand you correctly:" as even for me it was quite understandable!
Sponsored Links
    #3  
Old 10-09-2009
Lucky Ali Lucky Ali is offline
Registered User
 
Join Date: Oct 2009
Last Activity: 31 January 2014, 7:06 PM EST
Posts: 175
Thanks: 18
Thanked 0 Times in 0 Posts
Thank you very much
It worked perfectly
Lucky Ali
    #4  
Old 10-09-2009
danmero danmero is offline Forum Advisor  
 
Join Date: Nov 2007
Last Activity: 21 August 2014, 5:49 AM EDT
Location: H3X
Posts: 2,151
Thanks: 10
Thanked 117 Times in 110 Posts
Shorter

Code:
awk '$0=($0)?$0:++c' file

Sponsored Links
    #5  
Old 10-09-2009
Scott's Avatar
Scott Scott is online now Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 21 August 2014, 1:57 PM EDT
Location: Zürich
Posts: 8,472
Thanks: 330
Thanked 1,046 Times in 913 Posts
I'm not one to use things like LOL, LMAO, etc. But just for you:

ROFL!!!!

And it's only shorter because you didn't use spaces and your filename was shorter :-)

Shorter:

Code:
awk '$0=($0)?$0:++c' f


Last edited by Scott; 10-09-2009 at 04:47 PM.. Reason: Changed grammatical error "one one" to "not one", thus changing one one to not
Sponsored Links
    #6  
Old 10-09-2009
danmero danmero is offline Forum Advisor  
 
Join Date: Nov 2007
Last Activity: 21 August 2014, 5:49 AM EDT
Location: H3X
Posts: 2,151
Thanks: 10
Thanked 117 Times in 110 Posts
Quote:
Originally Posted by scottn View Post
Code:
awk '
/^$/ { print ++C; next} 1
' input_file

Thank you.
Your solution simplified

Code:
awk '!NF{$0=++c}1' file

There is always more than one way to solve a problem
Sponsored Links
    #7  
Old 10-09-2009
Scott's Avatar
Scott Scott is online now Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 21 August 2014, 1:57 PM EDT
Location: Zürich
Posts: 8,472
Thanks: 330
Thanked 1,046 Times in 913 Posts
And that's the beauty of it.

There's a lot to be said for the "concise but readable" option though.

When you can buy a 1.5 TB disk for 139 Swiss Francs, where's the harm in adding some white-space?!
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
find lines from one file in the other and print them coppuca UNIX for Dummies Questions & Answers 3 08-05-2009 09:52 PM
sed: print out to first blank line lagagnon Shell Programming and Scripting 2 12-16-2008 10:31 AM
How to print the lines after 2nd line from text file ram1729 Shell Programming and Scripting 6 09-16-2008 10:34 PM
Strip one line from 2 blank lines in a file tipsy Shell Programming and Scripting 6 06-23-2008 08:14 AM
how to print out line numbers of a text file? forevercalz Shell Programming and Scripting 4 12-12-2005 04:04 AM



All times are GMT -4. The time now is 02:01 PM.