LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 05-25-2012, 04:20 AM   #1
agarrett5
LQ Newbie
 
Registered: May 2012
Posts: 16

Rep: Reputation: Disabled
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
 
Old 05-25-2012, 04:25 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,110

Rep: Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268
I suggest you to use awk:
awk ' /DROP/ { print all your comments before DROP }
{ print } ' inputfile > outputfile
 
Old 05-25-2012, 04:32 AM   #3
cliffordw
Member
 
Registered: Jan 2012
Location: South Africa
Posts: 481

Rep: Reputation: 179Reputation: 179
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!
 
Old 05-25-2012, 04:45 AM   #4
agarrett5
LQ Newbie
 
Registered: May 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
Thankyou Clifford, I will try that. You are correct I do want to comment out the drop command in an sql file
 
Old 05-25-2012, 05:06 AM   #5
agarrett5
LQ Newbie
 
Registered: May 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
That worked well, thankyou
 
Old 05-25-2012, 11:19 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by cliffordw View Post
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.

Last edited by David the H.; 05-25-2012 at 11:27 AM. Reason: sounded too serious, so I smiley'd it.
 
1 members found this post helpful.
Old 05-27-2012, 11:38 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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.

Last edited by David the H.; 05-27-2012 at 11:51 AM. Reason: minor fixes to the code
 
Old 05-27-2012, 07:59 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

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


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
sed add data to the back of a particular line in a text file gengwei89 Linux - Newbie 1 11-06-2011 05:31 AM
using sed to replace text on one line in a text file vo1pwf Linux - Newbie 5 06-24-2009 08:54 AM
what is the command to add text into a text file? newbiebash Programming 2 09-08-2008 09:07 AM
How to add a text to first line of a text file? oskeewow Linux - Newbie 6 04-23-2008 01:40 PM
SED - display text on specific line of text file 3saul Linux - Software 3 12-29-2005 05:32 PM


All times are GMT -5. The time now is 02:48 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration