1

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
1
  • wrt I would like this to be a one liner inside of a shell script - asking for a one-liner tells us you care more above brevity than anything that really matters in software (e.g. clarity, portability, efficiency, robustness, maintainability, etc.) and so it's usually a bad idea to ever ask for a on-liner as it discourages us from providing a good solution as you'll probably end up accepting a worse but briefer solution. Additionally, if this piece of code is going to be inside a shell script as you say, then it's hard to imagine why you'd care how many lines it's on. – Ed Morton Nov 14 '20 at 14:36
0

If I did understand correctly here is the final output you are looking after executing code 1 & 2 :

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 suggest using a one line mixture of awk with gsub for search and replace function and asked.

    #CODE1 : oneliner
    awk -v a="$(awk 'NR==5, NR==8' File1.txt  | sed -ie 's/\\/#/g' | sed -ie 's/&/@/g' | sed 's/^/ @ &/')" '{ gsub(/str_text1/,a); print }' File2.txt > File3.txt
    #CODE2 : oneliner
    sed -i -e 's/Me/$\\\m$/g' -i -e 's/Sd/$\\\sig$/g' -i -e 's/Dian/\$\\\dian{x}$/g' -i -e 's/@/\&/g' -i -e 's/#/\\/g' File3.txt
    cat File3.txt

Explanation :

CODE 1

Same as sed -n 5,8p File1.txt but instead put the whole result in awk variable so it will be used next by the gsub utility awk -v a="$(awk 'NR==5, NR==8' File1.txt)"

I prefer using awk instead of sed for getting range of line from a text file awk 'NR==5, NR==8' File1.txt

Will replace the special characters \ and & respectively by # and @ ; because for the next command (awk) \ and & are specials chars sed -ie 's/\\/#/g' | sed -ie 's/&/@/g'

Replace str_text1 by the content of the variable a gsub(/str_text1/,a); print

CODE 2 :

I have added some to your command so it will match the wanted output sed -i -e 's/Me/$\\\m$/g' -i -e 's/Sd/$\\\sig$/g' -i -e 's/Dian/\$\\\dian{x}$/g'

Replace back the characters that we used as an escape chars : @-> & ; # -> \ sed -i -e 's/@/\&/g' -i -e 's/#/\\/g'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.