|
Prev: Passing shell variable to awk that contains spaces
Next: simple sed help - delete word containing + (plus sign)
From: JerryB on 16 Jan 2006 23:10 Hi, I have a file like this line1 line2 line3 (two new lines between lines). I want to replace those with something that I know does not exist in the file, in this case, something like the string 'SOMEMARKERHERE': line1SOMEMARKERHEREline2SOMEMARKERHEREline3 The, I plan to replace SOMEMARKERHERE with </p>\n\n<p>, so I get: line1 </p> <p>line 2</p> <p>line 3</p> All I need to do is to add <p> at the beginning of line 1 and I have beautifully formatted xhtml. The question: how do I replace 2 newlines with SOMEMARKERHERE? Thanks, Jerry.
From: Chris F.A. Johnson on 16 Jan 2006 23:32 On 2006-01-17, JerryB wrote: > Hi, > I have a file like this > > line1 > > line2 > > line3 > (two new lines between lines). > I want to replace those with something that I know does not exist in > the file, in this case, something like the string 'SOMEMARKERHERE': > > line1SOMEMARKERHEREline2SOMEMARKERHEREline3 > > The, I plan to replace SOMEMARKERHERE with </p>\n\n<p>, so I get: > line1 </p> > ><p>line 2</p> > ><p>line 3</p> > > All I need to do is to add <p> at the beginning of line 1 and I have > beautifully formatted xhtml. > The question: how do I replace 2 newlines with SOMEMARKERHERE? If you want to convert line1 line2 line3 To <p>line1</p> <p>line2</p> <p>line3</p> why not just do that? awk '/./ { $0 = "<p>" $0 "</p>" } { print }' FILE > FILE.html -- Chris F.A. Johnson, author | <http://cfaj.freeshell.org> Shell Scripting Recipes: | My code in this post, if any, A Problem-Solution Approach | is released under the 2005, Apress | GNU General Public Licence
From: Xicheng on 17 Jan 2006 00:03 JerryB wrote: > Hi, > I have a file like this > line1 > > line2 > > line3 > (two new lines between lines). > I want to replace those with something that I know does not exist in > the file, in this case, something like the string 'SOMEMARKERHERE': > > line1SOMEMARKERHEREline2SOMEMARKERHEREline3 > > The, I plan to replace SOMEMARKERHERE with </p>\n\n<p>, so I get: > line1 </p> > > <p>line 2</p> > > <p>line 3</p> > All I need to do is to add <p> at the beginning of line 1 and I have > beautifully formatted xhtml. > The question: how do I replace 2 newlines with SOMEMARKERHERE? use Perl, you can slurp in the file contents all at once and then replace all patterns \n\n(or any SOMEMARKERHEREs) with </p>\n\n<p>: perl -p00e 's{\n\n}{</p>\n\n<p>}g' file.txt you get=== line1</p> <p>line2</p> <p>line3 === or replace the pattern line by line as Chris said: perl -pe 's{(.*)}{<p>$1</p>} if /\S+/' file.txt you get=== <p>line1</p> <p>line2</p> <p>line3 </p> === Xicheng
From: Ed Morton on 17 Jan 2006 00:04 JerryB wrote: > Hi, > I have a file like this > > line1 > > line2 > > line3 > (two new lines between lines). > I want to replace those with something that I know does not exist in > the file, in this case, something like the string 'SOMEMARKERHERE': > > line1SOMEMARKERHEREline2SOMEMARKERHEREline3 > > The, I plan to replace SOMEMARKERHERE with </p>\n\n<p>, so I get: > line1 </p> > > <p>line 2</p> > > <p>line 3</p> > > All I need to do is to add <p> at the beginning of line 1 and I have > beautifully formatted xhtml. > The question: how do I replace 2 newlines with SOMEMARKERHERE? gawk -vRS= -vORS=SOMEMARKERHERE 1 file but it seems pointless to do it in 2 phases when you could just do either of these: gawk -vRS= -vORS="</p>\n\n" '$0="<p>"$0' file gawk -vRS= '{print "<p>" $0 "</p>\n"}' file or various other solutions... Regards, Ed.
From: Michael Paoli on 17 Jan 2006 04:49
JerryB wrote: > I have a file like this > > line1 > > line2 > > line3 > (two new lines between lines). > I want to replace those with something that I know does not exist in > the file, in this case, something like the string 'SOMEMARKERHERE': > line1SOMEMARKERHEREline2SOMEMARKERHEREline3 > The, I plan to replace SOMEMARKERHERE with </p>\n\n<p>, so I get: > line1 </p> > <p>line 2</p> > <p>line 3</p> > All I need to do is to add <p> at the beginning of line 1 and I have > beautifully formatted xhtml. > The question: how do I replace 2 newlines with SOMEMARKERHERE? I don't see the function of the intermediary (SOMEMARKERHERE) step. With sed, you could use something roughly like: sed -e '/^$/d;s/.*/<p>&<\/p>/' .... that would remove the empty lines, and put <p> and </p> around the non-empty lines. Someone already responded with an awk example. It could also likewise be done with ed, but classic versions of ed need to work on a file. E.g., with ed: ed file << \__EOT__ g/^$/d 1,$s/.*/<p>&<\/p>/ w q __EOT__ tr wouldn't be so useful - at least by itself. |