Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and 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 Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 10-09-2009
Scott's Unix or Linux Image
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 17 April 2015, 4:48 PM EDT
Location: Zürich
Posts: 8,534
Thanks: 336
Thanked 1,065 Times in 928 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 Unix and Linux 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 Unix and Linux 10-09-2009
danmero danmero is offline Forum Advisor  
 
Join Date: Nov 2007
Last Activity: 12 October 2014, 10:11 AM EDT
Location: H3X
Posts: 2,151
Thanks: 10
Thanked 118 Times in 111 Posts
Shorter Unix or Linux Image

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

Sponsored Links
    #5  
Old Unix and Linux 10-09-2009
Scott's Unix or Linux Image
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 17 April 2015, 4:48 PM EDT
Location: Zürich
Posts: 8,534
Thanks: 336
Thanked 1,065 Times in 928 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 Unix and Linux 10-09-2009
danmero danmero is offline Forum Advisor  
 
Join Date: Nov 2007
Last Activity: 12 October 2014, 10:11 AM EDT
Location: H3X
Posts: 2,151
Thanks: 10
Thanked 118 Times in 111 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 Unix or Linux Image
Sponsored Links
    #7  
Old Unix and Linux 10-09-2009
Scott's Unix or Linux Image
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 17 April 2015, 4:48 PM EDT
Location: Zürich
Posts: 8,534
Thanks: 336
Thanked 1,065 Times in 928 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image 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 07:35 PM.