How to Use a Multidimensional Array to SED?

Click For Summary

Discussion Overview

The discussion revolves around the use of a multidimensional array in a Bash script to replace values in a template input file using SED. Participants are exploring how to correctly implement this process while addressing errors encountered in the script.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes their approach to using SED for replacing placeholders in a template input file with values from a .dat file.
  • Another participant points out that the for loop runs 10 times instead of 9, suggesting a change in the loop condition from i <= 9 to i < 9 to potentially resolve the issue.
  • A later reply proposes that the issue might stem from the indexing of the array, suggesting that the second indexes could be 1-based instead of 0-based, and recommends adjusting the SED command accordingly.
  • Despite the suggestions, the original poster indicates that the problem persists, indicating ongoing uncertainty about the solution.

Areas of Agreement / Disagreement

Participants have not reached a consensus on the cause of the SED error, and multiple competing views on potential fixes remain unresolved.

Contextual Notes

There is uncertainty regarding the indexing of the array and the correct loop condition, which may depend on the specific implementation details of the Bash script and the format of the .dat file.

ecastro
Messages
249
Reaction score
8
TL;DR
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:

Similar threads

  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 11 ·
Replies
11
Views
3K
Replies
235
Views
14K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 2 ·
Replies
2
Views
11K
Replies
7
Views
3K
Replies
2
Views
2K