Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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/instance.properties|grep -vc '#'

I'm getting the following error :- -bash: sed "/^$/d" /home/nextag/instance.properties|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/instance.properties|grep -vc \'#\'';echo $i;done

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

share|improve this question

1 Answer 1

up vote 1 down vote accepted

This should work.

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

Below line is used to remove all the empty lines.

'/^$/d'

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
1  
Vipin, What is the requirement? What do you mean by together and seperately? –  devav2 Nov 6 '12 at 5:41
1  
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
1  
^$ will match empty lines. d is for deleting –  devav2 Nov 6 '12 at 5:48

Your Answer

 
discard

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.