LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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



Reply
 
Search this Thread
Old 01-30-2009, 12:46 PM   #1
dr44mon
LQ Newbie
 
Registered: Jan 2009
Posts: 7

Rep: Reputation: 0
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.....
 
Old 01-30-2009, 01:20 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
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
 
Old 01-30-2009, 02:02 PM   #3
dr44mon
LQ Newbie
 
Registered: Jan 2009
Posts: 7

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by David the H. View Post
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...
 
Old 01-30-2009, 02:59 PM   #4
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: SLED, openSUSE
Posts: 1,032

Rep: Reputation: 174Reputation: 174
Quote:
Originally Posted by dr44mon View Post
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.
 
Old 01-30-2009, 04:12 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
"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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script- text file manip- delete everything before/after string justin99 Programming 9 11-20-2014 04:16 AM
Need a bash shell script which will delete lines from file scjohnie Linux - Newbie 1 09-13-2008 09:51 PM
Recursiveli delete in Bash Script combatdoc Programming 2 02-08-2008 10:47 AM
Why I am not figuring the delete.sh bash script? acwbrat Programming 1 11-02-2007 02:37 AM
bash script to delete files c0d3 Programming 9 12-05-2004 11:45 PM


All times are GMT -5. The time now is 05:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration