Solved Sed question

Hello,

From googleing, and trial & error, I found out how to use sed to place a # sign at the beginning of a line while doing it inline.

Code:
sed -i .bak 's/searchstring/#&/' file

However, I was wondering if I could replace the searchstring with reading (line by line) the contents of a file.

I found out that sed can read an entire file with the r command. But this doesn't seem like what I want.

I was wondering if I could use sed to read the contents of a file line by line and add a # at the beginning of that line (in a succinct one line).

Side note:
I found that sed can use variables, so I could just write a script that would use while to read the contents of a file and use a variable instead of the searchstring. But was hoping to just use sed.

Thanks in advanced.
 
Thanks,

I guess I was over complicating it. If I read a file line by line, I could just edit it in place with your sed statement. That way it will only modify the lines I need. (I only need to modify certain lines, not the whole file)

I apologize!!
 
For a solution (if I understand what you are trying to do correctly), in place of a file that has the desired search strings like this:
Code:
searchstring1
searchstring2
searchstring3
create a "sed command file" that looks like this:
Code:
/searchstring1/s/^/#/
/searchstring2/s/^/#/
/searchstring3/s/^/#/
Then run the command sed -f <sed command file> <file-to-search-and-modify>
sed will read each line of the <file-to-search-and-modify>, and apply each of the editing commands in the <sed command file> to it.

/searchstring1/s/^/#/ translated to words means "for any line containing 'searchstring1', substitute/insert at the start of the line a '#' character".

The search will be case-sensitive by default.
 
Hello,

From googleing, and trial & error, I found out how to use sed to place a # sign at the beginning of a line while doing it inline.


http://sed.sourceforge.net/ Dokumentacija, skripte, linkovi i ostalo (web stranica generisana sed skriptom!)
http://www.grymoire.com/Unix/Sed.html Sed - An Introduction and Tutorial
http://www.tty1.net/sed-intro_en.html sed introduction
http://main.rtfiber.com.tw/~changyj/sed/ Sed and Regular Expresion
http://www.ptug.org/sed/sedfaq.htm THE SED FAQ Last-modified: 1999/07/18
http://legolas.mdh.se/~dat95abs/sed_tutorial.txt Do it with Sed

Famous Sed Oneliners Explained:

http://www.catonmat.net/blog/sed-one-liners-explained-part-one/ Part I: File Spacing, Numbering and Text Conversion and Substitution
http://www.catonmat.net/blog/sed-one-liners-explained-part-two/ Part II: Selective Printing of Certain Lines
http://www.catonmat.net/?s=sed+oneliners Part III: Selective Deletion of Certain Lines and Special Applications


whole book

Sed & Awk, 1st ed. by Dougherty, Dale and Robbins, Arnold. Make sure you get the first edition if you care about the sed. The second edition of the book is written after Dale passed away and is much worse (sed part) while Aho's book is much better book for AWK anyway (actually Aho's book is my favorite computer language book and AWK is my favorite programming language).
 
Thank you all for the suggestions, even the tip on the links and book. .. I will need to see if I can do something like:

sed -Ei .bak '/$LINE/ s/^/#/' file

where $LINE is the output a while lead statement, if not, then I think I have enough to do the file. However, I think the single quotes will mess up the variable, but I will do some testing.
There be hundreds of lines to process on, and if I can do the $LINE, it will make it so I just have to modify the file that the while loop reads. (I believe), instead of making a custom sed command file each time. Though I really appreciate knowing many different ways of accomplishing things. :)
 
There be hundreds of lines to process on, and if I can do the $LINE, it will make it so I just have to modify the file that the while loop reads. (I believe), instead of making a custom sed command file each time.
A sed command file is not limited to just a single sed command; it can contain hundreds of editing commands (each on a separate line) , and each command will be applied in the order they appear to each and every line of the file you're trying to edit. The sed command file becomes the while loop and the file that the while loop reads. sed itself just runs one time, reads the command file one time to figure out what edits are to take place and then reads the data file you are applying the edits to from start to finish one time.

Another nice thing about using a command file is that you have to worry less about watching out for those shell special characters, because it's sed reading the file, not the shell.
 
Back
Top