views:

129

answers:

4

hi all

how to find how many lines I have in file by sed (need to ignore spaces and empty lines)

for example

if I have file with 139 lines (line can include only one character) then sed should return 139

lidia

+3  A: 

You can try:

sed -n '/[^[:space:]]/p' filename | wc -l

Here sed prints only those line that have at least one non-space char and wc counts those lines.

codaddict
+1  A: 

Use nawk instead of sed.

nawk 'NF{c++}END{print "total: "c}' file
ghostdog74
+2  A: 

This is a job for grep, not sed:

<myfile grep -c '[^[:space:]]'
Gilles
A: 
sed '/^ *$/ d' filename | wc -l

Here, sed prints the lines after deleting all the lines with 0 or more spaces from beginning to the end. wc -l is to count the number of these lines.

dheerosaur