Friday, April 15, 2022

[SOLVED] Values of one file to change corresponding value in another file?

Issue

I need help with a simple dash script solution. A script reading the values of file: "Install.txt", (sample content):

TRUE 203
TRUE 301
TRUE 602
TRUE 603

The numbers will correspond with the same number (at the end of line) in file: "ExtraExt.sys", (sample content):

# Read $[EXTRA_DIR]/diaryThumbPlace #202
# Read $[CORE_DIR]/myBorderStyle #203
# Read $[EXTRA_DIR]/mMenu #301
# Read $[EXTRA_DIR]/dDecor #501
# Read $[EXTRA_DIR]/controlPg #601
# Read $[EXTRA_DIR]/DashToDock #602
# Read $[EXTRA_DIR]/deskSwitch #603

All lines are tagged (#). The script will untag the corresponding line that has the same number as in the file "install.txt". For example, TRUE 203 will untag the line ending with #203

# Read $[CORE_DIR]/myBorderStyle #203

to (without "#" before Read)

Read $[CORE_DIR]/myBorderStyle #203

I've searched for awk/sed solution, but this requires a loop to go through the numbers in Install.txt.

Any help is appreciated. Thank you.


Solution

you can try,

# store "203|301|602|603" in search variable
search=$(awk 'BEGIN{OFS=ORS=""}{if(NR>1){print "|"}print $2;}' Install.txt)
sed -r "s/^# (.*#($search))$/\1/g" ExtraExt.sys

you get,

# Read $[EXTRA_DIR]/diaryThumbPlace #202
Read $[CORE_DIR]/myBorderStyle #203
Read $[EXTRA_DIR]/mMenu #301
# Read $[EXTRA_DIR]/dDecor #501
# Read $[EXTRA_DIR]/controlPg #601
Read $[EXTRA_DIR]/DashToDock #602
Read $[EXTRA_DIR]/deskSwitch #603

or, -i option for edit in file

sed -i -r "s/^# (.*#($search))$/\1/g" ExtraExt.sys


Answered By - Jose Ricardo Bustos M.
Answer Checked By - Senaida (WPSolving Volunteer)