Visit Our UNIX and Linux User Community


Using varible/varible substitution in Perl/sed Search & Replace


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using varible/varible substitution in Perl/sed Search & Replace
# 1  
Old 05-07-2002
Using varible/varible substitution in Perl/sed Search & Replace

Hi,
I have a program that searches for a particular string patten. I am however having difficulty passing the varible $i (in a loop) as the string pattern to replace. Using either perl or sed search and replace statements, I get the same kinda result. For example, using the perl:

for i in `grep "#" $File`
do
perl -pe s/$i/Not-Implimented/' $File
done


I get no change. Furthermore if I replace the $i with "$i",
it simply writes the new string "Not-Implimented" on every line!

I get no change when using the "$i" variable when using the sed search and replace:
sed 's/"$i"/Not-Imp-lmplimented/g' $File


anyhelp would be appreciated.
Thanks
Smilie
# 2  
Old 05-07-2002
here is a link to some thing that may help.
https://www.unix.com/showthread.php?s...&highlight=sed

You could just as easily use sed instead of perl. Also, your match may be failing. It is possible that when you capture the value for $i that it is not matching properly.

Also I would try executing the sed/perl statement from the command line to see what it does there.

Here is my attempt. Here is my input file and the output after I execute the command line.

Every other line has a # in it...

/root> cat toddfile
# This is a test
This is a test
# This is a test
This is a test
# This is a test
This is a test
# This is a test
This is a test
# This is a test

Here is the execution of the command line and output...

root> sed -e 's/\#/Not-Implimented/' toddfile
Not-Implimented This is a test
This is a test
Not-Implimented This is a test
This is a test
Not-Implimented This is a test
This is a test
Not-Implimented This is a test
This is a test
Not-Implimented This is a test


Now that I look at your script, I think that the "grep" is the problem.

Last edited by Kelam_Magnus; 05-07-2002 at 03:29 PM..
# 3  
Old 05-07-2002
One thing that I noticed is that the problem is if the line returned by grep has spaces in it. Then the list for i is broken down even further.

Example:

I took this simple script that I created for debugging and got the seperated output:

Code:
for i in `grep "#" source.txt`
  do
    echo "i is ${i}."
  done

Output:

Code:
FreeBSD:joeuser:/home/joeuser/sample $ ./imp.sh
i is #.
i is This.
i is is.
i is a.
i is test.
i is #.
i is This.
i is is.
i is a.
i is test.
i is #.
i is This.
i is is.
i is a.
i is test.
i is #.
i is This.
i is is.
i is a.
i is test.
i is #.
i is This.
i is is.
i is a.
i is test.

Is there any way to reset the delimiter for the for loop in a shell? I don't remember off the top of my balding head.
# 4  
Old 05-08-2002
Using varible/varible substitution in Perl/sed search & Replace

Thank you both for your input. Yes, I considered that the grep will execute an iretation of the loop as it encounters a whitespace in the stream following '#', and subsquently, ensured the '#' occurs as one string only (therfore echo $i = #Normal-String).
The reference of '$i' seems to work well in the sed statement, so thanks again. Smilie

Previous Thread | Next Thread
Test Your Knowledge in Computers #55
Difficulty: Easy
The term UPS stands for 'Universal Power Source'.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Aa_app varible in Linux

Hi, I am analyzing linux script, below is the following script line ${AA_APP:-/opt/ty/aa}/sbin/save_file_list ApplEnv.prererun ${AA_APP:-/opt/ty/aa}/sbin/ApplEnv.install_save_list I really dont understand what this line does, can someone explain me. AA_APP is variable... (4 Replies)
Discussion started by: stew
4 Replies

2. Shell Programming and Scripting

awk - how to pass varible

I want to pass variable to below awk statement awk '/abc123/{x=NR+1}(NR<=x){print}' sftp_log_20150317.log I tried -v like below, but its not working. Please help!!! awk -v var1="abc123" '/var1/{x=NR+1}(NR<=x){print}' sftp_log_20150317.log Input file is: sftp_log_20150317.log ... (3 Replies)
Discussion started by: vegasluxor
3 Replies

3. Shell Programming and Scripting

Setting Varible with AWK in KSH

I am trying to set a variable from this AWK command in KSH but I keep getting an error that says my variable cannot be found. LOADNO = $(awk -F"|" 'NR==1{print $2}' file.txt) If I just run awk -F"|" 'NR==1{print $2}' file.txt I get the right value but as soon as I try to assign this... (1 Reply)
Discussion started by: cvigeant
1 Replies

4. UNIX for Dummies Questions & Answers

How to store/read multiple values from a varible

Hi, when I enter 'ps -ef| grep process_name'/'psu | grep process_name', i am getting multiple number of lines output( i mean multiple no of processes).how can i store it one by one and echo it in the same way(one by one). part of script is var1=$(remsh hostname -l username ps -ef|grep... (2 Replies)
Discussion started by: jeanzibbin
2 Replies

5. Shell Programming and Scripting

How to substitute a varible in script having value including quotes?

Hi All, We need to run a Connect direct script on Unix server to send a file to Mainframe server and at mainframe end there need to run another job through Runtask with some parameters need to be passed from C:D (unix) to mainframe. My question is I have to pass parameters like DSN and FNAME as... (2 Replies)
Discussion started by: matrix001
2 Replies

6. Shell Programming and Scripting

BASH Varible nesting and user input

Well, I think I've managed to take two different issues and conglomerate them into and embarrasing mess. #!/bin/bash # Set some variables dir1=/path/that/isnt/variable/$variabledir/dir/ dir2=/path/that/isnt/variable/$variabledir/important/"$variabledir"-subdirectory/path/ echo "Gimme... (7 Replies)
Discussion started by: karlp
7 Replies

7. Shell Programming and Scripting

Perl: varible-sized arrays?

How do you store strings in a variable-sized array? Background: I wrote a program earlier today to work with a very large text file. I chose Perl because it lets me do some nice formatting on the text I grab, instead of just using a shell script to con'cat'enate egrep results. The program... (2 Replies)
Discussion started by: CRGreathouse
2 Replies

8. UNIX for Dummies Questions & Answers

Is this a varible type?

Wondering what $@ does - is it a variable of some kind? (1 Reply)
Discussion started by: Jayden
1 Replies

9. Shell Programming and Scripting

Space in varible?

Hey guys I have here what I am sure amounts to a pretty dumb question.... how do I assign a value of say 999 to a variable called "random number" (note the space between random and number). What would the script look like using borne shell? Thanks! :) (4 Replies)
Discussion started by: pattingtonjbear
4 Replies

10. Shell Programming and Scripting

How to write a varible into file in ksh

set filename $logDir/PyLog/$logname echo $filename >> logname.txt I am trying to write to write the varibale filename into a file logname.txt,it is not working could any one suggest y (2 Replies)
Discussion started by: nathgopi214
2 Replies

Featured Tech Videos