LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash script to delete all comments after '#'?? (http://www.linuxquestions.org/questions/linux-newbie-8/bash-script-to-delete-all-comments-after-%27-%27-701061/)

dr44mon 01-30-2009 11:46 AM

bash script to delete all comments after '#'??
 
i need to make bash script than can delete all comment (all char after '#' in each line) in a single file.... I think it using grep? how to do make that? thanks a lot.....

David the H. 01-30-2009 12:20 PM

Since there's only a single matching reqirement this is a very easy task. You can do it with a simple sed replacement:

sed "s/#.*//" textfile.txt

dr44mon 01-30-2009 01:02 PM

Quote:

Originally Posted by David the H. (Post 3426518)
Since there's only a single matching reqirement this is a very easy task. You can do it with a simple sed replacement:

sed "s/#.*//" textfile.txt

may i ask again.... how we can do that if we have to use grep in the scripts? and how to replace the file that has been modified above (the comments has been removed) in the original file? thx a lot...

arizonagroovejet 01-30-2009 01:59 PM

Quote:

Originally Posted by dr44mon (Post 3426554)
may i ask again.... how we can do that if we have to use grep in the scripts? and how to replace the file that has been modified above (the comments has been removed) in the original file? thx a lot...

Sounds like homework. Run 'man grep' and look at the -v option. The other hint I'll give you is: regular expressions, specifically matching the start and end of a line.

pixellany 01-30-2009 03:12 PM

"have to use grep"----I assume because the instructor said so?

In addition to the advice above, you'll also be interested in the redirection operators (< and >), and the -i option in SED.

Depending on what texts are used in the class, you may want to get a copy of the Bash Guide for Beginners---- free at http://tldp.org


All times are GMT -5. The time now is 08:36 AM.