Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to execute the for loop with sed command, and is getting an error for the same

for i in <comma-separated server name list>;do "command";echo $i;done

where command=sed '/^$/d' /home/nextag/|grep -vc '#'

I'm getting the following error :- -bash: sed "/^$/d" /home/nextag/|grep -vc#: No such file or directory lu1

What is the correct way to execute this command to get the perfect output

I tried this as well>> for i in lu1;do 'sed \'/^$/d\' /home/nextag/|grep -vc \'#\'';echo $i;done

Also, can some explain the part '/^$/d'in sed '/^$/d' /home/nextag/|grep -vc '#'

share|improve this question
up vote 2 down vote accepted

This should work.

for i in <space-delimited server name list>;
sed '/^$/d' /home/nextag/ | grep -vc '^\s*#';
echo $i;

Below line is used to remove all the empty lines.


grep -vc '^\s*#' will ignore the lines starting with # and gives the count of other lines.

Hope this helps.

share|improve this answer
/^$ and /d both are used together?? – vipin8169 Nov 6 '12 at 5:29
can they be used seperately?? if yes then what do they mean separately? – vipin8169 Nov 6 '12 at 5:29
Vipin, What is the requirement? What do you mean by together and seperately? – devav2 Nov 6 '12 at 5:41
Say if test.txt file had more empty line. when you use sed '/^$/d' test.txt it will remove all the empty lines and print the non-empty lines. – devav2 Nov 6 '12 at 5:45
^$ will match empty lines. d is for deleting – devav2 Nov 6 '12 at 5:48

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.