Delete Comments


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Delete Comments
# 1  
Old 04-14-2011
Delete Comments

Hello i am back Smilie,

i have a prolem. I want to Delete the IPs which are in Comments.

Input
PHP Code:
192.168.0.1
192.168.0.2
#192.168.0.3
         #192.168.0.4   - when TAB or Space, delete too.
/*192.168.0.5
192.168.0.6
192.168.0.7*\ 
Output
PHP Code:
192.168.0.1
192.168.0.2 
My solution is
PHP Code:
sed -'/^#/d' -'/^$/d' or sed -ne '/^[[:blank:]]*[^#]/p' 
but it isnīt that what i want.

this removes only # with space or TAB
PHP Code:
sed '1p; /^[[:blank:]]*#/d; s/[[:blank:]][[:blank:]]*#.*//' 
thx

Last edited by eightball; 04-14-2011 at 10:07 AM..
# 2  
Old 04-14-2011
Try:
Code:
perl -lp0e 's/#.*\n//g;s/\/\*.*?\*\\//sg' file

# 3  
Old 04-14-2011
Quote:
Originally Posted by eightball
Hello i am back Smilie,

i have a prolem. I want to Delete the IPs which are in Comments.

Input
[PHP]
192.168.0.1
192.168.0.2
#192.168.0.3
#192.168.0.4 - when TAB or Space, delete too.
/*192.168.0.5
192.168.0.6
192.168.0.7*\
Shouldn't the last line
Code:
192.168.0.7*\

be
Code:
192.168.0.7*/

?
# 4  
Old 04-14-2011
sorry i mean /* */
# 5  
Old 04-14-2011
Try:
Code:
awk '/#/{next} /\/\*/{p=1} /\*\//{p=0;next}!p' file

# 6  
Old 04-14-2011
Quote:
Originally Posted by eightball
sorry i mean /* */
Then try this:
Code:
perl -lp0e 's/#.*\n//g;s/\/\*.*?\*\///sg' file

# 7  
Old 04-14-2011
The opposite approach - grep for "good" lines.
Code:
grep -E '^([0-9]{1,3}\.){3}[0-9]{1,3}$' file

 
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Deleting comments from c-file

hii all, i am writing a shell script to remove comments from a .c/.cpp file. i have written script as the above script file deletes line between /* and */ also lines starting with //. but the problems are : 1) i dont want to delete the content between /** and */. 2)sed -i... (16 Replies)
Discussion started by: yashwantkumar
16 Replies

2. Shell Programming and Scripting

print only comments

I want to write a shell script which it takes as argument a java file or a c++ file (.java or .cpp). It will check if the file is type of java or c++, else it ends with error message. If all are ok, it will call awk that prints only the comments that the java or c++ file contains, grouping and... (5 Replies)
Discussion started by: Mark_orig
5 Replies

3. Shell Programming and Scripting

Sed script, changing all C-comments to C++-comments

I must write a script to change all C++ like comments: // this is a comment to this one /* this is a comment */ How to do it by sed? With file: #include <cstdio> using namespace std; //one // two int main() { printf("Example"); // three }//four the result should be: (2 Replies)
Discussion started by: black_hawk
2 Replies

4. UNIX for Dummies Questions & Answers

Delete Comments in file

How can I delete comments (lines beginning with /* and ending with */) in file? with single command line..My suggestion is to use grep and sed! (4 Replies)
Discussion started by: aadi_uni
4 Replies

5. Shell Programming and Scripting

delete comments

Delete everything comes in between /* & */. Current File: ==================== create or replace procedure test421 is begin /* ---sasasas/*dsdsds */ dbms_output.put_line('SAURABH'); END; To be File: =================== create or replace procedure test421 is begin... (10 Replies)
Discussion started by: susau_79
10 Replies

6. Shell Programming and Scripting

Scripting comments

Anyone have a good link for documentation conventions for scripts? (4 Replies)
Discussion started by: gliesian
4 Replies

7. Programming

lint comments

Hi can anyone help me regarding the meaning of the following lint messages. what is the use of having such lint comments in the c program. /*lint -esym(534,cputs,fgets,cprintf) */ /*lint -efile(766,pragmas.h) */ Thanks a lot in advance. (5 Replies)
Discussion started by: axes
5 Replies

8. UNIX for Dummies Questions & Answers

Remove comments...

It may be a no-brainer, but the answer is escaping me right now: I'm trying to write a little script to remove all comments from .c source... I was thinking sed, but I'm not a very strong regexp user (e.g. I suck with sed). I tried dumping the file into: sed -e 's/\/\* * \*\///g' and several... (1 Reply)
Discussion started by: LivinFree
1 Replies
Login or Register to Ask a Question