how to remove words between /* and */ in several lines of file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to remove words between /* and */ in several lines of file
# 1  
Old 07-09-2009
how to remove words between /* and */ in several lines of file

hi,
I want to remove comments in somany files. coments started from /* and ends with */. so i wnt to remove words /* to */
how to remove words between /* and */ in several lines of file

EX :
cat haha.txt
"HI",/*hk*/ob1,raju,/*hjh*/boju,beeju
output should be : "HI",ob1,raju,boju,beeju

I tried like below way :

cat haha.txt | sed -e 's:/*.*/::'

but output is coming : boju,beeju

Please help me ...
# 2  
Old 07-09-2009
Try this:

Code:
sed '
s@/\*[^/\*]*\*/@@g
\@/\*@{
  N
  s@ *\n@ @
  s@/\*[^/\*]*\*/@@g
}' haha.txt

But it won't remove comments, spaning more than two lines...
# 3  
Old 07-09-2009
cat haha.txt | perl -e 'while(<>){ s/\/\*.*?\*\///g; print;}'

"HI",ob1,raju,boju,beeju

This is done with the assumption that all comments are in a single line. If comments span multiple lines then the codes need to be tweaked

Cheers
# 4  
Old 07-09-2009
Hi Sapfeer,

I tried ur code in my machine. Its working fine . But i couldnt understand how it works. Im very new to shell scripting.. even i tried with tutorials.....
Please explain me
1. s@/\*[^/\*]*\*/@@g why [^/\*] this is required?
2. how it checks for multiple lines... becaz i saw in one of the tutorial...label is required to check multiple lines ...



Please help me...

Thanks in advance
# 5  
Old 07-09-2009
Code:
awk 'BEGIN{ FS="*/"}
{  
    for(i=1;i<=NF;i++){
        gsub(/\/\*.*/,"",$i)
    }
}1' file

# 6  
Old 07-09-2009
spc432
Sure!

Lets start from the first line of the script.
Code:
s@/\*[^/\*]*\*/@@g

This removes comments on one line. '[^/\*]' needs to find all symbols, but '/' '*' to the closing comment. Actually, I've missed that in comments could be symbols '/' and '*'. It won't work in this circumstances. '[^/\*]' should be removed...
Code:
\@/\*@{

This line matches opening comment and start section, which is applied to all line, containing '/*' symbols.
Code:
N

This command reads next line and puts it in pattern space.
Code:
s@ *\n@

This line is not necessary here
Code:
@   s@/\*[^/\*]*\*/@@g}

This substitutes all symbols between '/*' and '*/' to ''.

And it doesn't need label to check two lines. For checking in more than two it needs.

So, final script should looks like this
Code:
s@/\*.*\*/@@g
\@/\*@{
  N
  s@/\*.*\*/@@g
}

And finally, try to find book Sed & Awk - it explains all sed stuff Smilie

Last edited by Sapfeer; 07-09-2009 at 04:01 PM..
# 7  
Old 07-10-2009
Code:
echo '"HI",/*hk*/ob1,raju,/*hjh*/boju,beeju' | sed 's/\/\*[^*]*\*\///g'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Filter all the lines with minimum specified length of words of a text file

Hi Can someone tell me which script will work best (in terms of speed and simplicity to write and run) for a large text file to filter all the lines with a minimum specified length of words ? A sample script with be definitely of great help !!! Thanks in advance. :) (4 Replies)
Discussion started by: my_Perl
4 Replies

2. Shell Programming and Scripting

Remove lines that are subsets of other lines in File

Hello everyone, Although it seems easy, I've been stuck with this problem for a moment now and I can't figure out a way to get it done. My problem is the following: I have a file where each line is a sequence of IP addresses, example : 10.0.0.1 10.0.0.2 10.0.0.5 10.0.0.1 10.0.0.2... (5 Replies)
Discussion started by: MisterJellyBean
5 Replies

3. UNIX for Dummies Questions & Answers

Want to remove all lines but not latest 50 lines from a file

Hi, I have a huge file which has Lacs of lines. File system got full. I want your guys help to suggest me a solution so that I can remove all lines from that file but not last 50,000 lines. I want solution which can remove lines from existing file so that I can have some space left with. (28 Replies)
Discussion started by: prashant2507198
28 Replies

4. UNIX for Dummies Questions & Answers

Extract lines with specific words with addition 2 lines before and after

Dear all, Greetings. I would like to ask for your help to extract lines with specific words in addition 2 lines before and after these lines by using awk or sed. For example, the input file is: 1 ak1 abc1.0 1 ak2 abc1.0 1 ak3 abc1.0 1 ak4 abc1.0 1 ak5 abc1.1 1 ak6 abc1.1 1 ak7... (7 Replies)
Discussion started by: Amanda Low
7 Replies

5. Shell Programming and Scripting

Remove lines from file

Hey Gang- I have a list of servers. I want to exclude servers that begin with and end with certain characters. Is there an easy command to do this? Example wvm1234dev wvm1234pro uvm1122dev uvm1122bku uvm1344dev I want to exclude any lines that start with "wvm" OR "uvm" AND end... (7 Replies)
Discussion started by: idiotboy
7 Replies

6. Shell Programming and Scripting

Need to parse lines in a file into two words and assign the values to two variables

For example, I have a file with below lines containing VOB tags and VOB paths. * /vobs/fts/FTSUSM20_VOB /ccvobsslx01/projects/vobs/eml/FTSUSM20_VOB * /vobs/fts/FTS20_VOB /ccvobsslx01/projects/vobs/eml/FTS20_VOB * /vobs/pmv/PMS_VOB /ccvobsslx01/projects/vobs/cpm/_/PMS_VOB *... (4 Replies)
Discussion started by: senthilkc
4 Replies

7. Shell Programming and Scripting

recursively Grep particular words among lines from a file

dear experts, Could you please help me to write a command/script to find follwing: from the log file i want to grep "resCode89270200100001552311" after that only "<resultCode>40614</resultCode>" it will be like that: resCode89270200100001552311 <resultCode>40614</resultCode> ... (7 Replies)
Discussion started by: thepurple
7 Replies

8. UNIX for Dummies Questions & Answers

Remove words beginning with a certain character from a file

Hi, how could you go about removing words that begin with a certain character. assuming that this character is '-' I currently have echo "-hello" | sed s/-/""/ which replaces the leading dash with nothing but I want to remove the whole word, even if there are multiple words beginning... (3 Replies)
Discussion started by: skinnygav
3 Replies

9. UNIX for Advanced & Expert Users

Remove words from file

Hello, I have a question: I have two different files, let's call them file1 and file2. file1 contains a list of words, the words are on seperate lines: word1 word2 word3 word4 etc... file2 also contains a list of words, seperated in the same way as file1. What I want to do is... (5 Replies)
Discussion started by: Beeser
5 Replies

10. Shell Programming and Scripting

Ignore some lines with specific words from file comparison

Hi all, I need help in doing this scenario. I have two files with multiple lines. I want to compare these two files but ignoring the lines which have words like Tran, Loc, Addr, Charge. Also if i have a word Credit in line, i want to tokenize (i.e string after character " ... (2 Replies)
Discussion started by: jakSun8
2 Replies
Login or Register to Ask a Question