Replace text in input file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace text in input file
# 1  
Old 04-23-2008
PHP Replace text in input file

I wish to replace values of specific parameters in an input file for batch runs of a java code. It's essentially a nested for-loop sorta like this:

valuearray1 contains values for param1
valuearray2 contains values for param2

for (all values in valuearray1)
go into specific position in "input.txt" (marked by say character pos1) and replace param1 with current value from valuearray1
for (all values in valuearray2)
go into specific position in "input.txt" (marked by say character pos2) and replace param2 with current value from valuearray2
run the simulation code
get next value from valuearray2
get next value from valuearray1
DONE!

I'm new to scripting so any help from the experts would be deeply appreciated!

-Sam
# 2  
Old 04-24-2008
Where do the arrays get their data from? Is it in a file or do you store it in the script?

What does "marked by character x" mean? Like find a line with the specified character at the beginning and only substitute on that particular line? Or all lines following that line? Or all lines with that marking?

Does input.txt need to change on every iteration or can the java program read it on standard input? I would suggest to keep a static file and run each java program on a temporary copy.

Code:
#!/bin/sh

valuearray1="foo bar baz"
valuearray2="ick y poo"

for v1 in $valuearray1; do
  for v2 in $valuearray2; do
    sed -e "/^pos1/s/param1/$v1/" -e "/^pos2/s/param2/$v2/" input.txt >/tmp/input.txt
    java suction /tmp/input.txt
  done
done

# 3  
Old 04-25-2008
You've got it, the code works beautifully after some changes here and there. Thx!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Replace character string in txt file using input file(S)

Hi I have a large txt file on my AIX server and I need to replace some text using two other files. So filename1 has about 500 lines similar to: txtcode SYStem100 I have the string I want to change in string2 and the new stringname in string3. Does anyone know a way of doing this? I have... (1 Reply)
Discussion started by: Grueben
1 Replies

2. Shell Programming and Scripting

Replace string - searching from input file

Hi I need help with writing a script to change a string in a file. The script needs to read an input list (list.txt) file line by line searching for that string in a text.file. Once the string is found the last few words in the string should be replaced. eg list.txt will contain hello my... (6 Replies)
Discussion started by: sudobash
6 Replies

3. Shell Programming and Scripting

Avoid overwriting backup file when multiple entries need to replace in one file input from another

Hello, I have been working on script in which search and replace the multiple pattern. 1. update_params.sh read the multiple pattern from input file ParamMapping.txt(old_entry|New_entry) and passing this values one by one to change_text.sh 2. change_text.sh read... (0 Replies)
Discussion started by: ketanraut
0 Replies

4. Linux

Search a template file and replace with input

Hi I have a CommonTemplateStop.template file . Inside the file i need to replace the variables DepName and CompInsName with the values(Trade and TradeIns) specified in the script. I have written the below .sh script in linux server which will read the .template file and has to replace the 2... (8 Replies)
Discussion started by: samrat dutta
8 Replies

5. Shell Programming and Scripting

Replace two values in a file with input from two different files

Hi, I was having the following issue cat input hello1, my name is unix.com. I am awesome. Hope you know this, hello2! cat hello1.txt Hi Friends Hi Folks Hi Well-Wishers cat hello2.txt Honey Sweety Darling Required Output (8 Replies)
Discussion started by: jacobs.smith
8 Replies

6. Shell Programming and Scripting

awk + gsub to search multiple input values & replace with located string + extra text

Hi all. I have the following command that is successfully searching for any one of the strings on all lines of a file and replacing it with the instructed value. cat inputFile | awk '{gsub(/aaa|bbb|ccc|ddd/,"1234")}1' > outputFile This does in fact replace any occurrence of aaa, bbb,... (2 Replies)
Discussion started by: dazhoop
2 Replies

7. Shell Programming and Scripting

find and replace in a directory using an input file

Hi folks, I need help to finish this script please. see below: I have an input file with all the IP address to names formated like so in a txt file addnsr1pri 166.7.3.105 addnsr1sec 166.2.100.22 addnsr2pri 166.2.220.121 addnsr2sec 166.3.68.45... (12 Replies)
Discussion started by: richsark
12 Replies

8. Shell Programming and Scripting

sed replace characters in next line with input from a file

Hi, I have a set of strings in filea. I want to search string xyz in fileb and replace next line in file b with the content from filea. #cat filea abc def ghi #cat fileb asdkjdslka sajljskdjoi xyzjjjjkko aaaaaaaa bbbbbbbb cccccccc xyzsdsajd dddddddd eeeeeeee (2 Replies)
Discussion started by: anilvk
2 Replies

9. Homework & Coursework Questions

[Scripting]Find & replace using user input then replacing text after

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: (o) Checkout an auto part: should prompt the user for the name of the auto part and borrower's name: Name:... (2 Replies)
Discussion started by: SlapnutsGT
2 Replies

10. UNIX for Dummies Questions & Answers

search and replace a specific text in text file?

I have a text file with following content (3 lines) filename : output.txt first line:12/12/2008 second line:12/12/2008 third line:Y I would like to know how we can replace 'Y' with 'N' in the 3rd line keeping 1st and 2nd lines same as what it was before. I tried using cat output.txt... (4 Replies)
Discussion started by: santosham
4 Replies
Login or Register to Ask a Question