Click here to Skip to main content
15,502,616 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need help with a bash script modification. Currently, the script reads the values of file "Install.txt", (sample content):
TRUE 203
TRUE 301
TRUE 602
TRUE 603

The numbers correspond with the same number in file: "ExtraExt.sys", (sample content):
false Styles "Diary Thumbnails" #202
false Styles "My Border Style" #203
false Menus "My Menu" #301
false Decorations "Diary Decor" #501
false Modules "Control Pager Button" #601
false Modules "Dash To Dock" #602
false Modules "Desk Switch" #603

All lines are "false". The script removes the text "false" of the corresponding line that has the same number as in file "Install.txt". For example, TRUE 203 changes the line with #203.
false Styles "My Border Style" #203

to
Styles "My Border Style" #203

This was the sed solution:
search=$(awk 'BEGIN{OFS=ORS=""}{if(NR>1){print "|"}print $2;}' Install.txt)
sed -i -r "s/^false (.*#($search))$/\1/g" ExtraExt.sys

The solution to remove works fine.
This solution is to replace "false" with "TRUE" in the file "ExtInstaller.sh".

What I have tried:

I have tried different codes but cause errors, for example..
sed -i -r "s/^false/TRUE (.*#($search))$/\1/g" ExtInstaller.sh
Posted
Updated 11-Mar-22 7:47am
v2
Comments
Richard MacCutchan 10-Mar-22 11:02am    
That script will replace "false" with TRUE #(203|301) or similar. I do not think you can do it with a simple sed script like that because the text you are trying to replace is not in the same position as the text found by the regex.

> @Richard MacCutchan: I do not think you can do it with simple sed...

I have this solution but it is too complex.
search=$(awk 'BEGIN{OFS=ORS=""}{if(NR>1){print "|"}print $2;}' Install.txt)
sed -r "s/^false (.*($search))$/\TRUE/g" ExtraExt.sys > Column1.txt

RESULT (Column1.txt):
false Styles "Diary Thumbnails" #202
TRUE
TRUE
false Decorations "Diary Decor" #501
false Modules "Control Pager Button" #601
TRUE
TRUE

Paste Column1.txt and ExtraExt.sys
paste <(awk '{print $1}' Column1.txt) \
      <(awk '{print $2, $3, $4, $5, $6}' ExtraExt.sys) > ExtInstaller.sh

RESULT (ExtInstaller.sh)
false	Styles "Diary Thumbnails" #202 
TRUE	Styles "My Border Style" #203
TRUE	Menus "My Menu" #301 
false	Decorations "Diary Decor" #501 
false	Modules "Control Pager Button" #601
TRUE	Modules "Dash To Dock" #602
TRUE	Modules "Desk Switch" #603 


I hope there is a better solution. Any help is appreciated. Thank you.
 
Share this answer
 
I got one good solution.
awk '
    NR==FNR {a["#"$2] = $1; next}
    $NF in a {$1 = a[$NF]}
1' Install.txt ExtInstaller.sh

RESULT same as Solution 1.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900