LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to add comments into a text file using sed (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-add-comments-into-a-text-file-using-sed-946772/)

agarrett5 05-25-2012 03:20 AM

how to add comments into a text file using sed
 
I am trying to alter a sql file. Its so big I have to alter it using sed or some other search and replace command. I want to add a comment into the file before drop table but I dont know how to add the /*. I need to encase it in some kind of charactor around it so it knows to add it and that its not a comment in the linux command line (if that makes sense). I have tried \\/\\* but it doesnt seem to have worked. command so far run: sed 's/'DROP' / \\/\\*DROP/ asteriskcdrdb2a.sql

pan64 05-25-2012 03:25 AM

I suggest you to use awk:
awk ' /DROP/ { print all your comments before DROP }
{ print } ' inputfile > outputfile

cliffordw 05-25-2012 03:32 AM

Hi there,

To prevent your shell from interpreting these characters, you should be able to just enclose the entire sed command in single quotes, as follows:

Code:

sed 's/'DROP / \/\*DROP /' asteriskcdrdb2a.sql
It's not 100% clear to me what the desired end result is, but presumably you want to comment out just the DROP statements, which means you also need to close the comments. Assuming a single line DROP statement like this:
Quote:

DROP TABLE IF EXISTS `jos_plugins`;
You could use a command like this:
Code:

sed 's/\(DROP .*;$\)/\/\* \1\ *\//' asteriskcdrdb2a.sql
Hope this helps!

agarrett5 05-25-2012 03:45 AM

Thankyou Clifford, I will try that. You are correct I do want to comment out the drop command in an sql file :)

agarrett5 05-25-2012 04:06 AM

That worked well, thankyou :)

David the H. 05-25-2012 10:19 AM

Quote:

Originally Posted by cliffordw (Post 4687129)
You could use a command like this:
Code:

sed 's/\(DROP .*;$\)/\/\* \1\ *\//' asteriskcdrdb2a.sql

How headache-inducing. ;) There are two things you can do to alleviate the leaning toothpick syndrome and make this more readable.

1) Change the delimiter for the 's' command to something other than a slash. Just about any basic ascii character can be used, just choose something not found in the expression itself.

2) Use the -r option to keep from having to backslash extended regex characters. The grep man page has a good section on the difference between basic and extended regex.

Code:

sed -r 's|(DROP .*;$)|/* \1 */|' asteriskcdrdb2a.sql

And for the OP, read these three links to get an understanding of how the shell handles arguments and quoting. The QUOTING section of the bash man page has some good info too.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

Just remember that the sed expression needs to be passed to the process as a single argument, with all the characters in it intact.

David the H. 05-27-2012 10:38 AM

Back again with some more advice.

You might consider using ed or vim instead. As dedicated text editors, they can update the file directly without the need to work through temporary files. All you need to do is feed them a series of commands separated by newline characters (I like to use printf for this personally).

ed is lighter and more portable, but it has the downside that you can't change the "s" delimiter, so we're back to some picket-fence like strings.

Code:


printf '%s\n' 'g/DROP .*;/ s/.*/\/* & *\//' ',p' | ed -s file.txt

",p" just prints the modified file to stdout. Change it to "w" to write the changes back to the original file.

How to use ed:
http://wiki.bash-hackers.org/howto/edit-ed
http://snap.nlc.dcccd.edu/learn/nlc/ed.html
(also read the info page)

vim works in an nearly identical fashion, and it can use different delimiters (although not my favorite "|", which is reserved as a command separator. :(, so I'm using "@" in my example below).

Code:

printf '%s\n' 'g/DROP .*;/ s@.*@/* & */@' '%p' | vim -es file.txt
Again, change "%p" to "w" to write the changes back to file.

I'm still a bit of a newbie regarding vim though, so I'm not 100% sure I have the best syntax for the command.

chrism01 05-27-2012 06:59 PM

If you're going to use vim, you could use a macro http://vimdoc.sourceforge.net/htmldoc/usr_10.html#10.1


All times are GMT -5. The time now is 06:30 PM.