Programming :: Perl: Find Variable Text Between Keywords And Replace W/ "term_<variable_text>_term"?
Jul 12, 2010
I am trying to write a Perl script that can open a file, find text that appears between two identifying strings (for now, "start" and "end"), then modify that text by enclosing it between "term_" and "_term" . Since the identified strings vary, the replacement string becomes "term_$1_term". From looking at other threads in this forum I've been able to get as far as spitting out the modified terms using the following code:
open FILE, "start2.txt" || die ("Could not open file <br> $!");
$text = <FILE>;
while ($text=~ s/start (.*?) end//) {
[code]....
The problem is how to get "term_$1_term" into the file in the same while loop, which I'm guessing would be some of variant of "$text=~ s/$1/$term/;" (which doesn't work as it stands).
View 6 Replies
ADVERTISEMENT
Aug 27, 2010
What I have works, but wondering what is the 'right' way to replace the digits with the letters given in this loop? somehow use a case or multiple sed? i thought of a multiple sed or a case but couldn't get it to work
Code:
# ...
bcv=$(echo $line | awk -F" " '{ print $1 }' | sed 's/1/q/g;s/2/w/g;s/3/e/g') # and so on
Code:
while read line
do
bcv=$(echo $line | awk -F" " '{ print $1 }')
if [ $bcv == "" ]
[code].....
View 12 Replies
View Related
Feb 25, 2011
Code:
#!/bin/bash
VARR=`cat /proc/asound/cards | grep HDMI | cut -c 1-2`
VARX="defaults.ctl.card $VARR"
VARY="defaults.pcm.card $VARR"
FILE1="alsa"
FILE2="alsa.new"
echo $VARX
echo $VARY
sed "s/defaults.ctl.card*/'$VARX'/g" $FILE1 > $FILE2
This is what I have right now. Well, I thought I knew sed, and apparently I don't... I tried writing this for someone else, and this has given me trouble, so since the user pretty much figured it out on his own, here it goes.
Say VARR=1, so VARX and VARY contain the above text, appended by 1.
What I am trying to do is replace the text "defaults.ctl.card 0" by VARX and "defaults.pcm.card 0" by VARY. The contents of FILE1 is the file being used to search for both text fields, and FILE2 is the output file. I tried using single quotes, double quotes, and a mixture of both, and no go whatsoever. So my question... What is the proper way of searching for text within a file and replacing with a variable?
View 5 Replies
View Related
Feb 14, 2011
I wanted to find and replace a string from a perl file. I have written a script in bash which runs the following command.
perl -pi -e "s/$findstring/$replacestring/" testfile
where as $findstring = print F_WC_TMP"$line
";
and $replaceString = $line = join ' ', split ' ', $line; print F_WC_TMP"$line
";
But when I am running the above command, i think it is replacing the $findstring with the above mentioned string and hence it contains a $line, it is looking for the variable $line and not finding the exact string. I am confused about how to search for a string that contains $ in it and replace it with another $string.
View 5 Replies
View Related
Aug 13, 2010
I have a sysctl.conf that has the following in it:
kernel.exec-shield-randomize = 1
kernel.exec-shield = 1
When I grep kernel.exec-shield I get both line, hence I keep over writing the kernel.exec-shield-randomize in my script because it finds them both for my sed commend.
How can I get an exact match with either sed/awk/grep in shell so I can do a find and replace?
Example: sed 's/^kernel.exec-shield =.*/kernel.exec-shield = 1/g' /etc/sysctl.conf will replace BOTH lines
Example: grep "^kernel.exec-shield" find both line and I want it to find only the exact line.
View 3 Replies
View Related
Jul 28, 2009
I have script that I'm working on that updates a username in all the files that are called blah.inc for my framework. since i host a bunch of these web apps i need to do it to all of them. so I need to figure out how to update these files automagically with out me watching it to call vim every time. heres what I have so far
Code:
This finds the files but now i need to figure out how to do s/bob/fred/g on those files.
View 5 Replies
View Related
Jun 21, 2010
on creating a new perl script which replace IP address from the text file. eg. If in a file, we found any word like 11.222.333.44 then it has to be replaced to XX.XXX.333.44
View 8 Replies
View Related
Oct 11, 2010
I found this command that works great finding and replacing a simple string to another in files located in that folder and all sub-folders.
Code: find . -name '*.php' | xargs perl -pi -e 's/OldText/NewText/g'
The problem I have is that I need to replace a more complex string, like this: Old string: /mnt/stor6-wc2-dfw1/627896/982574/ New string: /mnt/stor8-wc2-dfw1/369587/302589/ There I don't know how to do it... since the / is what separates the old from the new strings, and the strings that I want to replace have / in it. Also, I would like to know how to specify under what folder replace the files, for example, I want that it search/replaces all files under /var/www/mysite/htdocs folder.
View 1 Replies
View Related
Jun 15, 2011
I have one perl sctipt which generate cfg file with ^M in all the EOL. I need to remove the ^M from the cfg file. How can i remove that from the perl script itself. I have tried the following unix command in perl script,
system("perl -pi -e 's/^M//g' *.cfg");
It doesn't work.
View 5 Replies
View Related
May 2, 2010
i am on processing text tasks And i found that if you assign a text to a variable is chomp'ed automatically the newline
Code:
variable=$(cat file.txt)
The problem is i can only access the items/lines using:
Code:
for line in $variable
do
echo $line
# Other commands
done
how do i convert this to an indexed array. More importantly, how do i get access to individual $line[0], ..., $line[n] Another thing, if the file.txt, has lines with spaces it is a mess using the for...in..., but echoing prints line by line...o_0
View 11 Replies
View Related
Jun 30, 2011
I have question about replacing a string in a file.
How can I replace the printerb's 10.1.1.1 to something else(10.1.1.2, for example) without replace printera's 10.1.1.1 accidentally?
I have tried perl -e -pi "s/10.1.1.1/10.1.1.2/g" /etc/hosts. but, perl replace both 10.1.1.1 to 10.1.1.2.
View 13 Replies
View Related
Apr 18, 2009
I have a program I am writing where I have a for loop and I want to make it substitute the variable twice like:
for ((i=0;i<5;i++)) do
echo $"$i"
done
[code]...
View 3 Replies
View Related
Jan 17, 2010
I was wondering if you insert a variable into a regex in Perl, will the contents of the variable be expanded by the regex engine?
View 14 Replies
View Related
Sep 20, 2010
I'm having a hard time googling for answers because I keep using the word "keyword."
I've never really used any kind of database before, but I want to learn the basics, so I'm playing with it a little bit at work. What I want to do is make a database to hold information on various articles. For example, with columns for the title, author, and date. Those are all easy. The tricky part is that I'd also like to be able to label articles with keywords so I can search for certain topics.
Since there's no way to know ahead of time how many keywords, if any, each article might warrant, I'm not sure how to put this in database form. There's no variable-sized array data type. It seems sloppy to just list them all in a single VARCHAR column. Making several columns that may or may not be left null and putting one keyword in each seems wasteful.
View 2 Replies
View Related
Jan 9, 2010
how do you include a string variable as part of a regex in Perl?
View 5 Replies
View Related
Jun 20, 2010
There is the Archive::Zip I think I can use with Perl 5.10 but I don't know how. I don't want to read or write any files, just zip something in memory, with best compression, like
$text = "this is a test";
$zippedtext = &Zip($text);
sub Zip {
[code]...
I guess it's only a few lines.
View 3 Replies
View Related
Aug 27, 2010
How can I to pass a perl variable into html input tag? For example, If a have got a cgi script:
Quote:
use CGI;
use DBI;
my $owner = $cgi->param('owner');
[code]....
How can I to pass $owner variable?
View 1 Replies
View Related
Jun 12, 2013
I have a directory of orchestral music .ogg files from a family member. Each track is from a different artist and the CDDB entry adds a ":" character after the artist name in the track title.
Here is an example of what I am referring to:
Code: Select all13_-_Mozart:_Sonata_in_A_major_KV_331.ogg
I would like to parse file names in any given directory and search for the string Code: Select all: and replace it with Code: Select all_ According to this post on stackoverflow, I can use Perl to accomplish this task. I've tried Code: Select allperl -i.bak -pe 's/:/_/' but since I am still learning Perl I'm probably commiting a PEBKAC error.
How would I go about solving this issue with regular expressions using Perl?
View 3 Replies
View Related
Jul 8, 2010
I have a many directories each with about 20 html files inside. All the files have .html ext. What I'm hoping is possible is from command line to find some text in each one and replace it with some other text.
Basically what I want to replace is;
/awstats/
with
awstats/
I can do this easily with dreamweaver or some other application but because I have 960 pages total to do I'm hoping to do it this way.
View 6 Replies
View Related
Feb 11, 2011
I have an SQL dump, file.sql that has many references to a particular domain, d1.com. I would like to run a command that can replace every occurrence of d1.com with d2.com. I've tried looking into sed before but the man pages are quite daunting.
View 14 Replies
View Related
Apr 1, 2009
Want to return current LINES=, tryng to avoid the "hacky" backticks and string manipulation way
ie: my $somevar = `set | grep LINES`
I tried using the env command and it returns a empty value?
my $lines = $ENV{'LINES'};
$= doesnt work well for putty sessions...
my $lines = $=;
View 6 Replies
View Related
Jul 22, 2011
I'm reading "OReilly Learning Perl 5th Edition", and there are such words:Code:You can use an array element like $fred[2] in every place? where you could use any other scalavariable like $fred.At the bottom of the page, it explains the ? like this:Code:The most notable exception is that the control variable of a foreach loop, which you?ll see later in this chapter, must be a simple scalar.Since Perl has the save-and-restore mechanism for the control variable, why an array element can't be used as the control variable
View 1 Replies
View Related
Apr 3, 2010
I'm having problems with Tomboy. I have a few hundred note files and I need to go through all of them and replace all instances of "<link:broken>a</link:broken>" with "a". Is there a bash command I can use to do this?
View 2 Replies
View Related
Aug 28, 2010
I'm pretty sure this is doable from the command line, but my CLI skills have degraded a lot since my pre-Y2K admin days. The goal is to search all the files in the directory for a very long string of text and replace it with another string of text. The text being searched for is my Google Adsense code (which will be stripped from my website) and it will be replaced with a placeholder so I can easily tack something else in there in the future.
Seeing how I have that long snip of code on about 100 pages, automating the process would make life easier.
If I was searching for a single word, I can see ways to do this.
If I paste the code I'm searching for into a text file, is there a way to:
find (contents of oldstring.txt) and replace with (contents of newstring.txt)?
View 9 Replies
View Related
Feb 24, 2011
i'm trying to script a lil piece to execute in terminal so when i type something like:
scriptname textfile
the script searches the text file, replaces charecters of my choosing with charecters of my choosing
"sed" im told is the way to go, and i got summat like this going
Code:
#!/bin.sh
bash -c "sed -i.backup -e /s/char/newchar/"
(if need be, i think i can just add another line of "-e /s/char/newchar/" if i need to target more charecters or words as needed.) the issue with the above code. . . how do i get it to target whatever text file follows the command? without having to manually designate sed to it each time? e.g. in commanding:
scriptname textfile
how do i get the script to target textfile^^^?
View 4 Replies
View Related
Mar 6, 2011
I have a few problem. I have txt file like this:Quote:00 21 55 84 9a ff 00 1f 9e 1a 5b 00 08 00 45 00 00 4b 00 00 40 00 3f 11 9a 0e a1 8b fa 02 04 02Then, based on my txt file, I would like to generate text like this:Quote:00215584 2155849a 55849aff 849aff00 9aff001f ff001f9e 001f9e1a 1f9e1a5b 9e1a5b00 1a5b0008 5b000800 00080045 08004500 00450000 00004b00 004b0000 4b000040 00004000 0040003f 40003f11 003f119a 3f119a0e 119a0ea1 9a0ea18b 0ea18bfa a18bfa02ased in my reading, I found about ngram solution in perlbut I not really understand to edit from source code given. I m begineer user in programming language. I hope to get the solution. [URL]
View 1 Replies
View Related
Feb 13, 2010
The output of following code is not like it's intended ...
Code:
This is the output:
Code:
Test prepending ...apple is a nice word, hour is a nice word, But of course what I want to do in the first set of commands is to prepend the word "an" to the words "apple" and "hour" in the for-loop.
View 4 Replies
View Related
Jun 21, 2010
How can I delete rows into text file in perl?
View 4 Replies
View Related
Apr 8, 2010
I've been trying to figure out a way to more easily color text in Perl like I do on Bash on a Linux box. In bash, what I'll do is set color variables up to equal the escape sequence, then echo out with escape seqeunces to print it exactly how I want it. Typically I'll want a character or a word in a different color, not the whole line. For example
echo -n -e "My face is turning ${RED}red${UNCOLOR} like a lobster." In Perl with the term::ANSIColor module, it seems to just do a line. Am I being dense? Is there a way that I can do it like I do it in BASH that's fairly easy to read after the fact?
View 11 Replies
View Related
Jan 5, 2011
I want to get the last term of an unknown-sized text line, by usig the awk command. Is there any variable which points to the last term of a text line ? (obviously, y can obtain that term if i knew how many terms does the line have... i.e. awk '{print $12}' ; if we are talking about a 12-terms text line) I wonder if there is an option like $?, to get the last term.
View 2 Replies
View Related