Skip to main content
added 20 characters in body
Source Link

I have two files of code, the first (File1) including a few lines of text to be included and the second (File2) including strings that will be replaced with a range of lines from the first file. The lines included from the first file will also need the following string appended to each of the lines when they are included in the second file (not including the quotations): " & ". In addition, a search and replace for some of the strings from the first file (File1) needs to be replaced in the output file (File3). An example of the first two files is included below.

File1

A A
B B
C C
D D
N 1 & 1 \\
MMe 2 & 2 \\
SSd 3 & 3 \\
DDian 4 & 4 \\
E E
F F
G G
H H

File2

D D
C C
B B
A A
str_text1
H H
G G
F F
E E

The example below is what I would like the new output file (File3) to have. It is imperative File1 and File2 are not overwritten, and everything must be restructured into the new file File3.

File3

D D
C C
B B
A A
 & N 1 & 1 \\
 & $\m$ 2 & 2 \\
 & $\s$$\sig$ 3 & 3 \\
 & $\d$\dian{x}$ 4 & 4 \\
H H
G G
F F
E E

I would like this to be a one liner inside of a shell script, and below is an example of what I have so far. I am not sure how to do the last substitution into the same one liner, however as a second one liner it is very simple. If these could be combined, I would appreciate that greatly.

Code1

sed -e 's/str_text1/ \& $(sed -n 5,8p File1.txt)/' \
    < File2.txt > File3.txt

Code1 Output* Fails to include the lines. Use of double quotes results in error: sed: -e expression #1, char 18: unterminated `s' command.

D D
C C
B B
A A
 & $(sed -n 5,8p File1.txt)
H H
G G
F F
E E

Code2 Simple for search and replace and works fine as is. This is replacing some of the strings in the first column from File1 in the new File3 file.

    sed -i -e 's/MMe/$\m$/g' \
        -i -e 's/SSd/$\s$$\sig$/g' \
        -i -e 's/DDian/$\d$\dian{x}$/g' \
        File3.txt

I have two files of code, the first (File1) including a few lines of text to be included and the second (File2) including strings that will be replaced with a range of lines from the first file. The lines included from the first file will also need the following string appended to each of the lines when they are included in the second file (not including the quotations): " & ". In addition, a search and replace for some of the strings from the first file (File1) needs to be replaced in the output file (File3). An example of the first two files is included below.

File1

A A
B B
C C
D D
N 1 & 1 \\
M 2 & 2 \\
S 3 & 3 \\
D 4 & 4 \\
E E
F F
G G
H H

File2

D D
C C
B B
A A
str_text1
H H
G G
F F
E E

The example below is what I would like the new output file (File3) to have. It is imperative File1 and File2 are not overwritten, and everything must be restructured into the new file File3.

File3

D D
C C
B B
A A
 & N 1 & 1 \\
 & $\m$ 2 & 2 \\
 & $\s$ 3 & 3 \\
 & $\d{x}$ 4 & 4 \\
H H
G G
F F
E E

I would like this to be a one liner inside of a shell script, and below is an example of what I have so far. I am not sure how to do the last substitution into the same one liner, however as a second one liner it is very simple. If these could be combined, I would appreciate that greatly.

Code1

sed -e 's/str_text1/ \& $(sed -n 5,8p File1.txt)/' \
    < File2.txt > File3.txt

Code1 Output* Fails to include the lines. Use of double quotes results in error: sed: -e expression #1, char 18: unterminated `s' command.

D D
C C
B B
A A
 & $(sed -n 5,8p File1.txt)
H H
G G
F F
E E

Code2 Simple for search and replace and works fine as is. This is replacing some of the strings in the first column from File1 in the new File3 file.

    sed -i -e 's/M/$\m$/g' \
        -i -e 's/S/$\s$/g' \
        -i -e 's/D/$\d{x}$/g' \
        File3.txt

I have two files of code, the first (File1) including a few lines of text to be included and the second (File2) including strings that will be replaced with a range of lines from the first file. The lines included from the first file will also need the following string appended to each of the lines when they are included in the second file (not including the quotations): " & ". In addition, a search and replace for some of the strings from the first file (File1) needs to be replaced in the output file (File3). An example of the first two files is included below.

File1

A A
B B
C C
D D
N 1 & 1 \\
Me 2 & 2 \\
Sd 3 & 3 \\
Dian 4 & 4 \\
E E
F F
G G
H H

File2

D D
C C
B B
A A
str_text1
H H
G G
F F
E E

The example below is what I would like the new output file (File3) to have. It is imperative File1 and File2 are not overwritten, and everything must be restructured into the new file File3.

File3

D D
C C
B B
A A
 & N 1 & 1 \\
 & $\m$ 2 & 2 \\
 & $\sig$ 3 & 3 \\
 & $\dian{x}$ 4 & 4 \\
H H
G G
F F
E E

I would like this to be a one liner inside of a shell script, and below is an example of what I have so far. I am not sure how to do the last substitution into the same one liner, however as a second one liner it is very simple. If these could be combined, I would appreciate that greatly.

Code1

sed -e 's/str_text1/ \& $(sed -n 5,8p File1.txt)/' \
    < File2.txt > File3.txt

Code1 Output* Fails to include the lines. Use of double quotes results in error: sed: -e expression #1, char 18: unterminated `s' command.

D D
C C
B B
A A
 & $(sed -n 5,8p File1.txt)
H H
G G
F F
E E

Code2 Simple for search and replace and works fine as is. This is replacing some of the strings in the first column from File1 in the new File3 file.

    sed -i -e 's/Me/$\m$/g' \
        -i -e 's/Sd/$\sig$/g' \
        -i -e 's/Dian/$\dian{x}$/g' \
        File3.txt
added 1 character in body
Source Link

I have two files of code, the first (File1) including a few lines of text to be included and the second (File2) including strings that will be replaced with a range of lines from the first file. The lines included from the first file will also need the following string appended to each of the lines when they are included in the second file (not including the quotations): " & ". In addition, a search and replace for some of the strings from the first file (File1) needs to be replaced in the output file (File3). An example of the first two files is included below.

File1

A A
B B
C C
D D
N 1 & 1 \\
M 2 & 2 \\
S 3 & 3 \\
D 4 & 4 \\
E E
F F
G G
H H

File2

D D
C C
B B
A A
str_text1
H H
G G
F F
E E

The example below is what I would like the new output file (File3) to have. It is imperative File1 and File2 are not overwritten, and everything must be restructured into the new file File3.

File3

D D
C C
B B
A A
 & N 1 & 1 \\
 & $\m$ 2 & 2 \\
 & $\s$ 3 & 3 \\
 & $\d{x}$ 4 & 4 \\
H H
G G
F F
E E

I would like this to be a one liner inside of a shell script, and below is an example of what I have so far. I am not sure how to do the last substitution into the same one liner, however as a second one liner it is very simple. If these could be combined, I would appreciate that greatly.

Code1

sed -e 's/str_textstr_text1/ \& $(sed -n 5,8p File1.txt)/' \
    < File2.txt > File3.txt

Code1 Output* Fails to include the lines. Use of double quotes results in error: sed: -e expression #1, char 18: unterminated `s' command.

D D
C C
B B
A A
 & $(sed -n 5,8p File1.txt)
H H
G G
F F
E E

Code2 Simple for search and replace and works fine as is. This is replacing some of the strings in the first column from File1 in the new File3 file.

    sed -i -e 's/M/$\m$/g' \
        -i -e 's/S/$\s$/g' \
        -i -e 's/D/$\d{x}$/g' \
        File3.txt

I have two files of code, the first (File1) including a few lines of text to be included and the second (File2) including strings that will be replaced with a range of lines from the first file. The lines included from the first file will also need the following string appended to each of the lines when they are included in the second file (not including the quotations): " & ". In addition, a search and replace for some of the strings from the first file (File1) needs to be replaced in the output file (File3). An example of the first two files is included below.

File1

A A
B B
C C
D D
N 1 & 1 \\
M 2 & 2 \\
S 3 & 3 \\
D 4 & 4 \\
E E
F F
G G
H H

File2

D D
C C
B B
A A
str_text1
H H
G G
F F
E E

The example below is what I would like the new output file (File3) to have. It is imperative File1 and File2 are not overwritten, and everything must be restructured into the new file File3.

File3

D D
C C
B B
A A
 & N 1 & 1 \\
 & $\m$ 2 & 2 \\
 & $\s$ 3 & 3 \\
 & $\d{x}$ 4 & 4 \\
H H
G G
F F
E E

I would like this to be a one liner inside of a shell script, and below is an example of what I have so far. I am not sure how to do the last substitution into the same one liner, however as a second one liner it is very simple. If these could be combined, I would appreciate that greatly.

Code1

sed -e 's/str_text/ \& $(sed -n 5,8p File1.txt)/' \
    < File2.txt > File3.txt

Code1 Output* Fails to include the lines. Use of double quotes results in error: sed: -e expression #1, char 18: unterminated `s' command.

D D
C C
B B
A A
 & $(sed -n 5,8p File1.txt)
H H
G G
F F
E E

Code2 Simple for search and replace and works fine as is. This is replacing some of the strings in the first column from File1 in the new File3 file.

    sed -i -e 's/M/$\m$/g' \
        -i -e 's/S/$\s$/g' \
        -i -e 's/D/$\d{x}$/g' \
        File3.txt

I have two files of code, the first (File1) including a few lines of text to be included and the second (File2) including strings that will be replaced with a range of lines from the first file. The lines included from the first file will also need the following string appended to each of the lines when they are included in the second file (not including the quotations): " & ". In addition, a search and replace for some of the strings from the first file (File1) needs to be replaced in the output file (File3). An example of the first two files is included below.

File1

A A
B B
C C
D D
N 1 & 1 \\
M 2 & 2 \\
S 3 & 3 \\
D 4 & 4 \\
E E
F F
G G
H H

File2

D D
C C
B B
A A
str_text1
H H
G G
F F
E E

The example below is what I would like the new output file (File3) to have. It is imperative File1 and File2 are not overwritten, and everything must be restructured into the new file File3.

File3

D D
C C
B B
A A
 & N 1 & 1 \\
 & $\m$ 2 & 2 \\
 & $\s$ 3 & 3 \\
 & $\d{x}$ 4 & 4 \\
H H
G G
F F
E E

I would like this to be a one liner inside of a shell script, and below is an example of what I have so far. I am not sure how to do the last substitution into the same one liner, however as a second one liner it is very simple. If these could be combined, I would appreciate that greatly.

Code1

sed -e 's/str_text1/ \& $(sed -n 5,8p File1.txt)/' \
    < File2.txt > File3.txt

Code1 Output* Fails to include the lines. Use of double quotes results in error: sed: -e expression #1, char 18: unterminated `s' command.

D D
C C
B B
A A
 & $(sed -n 5,8p File1.txt)
H H
G G
F F
E E

Code2 Simple for search and replace and works fine as is. This is replacing some of the strings in the first column from File1 in the new File3 file.

    sed -i -e 's/M/$\m$/g' \
        -i -e 's/S/$\s$/g' \
        -i -e 's/D/$\d{x}$/g' \
        File3.txt
Source Link

Replace string with line range from another file, including modification of line range on replace

I have two files of code, the first (File1) including a few lines of text to be included and the second (File2) including strings that will be replaced with a range of lines from the first file. The lines included from the first file will also need the following string appended to each of the lines when they are included in the second file (not including the quotations): " & ". In addition, a search and replace for some of the strings from the first file (File1) needs to be replaced in the output file (File3). An example of the first two files is included below.

File1

A A
B B
C C
D D
N 1 & 1 \\
M 2 & 2 \\
S 3 & 3 \\
D 4 & 4 \\
E E
F F
G G
H H

File2

D D
C C
B B
A A
str_text1
H H
G G
F F
E E

The example below is what I would like the new output file (File3) to have. It is imperative File1 and File2 are not overwritten, and everything must be restructured into the new file File3.

File3

D D
C C
B B
A A
 & N 1 & 1 \\
 & $\m$ 2 & 2 \\
 & $\s$ 3 & 3 \\
 & $\d{x}$ 4 & 4 \\
H H
G G
F F
E E

I would like this to be a one liner inside of a shell script, and below is an example of what I have so far. I am not sure how to do the last substitution into the same one liner, however as a second one liner it is very simple. If these could be combined, I would appreciate that greatly.

Code1

sed -e 's/str_text/ \& $(sed -n 5,8p File1.txt)/' \
    < File2.txt > File3.txt

Code1 Output* Fails to include the lines. Use of double quotes results in error: sed: -e expression #1, char 18: unterminated `s' command.

D D
C C
B B
A A
 & $(sed -n 5,8p File1.txt)
H H
G G
F F
E E

Code2 Simple for search and replace and works fine as is. This is replacing some of the strings in the first column from File1 in the new File3 file.

    sed -i -e 's/M/$\m$/g' \
        -i -e 's/S/$\s$/g' \
        -i -e 's/D/$\d{x}$/g' \
        File3.txt