views:

40

answers:

1

I need a way to read and delete text between two different strings found in some file, then delete the two strings. Like a "cut command." I would like to have the text stored in a variable.

I saw the post about reading text between two strings, but I could not figure out how to delete it as well.

I intend to execute the stored text in bash. Efficiency is desirable. This script is not going to be used on large files, but it may be executed many times sequentially so the faster the script works the better.

The stored text will usually have special characters.

Thanks

A: 

Specify the beginning and ending strings via the environment, and the file to use on the perl command line:

export START_STRING='abc def'
export END_STRING='ghi jkl'
perl -0777 -i -wpe's/\Q$ENV{START_STRING}\E(.*)\Q$ENV{END_STRING}\E/s;print STDERR $1' file_to_use 2>savedtext
ysth
when I type that in command prompt it is not saving the text into savedtext. When I type echo $savedtext I get a blank space. It does however delete the correct text--but only if string1 is NOT the first word. If string1 IS the first word then it deletes everything, but still no savedtext.
Feynman
@Feynman: the $1 is the special character w/ the stored text
vol7ron
Better example: `$text="foostringbar"; $text =~ s/foo(.*?)bar//i; $cut=$1; print $cut;`
vol7ron
@Feynman: perl version, OS? Can you paste the actual command that you ran that doesn't seem to work?
ysth
Perl version: 5.10.0, OS: Debian.Gladly. It's actually part of a shell script (I am so used to making everything a shell script I forget I can compile it with perl directly).#!/bin/bashperl -0777 -i -wpe's/$ARGV[0](.*)$ARGV[1]//s;print STDERR $1' $3 2>savetextActually, just having the module execute whatever is between the strings as a shell script rather than saving it as a variable would be ideal. Saving is a useful tool, but not directly what I need.
Feynman
And vol7ron--how to I get $text to be the text of a file rather than an input? I gave putting the file name in <> but as you probably would have guessed that did not work.
Feynman
ah; I was using the command line argument for the filename to change; that's incompatible with using them for the strings to find. can you have those use environment variables? also, if they may have meta characters that need quoting, do: `s/\Q$ENV{START_STRING}\E(.*)\Q$ENV{END_STRING}\E/...`
ysth
what comes after the ... ? Bash does not like it if I past ins/\Q$ENV{START_STRING}\E(.*)\Q$ENV{END_STRING}\E/...It doesn't like the "..." I understand that is a placeholder for something obvious. Unfortunately, I do not know much perl. I am just getting the necessary functions for my program.
Feynman
… means same as above, starting with `print`
daxim
nope. It says next token ???#!/bin/perls/\Q$ENV{this better work}\E(.*)\Q$ENV{end of script}\E/print STDERR $1 Desktop/test 2>savedtext
Feynman
@Feynman: that's not right; updating my answer with a full example
ysth