Replacing a variable in a loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing a variable in a loop
# 1  
Old 12-06-2012
Replacing a variable in a loop

input.txt

Quote:
data<-read.table("k_file.txt")
Hi, hello
I want to make 3 seprate ouputs such that

toast1.txt

Quote:
data<-read.table("toast1_file.txt")
Hi, hello
ICH.txt
Quote:
data<-read.table("ICH_file.txt")
Hi, hello
ICH_SV.txt
Quote:
data<-read.table("ICH_SV_file.txt")
Hi, hello
I have tried "echo" and "sed 's/k/toast1/g' to replace k, but its not quite working. Please help me out Smilie
Thanks!
# 2  
Old 12-06-2012
Hammer & Screwdriver

Did you try as below..?
Code:
$ sed 's/k/toast1/' input.txt
data<-read.table("toast1_file.txt")
Hi, hello 
$

# 3  
Old 12-06-2012
right, but how do it make it in a loop such that it can do it for

toast1, ICH, ICH_SV

Quote:
for i in toast1 ICH ICH_SV
do
sed 's/k/${i}/g' input.txt > ${i}.txt
done
this is the idea, but i can't put ${i} in sed...
# 4  
Old 12-06-2012
Replace with the below sed in the loop and try..
Code:
sed 's/k/'$i'/g' inputfile

This User Gave Thanks to michaelrozar17 For This Post:
# 5  
Old 12-06-2012
i was missing ' ' sign. Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies

2. Shell Programming and Scripting

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now... (5 Replies)
Discussion started by: mrm5102
5 Replies

3. Shell Programming and Scripting

[SHELL: /bin/sh] For loop using variable variable names

Simple enough problem I think, I just can't seem to get it right. The below doesn't work as intended, it's just a function defined in a much larger script: CheckValues() { for field in \ Group_ID \ Group_Title \ Rule_ID \ Rule_Severity \ ... (2 Replies)
Discussion started by: Vryali
2 Replies

4. Shell Programming and Scripting

replacing ' ' and :'s with _'s in a variable

Hi guys In my shell script I have a variable that contains a string that contains the current time. I want to replace the spaces and colons in the value with underscores. TIME=`date` echo $TIME gives me: Thu Sep 24 16:40:53 BST 2009 I want it to show:... (4 Replies)
Discussion started by: alinaqvi90
4 Replies

5. Shell Programming and Scripting

replacing line with variable

All I want to do is replace the 2nd line in a file with a variable, eg, var=xxx the file 'test' containing: aaa bbb ccc replace bbb with xxx aaa xxx ccc I had it working with sed on a redhat machine, but it doesn't work on a mac machine. (4 Replies)
Discussion started by: sideways
4 Replies

6. UNIX for Dummies Questions & Answers

Replacing in a variable

Hi, If I have a variable var1 ='TH 12/1234' How can I set this too in a script var1 ='TH 12~1234' Bearing in mind "/" might be in a difference place each time and that the "/" might not even exist each time the variable is set. Thanks (3 Replies)
Discussion started by: belfastbelle
3 Replies

7. Shell Programming and Scripting

Replacing a pattern using variable?

ip1="xxx" ip2="bbb" sed 's/$ip1/$ip2/g' (3 Replies)
Discussion started by: shivarajM
3 Replies

8. Shell Programming and Scripting

Help in replacing text with the value of a variable

Input file - tmp <begin> ./00003/ ./00004/ <end> I would like to replace "." with the value of pwd so that the output will look like /dev/project/00003/ t=`pwd` sed -e "s/\./$t/g" tmp > tmp1; The above piece of code is not working. Appreciate your help. (4 Replies)
Discussion started by: lotto_123
4 Replies

9. UNIX for Dummies Questions & Answers

Replacing $ in variable

hi I have a variable like k=$DESTDIR/$PKG/$VERSION I want to replace each $ in string k with say "XXX". so that k becomes like this "XXXDESTDIR/XXXPKG/XXXVERSION" when I use echo $k | sed -e "s/\$/XXX" it actually passes expanded of variables $DESTDIR, $PKG and $VERSION to sed. ... (10 Replies)
Discussion started by: ashish_uiit
10 Replies

10. Shell Programming and Scripting

Replacing pattern in variable

My String variable is holding value as - abc"def I want to replce " with \" I tried with awk : echo $var | awk '{gsub(/"/,"\"");print}' and I am getting an error, `)' is not expected. (1 Reply)
Discussion started by: videsh77
1 Replies
Login or Register to Ask a Question