LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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, 11:46 AM   #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, 12:20 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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, 01: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, 01:59 PM   #4
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,094

Rep: Reputation: 198Reputation: 198
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, 03:12 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
"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
 
Old 01-03-2020, 01:04 PM   #6
reduardo7
LQ Newbie
 
Registered: Jan 2020
Posts: 1

Rep: Reputation: Disabled
Example input

Code:
cat example.sh
Code:
#!/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
Remove comments

Code:
sed -e '1{/^#!/ {p}}; /^[\t\ ]*#/d;/\.*#.*/ {/[\x22\x27].*#.*[\x22\x27]/ !{:regular_loop s/\(.*\)*[^\]#.*/\1/;t regular_loop}; /[\x22\x27].*#.*[\x22\x27]/ {:special_loop s/\([\x22\x27].*#.*[^\x22\x27]\)#.*/\1/;t special_loop}; /\\#/ {:second_special_loop s/\(.*\\#.*[^\]\)#.*/\1/;t second_special_loop}}' example.sh
Result

Code:
cat example.sh
Code:
#!/bin/bash

echo "# test";

if [ "$1" = "#" ]; then 
  echo "#"; 
  echo '\#'; 
  echo \#\#\#; 
  echo \#
  echo \#;
  echo \#;
fi
Source: https://unix.stackexchange.com/a/560198/22180
 
Old 01-03-2020, 08:34 PM   #7
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,881
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
@reduardo7,

Thanks for contributing.

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.
 
Old 01-04-2020, 10:21 AM   #8
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,780

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
Time to let the cat out of the bag: GNU grep can print partial lines by means of the -o option (print what matches).
Code:
grep -o '^[^#]*'
Good for the exercise.
In practice you'll use sed.

Last edited by MadeInGermany; 01-04-2020 at 10:22 AM.
 
  


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 03:16 AM
Need a bash shell script which will delete lines from file scjohnie Linux - Newbie 1 09-13-2008 08:51 PM
Recursiveli delete in Bash Script combatdoc Programming 2 02-08-2008 09:47 AM
Why I am not figuring the delete.sh bash script? acwbrat Programming 1 11-02-2007 01:37 AM
bash script to delete files c0d3 Programming 9 12-05-2004 10:45 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:00 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
Open Source Consulting | Domain Registration