How to Use a Multidimensional Array to SED?

AI Thread Summary
The discussion revolves around a user attempting to process a 9x2 array from a .dat file using a Bash script and SED to generate an input file for an external program. The user is facing an error related to an "unterminated 's'" in SED while trying to replace placeholders in a template file with values from the array. Key points include the user's initial approach of reading the .dat file into an array and using a for-loop to iterate through the values. Suggestions from other participants highlight that the loop condition should be adjusted from "i <= 9" to "i < 9" to match the number of rows in the array. Additionally, there is a recommendation to check the indexing of the array elements, as they may need to be zero-based instead of one-based. Despite these suggestions, the user reports that the issue persists, indicating a need for further troubleshooting in the script.
ecastro
Messages
249
Reaction score
8
TL;DR Summary
How to apply each element of a multi-dimensional array to an SED in Bash script?
I have a .dat file which contains an ##m \times n## (specifically, a ##9 \times 2##) array and I have a file which has this kind of format,
Bash:
variable_x xx
variable_y yy
where xx and yy are numbers (I'll call this file the input_file). This file serves as an input to an external program, which I shall insert into a for-loop. So, in every iteration, the input_file will have different values and will be processed by the external program. However, the external program needs to be run in a Bash script, which I am not familiar.

I have known that by simply replacing xx and yy by a string and saving it into a sort of template input_file I could use SED to replace the string by the elements of the .dat file. My currently working example would be:
Code:
input=`cat dat_file.dat`

for ((i=0;i<=9;i++)); do
    sed "s/xx/${input[$i, 1]}/" -e "s/yy/${input[$i, 2]}/" template_file > input_file
    external_program < input_file >> output_file # calls the external program and returns the output file
done

But I am running through an error in SED saying that I have an unterminated 's'. What am I doing wrong here?

Thank you in advance, and I hope I conveyed my problem clearly.
 
Technology news on Phys.org
ecastro said:
Summary: How to apply each element of a multi-dimensional array to an SED in Bash script?

I have a .dat file which contains an ##m \times n## (specifically, a ##9 \times 2##) array and I have a file which has this kind of format,
Bash:
variable_x xx
variable_y yy
where xx and yy are numbers (I'll call this file the input_file). This file serves as an input to an external program, which I shall insert into a for-loop. So, in every iteration, the input_file will have different values and will be processed by the external program. However, the external program needs to be run in a Bash script, which I am not familiar.

I have known that by simply replacing xx and yy by a string and saving it into a sort of template input_file I could use SED to replace the string by the elements of the .dat file. My currently working example would be:
Code:
input=`cat dat_file.dat`

for ((i=0;i<=9;i++)); do
    sed "s/xx/${input[$i, 1]}/" -e "s/yy/${input[$i, 2]}/" template_file > input_file
    external_program < input_file >> output_file # calls the external program and returns the output file
done

But I am running through an error in SED saying that I have an unterminated 's'. What am I doing wrong here?

Thank you in advance, and I hope I conveyed my problem clearly.
The array in the input file has 9 rows, each with two numbers. The for loop in your code runs 10 times. See if changing the condition part of your loop from i <= 9 to i < 9 fixes your problem.
 
Mark44 said:
The array in the input file has 9 rows, each with two numbers. The for loop in your code runs 10 times. See if changing the condition part of your loop from i <= 9 to i < 9 fixes your problem.

Unfortunately, the problem still persists.
 
Another possibility is that your 2nd indexes are 1-based, but maybe they should be 0-based.

IOW, does changing
sed "s/xx/${input[$i, 1]}/" -e "s/yy/${input[$i, 2]}/"
to
sed "s/xx/${input[$i, 0]}/" -e "s/yy/${input[$i, 1]}/"
fix things?

I'm not knowledgeable with Linux or Bash, but this and my other suggested fix are things that are common to most programming languages.
 
Last edited:
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
I am trying to run an .ipynb file and have installed Miniconda as well as created an environment as such -conda create -n <env_name> python=3.7 ipykernel jupyter I am assuming this is successful as I can activate this environment via the anaconda prompt and following command -conda activate <env_name> Then I downloaded and installed VS code and I am trying to edit an .ipynb file. I want to select a kernel, via VS Code but when I press the button on the upper right corner I am greeted...
Back
Top