How to Use a Multidimensional Array to SED?

In summary: It sounds like you are trying to use a string to replace an element in an array, but you are not using the correct syntax.In summary, the problem is that you are trying to use a string to replace an element in an array, but you are not using the correct syntax.
  • #1
ecastro
254
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
  • #2
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.
 
  • #3
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.
 
  • #4
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:

1. How do I declare a multidimensional array in my code?

To declare a multidimensional array, you can use the following syntax:
arrayName = new datatype[rows][columns];
For example, if you want to create a 3x3 array of integers, you can use the code:
int[][] myArray = new int[3][3];

2. How do I access elements in a multidimensional array?

To access elements in a multidimensional array, you use the index values for the row and column of the element you want to access. For example, to access the element in the second row and third column of a 3x3 array, you would use the code:
myArray[1][2];
Note that array indexes start at 0, so the first row and column would be accessed using indexes 0 and 0.

3. Can I have different data types in a multidimensional array?

Yes, you can have different data types in a multidimensional array. However, each individual array within the multidimensional array must be of the same data type. For example, you can have a multidimensional array with one array of integers and another array of strings, but you cannot have one array with both integers and strings.

4. How do I loop through a multidimensional array?

To loop through a multidimensional array, you can use nested for loops. The outer loop will iterate through the rows, and the inner loop will iterate through the columns. For example:
for(int i = 0; i < myArray.length; i++) {
  for(int j = 0; j < myArray[i].length; j++) {
   //Code to access and manipulate each element
  }
}

5. What is the advantage of using a multidimensional array over a one-dimensional array?

A multidimensional array allows you to store data in a grid-like structure, making it easier to organize and access data. It also allows you to represent more complex data sets. For example, a 2D array can represent a table of data with rows and columns, while a 3D array can represent a cube of data with multiple layers.

Similar threads

  • Programming and Computer Science
Replies
19
Views
1K
  • Programming and Computer Science
Replies
6
Views
977
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
2
Views
635
  • Programming and Computer Science
Replies
22
Views
921
  • Programming and Computer Science
Replies
1
Views
10K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
7
Replies
235
Views
10K
Back
Top