Skip to main content
Left closed in review as "Duplicate" by Adrian Mole, z.g.y, rob2universe
added 550 characters in body
Added to review
Source Link
B.Eng
  • 23
  • 4

I am looking to replace an entire line in one .sh file with another .sh file using the sed -i command in bash.

The command pattern sed <line number>s/<search pattern>/<replacement string>/ and I'm replacing the entire line with a replacement string that contains a $ variable.

The first file write.sh contains the information that must be written to the second file insert.sh.

#!/bin/bash

output_file="/random/path/that/should/be/there1/"
error_file="/random/path/that/should/be/there2/"

sed -i '2s/.*/#SBATCH --job-name=775_none/' insert.sh
sed -i '3s/.*/#SBATCH --cpus-per-task=1/' insert.sh
sed -i '4s/.*/#SBATCH --mem=5GB/' insert.sh
sed -i '5s/.*/#SBATCH --output="'$output_file'"/' insert.sh
sed -i '6s/.*/#SBATCH --error="'$error_file'"/' insert.sh
sed -i '7s/.*/#SBATCH --time=5:00:00/' insert.sh

All lines work except for lines 5s and 6s with the error returning:

sed: -e expression #1, char 27: unknown option to `s'
sed: -e expression #1, char 26: unknown option to `s'

The second file insert.sh contains:

#!/bin/bash
#SBATCH --job-name=none-chi-1
#SBATCH --cpus-per-task=1
#SBATCH --mem=5GB
#SBATCH --output=/path/i/want/to/repalce
#SBATCH --error=/path/i/want/to/repalce
#SBATCH --time=8:00:00

and what I would like is

#!/bin/bash
#SBATCH --job-name=775_none
#SBATCH --cpus-per-task=1
#SBATCH --mem=5GB
#SBATCH --output=/random/path/that/should/be/there1/
#SBATCH --error=/random/path/that/should/be/there2/
#SBATCH --time=5:00:00

Some links I have checked: 1, 2, 3

Any assistance would be appreciated if this is possible.

I am looking to replace an entire line in one .sh file with another .sh file using the sed -i command in bash.

The command pattern sed <line number>s/<search pattern>/<replacement string>/ and I'm replacing the entire line with a replacement string that contains a $ variable.

The first file write.sh contains the information that must be written to the second file insert.sh.

#!/bin/bash

output_file="/random/path/that/should/be/there1/"
error_file="/random/path/that/should/be/there2/"

sed -i '2s/.*/#SBATCH --job-name=775_none/' insert.sh
sed -i '3s/.*/#SBATCH --cpus-per-task=1/' insert.sh
sed -i '4s/.*/#SBATCH --mem=5GB/' insert.sh
sed -i '5s/.*/#SBATCH --output="'$output_file'"/' insert.sh
sed -i '6s/.*/#SBATCH --error="'$error_file'"/' insert.sh
sed -i '7s/.*/#SBATCH --time=5:00:00/' insert.sh

All lines work except for lines 5s and 6s with the error returning:

sed: -e expression #1, char 27: unknown option to `s'
sed: -e expression #1, char 26: unknown option to `s'

Some links I have checked: 1, 2, 3

Any assistance would be appreciated if this is possible.

I am looking to replace an entire line in one .sh file with another .sh file using the sed -i command in bash.

The command pattern sed <line number>s/<search pattern>/<replacement string>/ and I'm replacing the entire line with a replacement string that contains a $ variable.

The first file write.sh contains the information that must be written to the second file insert.sh.

#!/bin/bash

output_file="/random/path/that/should/be/there1/"
error_file="/random/path/that/should/be/there2/"

sed -i '2s/.*/#SBATCH --job-name=775_none/' insert.sh
sed -i '3s/.*/#SBATCH --cpus-per-task=1/' insert.sh
sed -i '4s/.*/#SBATCH --mem=5GB/' insert.sh
sed -i '5s/.*/#SBATCH --output="'$output_file'"/' insert.sh
sed -i '6s/.*/#SBATCH --error="'$error_file'"/' insert.sh
sed -i '7s/.*/#SBATCH --time=5:00:00/' insert.sh

All lines work except for lines 5s and 6s with the error returning:

sed: -e expression #1, char 27: unknown option to `s'
sed: -e expression #1, char 26: unknown option to `s'

The second file insert.sh contains:

#!/bin/bash
#SBATCH --job-name=none-chi-1
#SBATCH --cpus-per-task=1
#SBATCH --mem=5GB
#SBATCH --output=/path/i/want/to/repalce
#SBATCH --error=/path/i/want/to/repalce
#SBATCH --time=8:00:00

and what I would like is

#!/bin/bash
#SBATCH --job-name=775_none
#SBATCH --cpus-per-task=1
#SBATCH --mem=5GB
#SBATCH --output=/random/path/that/should/be/there1/
#SBATCH --error=/random/path/that/should/be/there2/
#SBATCH --time=5:00:00

Some links I have checked: 1, 2, 3

Any assistance would be appreciated if this is possible.

2
tripleee
  • 191.6k
  • 37
  • 317
  • 367
Post Closed as "Duplicate" by tripleee bash
Source Link
B.Eng
  • 23
  • 4

Bash: Using sed -i to replace a line with a $ variable

I am looking to replace an entire line in one .sh file with another .sh file using the sed -i command in bash.

The command pattern sed <line number>s/<search pattern>/<replacement string>/ and I'm replacing the entire line with a replacement string that contains a $ variable.

The first file write.sh contains the information that must be written to the second file insert.sh.

#!/bin/bash

output_file="/random/path/that/should/be/there1/"
error_file="/random/path/that/should/be/there2/"

sed -i '2s/.*/#SBATCH --job-name=775_none/' insert.sh
sed -i '3s/.*/#SBATCH --cpus-per-task=1/' insert.sh
sed -i '4s/.*/#SBATCH --mem=5GB/' insert.sh
sed -i '5s/.*/#SBATCH --output="'$output_file'"/' insert.sh
sed -i '6s/.*/#SBATCH --error="'$error_file'"/' insert.sh
sed -i '7s/.*/#SBATCH --time=5:00:00/' insert.sh

All lines work except for lines 5s and 6s with the error returning:

sed: -e expression #1, char 27: unknown option to `s'
sed: -e expression #1, char 26: unknown option to `s'

Some links I have checked: 1, 2, 3

Any assistance would be appreciated if this is possible.