Match non-empty lines

grep -v "^\W$" <filename>
I had some trouble removing empty lines from a file (perhaps due to utf-8, as it's the source of all evil), \W did the trick eventually.

By: nikc
2009-06-18 08:17:22

These Might Interest You

  • Scrap everything and use `gawk` to do all the magic, since it's like the future or something. gawk 'match($11, /[a-z]{3}$/) && match($9, /^ata-/) { gsub("../", ""); print $11,"\t",$9 }' Yank out only ata- lines that have a drive letter (ignore lines with partitions). Then strip ../../ and print the output. Yay awk. Be sure to see the alternatives as my initial command is listed there. This one is a revision of the original. Show Sample Output

    ls -l /dev/disk/by-id |gawk 'match($11, /[a-z]{3}$/) && match($9, /^ata-/) { gsub("../", ""); print $11,"\t",$9 }' |sort
    lig0n · 2015-05-18 15:42:33 1
  • grep '^[^#]' sample.conf \__/ |||| \_________/ | |||| | | |||| \- Filename | |||| | |||\- Only character in group is '#' | ||| | ||\- Negate character group (will match any cahracter *not* in the | || group) | || | |\- Start new character group (will match any character in the | | group) | | | \- Match beginning of line | \- Run grep Empty lines will also be not matched, because there has to be at least one non-hash-sign character in the line. Show Sample Output

    grep '^[^#]' squid.conf
    Flow · 2013-02-21 18:51:06 4

  • 0
    sed -n -e '1,/match/p'
    climatewarrior · 2009-06-03 16:33:43 2
  • grep searches through a file and prints out all the lines that match some pattern. Here, the pattern is some string that is known to be in the deleted file. The more specific this string can be, the better. The file being searched by grep (/dev/sda1) is the partition of the hard drive the deleted file used to reside in. The ?-a? flag tells grep to treat the hard drive partition, which is actually a binary file, as text. Since recovering the entire file would be nice instead of just the lines that are already known, context control is used. The flags ?-B 25 -A 100? tell grep to print out 25 lines before a match and 100 lines after a match. Be conservative with estimates on these numbers to ensure the entire file is included (when in doubt, guess bigger numbers). Excess data is easy to trim out of results, but if you find yourself with a truncated or incomplete file, you need to do this all over again. Finally, the ?> results.txt? instructs the computer to store the output of grep in a file called results.txt. Source:

    grep -a -B 25 -A 100 'some string in the file' /dev/sda1 > results.txt
    olalonde · 2010-08-19 20:07:42 4

What Others Think

I'm not sure I understand this. \W matches a single non-alphanumeric character. The -v option negates the match. So your command prints all lines that do not consist of a single non-alphanumeric character. Maybe it was a line with MS-DOS line endings, i.e. CR-LF sequences, so the "empty" lines actually contained a single CR character? (Remember that UNIX only considers LF to terminate lines.) Anyway, there are several simple ways to print all non-empty lines (provided you have a UNIX text file with LF line termination). The simplest one is this: grep . file.txt But these work, too: grep -vx "" file.txt awk /./ file.txt sed -n /./p file.txt sed '/^$/d' file.txt tr -s \\n < file.txt echo g/./p | ed -s file.txt ... and many more.
inof · 466 weeks and 3 days ago
My preferred way: grep -v "^$" file.txt
unixmonkey3280 · 466 weeks and 3 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: