I'm retrieving a section from a file and want to replace a line in another file with this multi-line data. Currently I'm outputting to a file but would prefer to use a variable.
For instance
R 0x00007d04 0x70040000
[OVERWRITE_1]
C "- Starting Execution"
Becomes:
R 0x00007d04 0x70040000
W 0x00001118 0x0d1f4e3a
W 0x0000111c 0xa3795ac8
W 0x00001120 0xc50e69d5
C "- Starting Execution"
Here is what I have:
START="R 0x00007fd0 0x00000000 # CSR:014 (CSRCID-OPT2)"
END="W 0x00007f80"
FILE=file.txt
#Retrieve text between sections above to variable
OUTPUT=$(sed -n "/^$START$/,/$END/ { /^$START$/d ; /$END/d ; /^$/d ; p }"
$FILE)
echo "$OUTPUT" > tmp.txt
#This line currently appends after [OVERWRITE_1]
sed '/\[OVERWRITE_1\]/r tmp.txt' test.asm
Which outputs this:
R 0x00007d04 0x70040000
[OVERWRITE_1]
W 0x00001118 0x0d1f4e3a
W 0x0000111c 0xa3795ac8
W 0x00001120 0xc50e69d5
C "- Starting Execution"
I know at this point I could remove the line but I feel like others might want to know this also and I haven't found a good solution.
Found solution here
With GNU sed:
Find line in file file.csv which contains find, append content (r) of file newline.txt and delete (d) line which contains find
START="R 0x00007fd0 0x00000000 # CSR:014 (CSRCID-OPT2)"
END="W 0x00007f80"
FILE=file.txt
#Retrieve text between sections above to variable
OUTPUT=$(sed -n "/^$START$/,/$END/ { /^$START$/d ; /$END/d ; /^$/d ; p }"
$FILE)
echo "$OUTPUT" > tmp.txt
sed -e '/\[OVERWRITE_1\]/{r tmp.txt' -e 'd}' test.asm
Sed from variable instead of file :
START="R 0x00007fd0 0x00000000 # CSR:014 (CSRCID-OPT2)"
END="W 0x00007f80"
FILE=file.txt
#Retrieve text between sections above to variable
OUTPUT=$(sed -n "/^$START$/,/$END/ { /^$START$/d ; /$END/d ; /^$/d ; p }"
$FILE)
echo "$OUTPUT" > tmp.txt
sed "s/\[OVERWRITE_1\]/$OUTPUT/" test.asm
Note that we used double quotes instead of singles. But as found here there is a problem if you have special characters inside $OUTPUT (\\, & and /). Didn't find a good solution for this so I would keep the workaround with the file.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.