Terminal Tip: the joy of nl
Too often overlooked, nl provides a very useful shell command. As you can see here, when issued, it numbers the lines of a text file. But that's certainly not all that this little utility can do. My favorite feature allows you to count the occurrences of a regular expression. Use the -bp flag to tell nl to count all the lines that match that expression. For example:
% nl -bp"So.*e" sonnet.txt
Shall I compare thee to a summer's day?
Thou art more lovely and more temperate:
Rough winds do shake the darling buds of May,
And summer's lease hath all too short a date:
1 Sometime too hot the eye of heaven shines,
And often is his gold complexion dimm'd;
And every fair from fair sometime declines,
By chance or nature's changing course untrimm'd;
But thy eternal summer shall not fade
Nor lose possession of that fair thou owest;
Nor shall Death brag thou wander'st in his shade,
When in eternal lines to time thou growest:
2 So long as men can breathe or eyes can see,
3 So long lives this and this gives life to thee.
%
Share
Source: http://www.ss64.com/bash/nl.html
Categories
Too often overlooked, nl provides a very useful shell command. As you can see here, when issued, it numbers the lines of a text file. But...
Add a Comment
cat -n
or
cat -b
works too, depending on how you want to nuber the lines
Sovak:
grep "." sonnet.txt | wc -l
Thanks. I made a script with this, which returns the number of non-empty lines:
#!/bin/bash
# Count Lines
case "$1" in "") echo "Usage: ${0##*/} filename"; exit $E_PARAM;;
esac
nl $1 | tail -n 1 | awk '{print $1}'
Does anyone know a shorter/faster way?
Brilliant! Thank you!
-systemsboy
It's part of the regular expression. "So.*e" means to match any string starting with a capital S, a lower case o, followed by any number of characters (.*, where the dot is a wildcard and * means zero or more occurances) followed by a lower-case e.
April 09 2007 at 6:49 PM Report abuse Permalink rate up rate down Replywhat does the .*e mean? (terminal n00b)
April 09 2007 at 6:45 PM Report abuse Permalink rate up rate down ReplyHot Apps on TUAW
Deals of the Day
more deals- Used Apple iMac 17" Core 2 Duo 1.83GHz for $430 + $28 s&h
- Lounge Deluxe Stand for iPhone / iPod touch for $28 + $8 s&h
- Brookstone Surround-Sound Earbuds for $14 + $7 s&h
- Refurbished Skullcandy Tokidoki Smokin' Buds Mic'd Headset for $5 + $2 s&h
- Stitchway Backup Battery for iPod / iPhone for $5 + free shipping
- Used Apple MacBook Pro 2.4GHz 15" LED Laptop for $1,030 + $29 s&h
Software Updates
more updates- EFI Firmware Update brings Lion Internet Recovery to 2010-model Macs
- OS X Lion 10.7.3 released with Safari 5.1.3, Wi-Fi bug fix
- Aperture updated to 3.2.2, addresses Photo Stream issue
- Apple updates Keynote to address Lion issues
- Google Search app gets new look on iPad
- Apple releases Apple TV Software Update 4.4.3



6 Comments