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:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top