Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.....
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...
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.
#!/bin/bash
# example script
echo "# test";# echo "# test"
# check the first parameter
if [ "$1" = "#" ]; then
# test couple of different cases
echo "#"; # output # character
echo '\#'; # output # character '#' for test purpose
echo \#\#\#; # comment # comment # comment '# comment'
echo \#
echo \#;
echo \#; # comment
fi
# end of the script
Please note that the age of the thread is in excess of 11 years.
Also seems based on the manner of questioning, it was an assignment of some type.
1. As shown, there were specific restrictions for how to accomplish it.
2. Rather than offer a full solution, we try to guide an OP by asking them to show their efforts and help them to learn how they can implement their own solution. They always could've performed a web search, they don't appear to have done so. This OP doesn't appear to have shown any effort, even the little you've put forth with your web search.
Also please note that the LQ site does ask you if you really intend to post new content to threads with no posts newer than six months ago. Not trying to he critical, but it's a good guide to consider and gauge whether or not any new information will be of any help.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.