|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
Hi. Code:
awk '
/^$/ { print ++C; next} 1
' input_fileLast 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
|
|||
|
|||
|
Thank you very much
It worked perfectly Lucky Ali |
|
#4
|
|||
|
|||
|
Shorter ![]() Code:
awk '$0=($0)?$0:++c' file |
| Sponsored Links | |
|
|
#5
|
||||
|
||||
|
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
|
|||
|
|||
|
Thank you. Your solution simplified Code:
awk '!NF{$0=++c}1' fileThere is always more than one way to solve a problem
|
| Sponsored Links | |
|
|
#7
|
||||
|
||||
|
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|