em Posted June 6, 2011 Report Posted June 6, 2011 S? spunem c? face?i rost de o sursa de C dar nu vre?i ?i comentariile din ea.#! /bin/sed -nf# Remove C and C++ comments, by Brian Hiles (brian_hiles@rocketmail.com)# Sped up (and bugfixed to some extent) by Paolo Bonzini (bonzini@gnu.org)# Works its way through the line, copying to hold space the text up to the# first special character (/, ", '). The original version went exactly a# character at a time, hence the greater speed of this one. But the concept# and especially the trick of building the line in hold space are entirely# merit of Brian.# ./script.sed < input.c# ./script.sed < input.c > output.c# for c in *.c; do script.sed < $c > /tmp/zyzcc.c; /bin/cp -f /tmp/zyzcc.c $c; done:loop# This line is sufficient to remove C++ comments!/^\/\// s,.*,,/^$/{ x p n b loop}/^"/{ :double /^$/{ x p n /^"/b break b double } H x s,\n\(.[^\"]*\).*,\1, x s,.[^\"]*,, /^"/b break /^\\/{ H x s,\n\(.\).*,\1, x s/.// } b double}/^'/{ :single /^$/{ x p n /^'/b break b single } H x s,\n\(.[^\']*\).*,\1, x s,.[^\']*,, /^'/b break /^\\/{ H x s,\n\(.\).*,\1, x s/.// } b single}/^\/\*/{ s/.// :ccom s,^.[^*]*,, /^$/ n /^\*\//{ s/..// b loop } b ccom}:breakHxs,\n\(.[^"'/]*\).*,\1,xs/.[^"'/]*//b loop Cum se foloseste?./removeCommends.sed fisierInitial.cpp > fisierFaraComentarii.cppBonus:Nu strica indentarea. 1 Quote
Zatarra Posted June 6, 2011 Report Posted June 6, 2011 Prea simpluSa presupunem ca ai o sursa source.cDai in terminal comanda astacat source.c |grep -v "^#" > newsource.cSi apoi vei avea fisieru newsource.c fara linile cu comentarii de genu #Comment ;-)Scuze asta e daca ai o sursa de bash scripting, cu care eu sunt obisnuit Exista si asa:cat source.c |grep -v "^//" > newsource.cPentru a sterge linile care incep cu '//' (daca asta vroiai)Pentru '/*' e putin mai complicat dar se poateEdit: Am perfectionat umpic script-ul pentru a scoate liniile care incep cu # (pastrand prima linie - pentru bash scripting) si totodata eliminand liniile goale1. head -1 source.c > newsource.c2. cat source.c |grep -v "^#" |sed '/^$/d' >> newsource.c1. Copiaza prima linie in fisierul newsource.c2. Elimina liniile care incep cu # si liniile goaleExemplu:source.c#! /bin/sed -nf# Remove C and C++ comments, by Brian Hiles (brian_hiles@rocketmail.com)# Sped up (and bugfixed to some extent) by Paolo Bonzini (bonzini@gnu.org)# Works its way through the line, copying to hold space the text up to thesdgsdfg//dsfgsdfh#//r123tW/*Qdg23g#@!%@!#TdgsdgsgweSi am obtinut:newsource.c#! /bin/sed -nfsdgsdfg//dsfgsdfh//r123tW/*Qdg23gdgsdgsgwe Quote
em Posted June 6, 2011 Author Report Posted June 6, 2011 Neah. Asta le scoate si daca sunt asacod // comentariu/*comentariu*/Cred ca ai vrut sa scrii grep -v "//". Quote
cifratorul Posted June 6, 2011 Report Posted June 6, 2011 Edit: Am perfectionat umpic script-ul pentru a scoate liniile care incep cu # De ce as vrea sa scot dintr-un fisier C liniile ce incep cu #? Sa scot toate directivele preprocesor? Quote
Zatarra Posted June 6, 2011 Report Posted June 6, 2011 @cifratorul am spus la faza cu scuze cum sta treabaOricum am facut un tutorial cu scoaterea comment-urilor din surse in C/C++Pentru tutorial click Here Quote