How to Use a Multidimensional Array to SED?

Click For Summary
SUMMARY

This discussion focuses on using a multidimensional array in a Bash script to replace placeholders in a template file using SED. The user is attempting to read a 9x2 array from a .dat file and replace 'xx' and 'yy' in a template file with corresponding values from this array. The primary issue encountered is an "unterminated 's'" error in SED, which is likely due to incorrect indexing or an off-by-one error in the loop condition. Suggested solutions include adjusting the loop to iterate from 0 to 8 and ensuring that array indexing is zero-based.

PREREQUISITES
  • Understanding of Bash scripting
  • Familiarity with SED syntax
  • Knowledge of array indexing in programming
  • Basic file I/O operations in Linux
NEXT STEPS
  • Learn about Bash array indexing and how it differs between languages
  • Study SED command options and common errors
  • Explore loop constructs in Bash for better control flow
  • Review file handling techniques in Bash for effective input/output management
USEFUL FOR

This discussion is beneficial for Bash script developers, Linux users working with text processing, and anyone needing to manipulate files using SED and arrays in a scripting context.

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