Bash Scripting Help to automate replacing multiple lines

Background:
I am writing a script to help me automate tweaks and things I apply to a custom Android rom I developed. I am on the very last part of my script, and I am stuck trying to find the right command to do what I seek.

When I build roms from source, a file called updater-script is generated and it always has the signature of the source code's creator. Since I am now building it from source, I have to replace this signature with my own. Also there are a few commands I add in to make some apps uninstallable.

What I am looking for:
I want to automate the text replacement. I have created two files, one is called findsts and the other replaceccore.

findsts contains the signature that is ALWAYS in the updater-script. I want to use this file to tell the command I am to use that that is the selection I want replaced.

replaceccore has my signature and will eventually have commands to replace text in the updater-script, but for now I just want the signature replaced with the data in this file.

I first thought sed was the answer, but soon found that sed doesn't like variables, but rather would use literal strings. I was then referred to awk. Awk seems to be a very powerful command, but I have yet to see how it will help me in my current situation. I also know that the signature that i am finding and replacing has tons of special characters, including parenthesis, semi colons, and quotations. This also poses a problem because now not only do I need to figure out the correct command, but then how to tell it to use these files in a literal way and not try to interpret the commands in the file.

The details:

findsts contains:

ui_print("************************************************");
ui_print("*                                              *");
ui_print("*            STS-Dev-Team presents             *");
ui_print("*                                              *");
ui_print("*         JELLY BEAN for OMAP4 devices         *");
ui_print("*                                              *");
ui_print("*     Follow us  @dhacker29 and @hashcode0f    *");
ui_print("*        Get the GApps at goo.im/gapps         *");
ui_print("*                                              *");
ui_print("************************************************");
ui_print(" ");

replaceccore contains:

ui_print("*************************");
ui_print("*       CyanCore        *");
ui_print("*                       *");
ui_print("*   Brought to you by:  *");
ui_print("*    Team SpeedCore     *");
ui_print("*                       *");
ui_print("*    <-Developers->     *");
ui_print("*       2tealth         *");
ui_print("*     Silverlink34      *");
ui_print("*                       *");
ui_print("*<Performance Optimizer>*");
ui_print("*      The Doctor       *");
ui_print("*        Taecon         *");
ui_print("*************************");
ui_print(" ");

Here is a small part of updater-script, the file I actually am trying to edit, from the beginning of the file:

mount("ext3", "EMMC", "/dev/block/system", "/system");
package_extract_file("system/bin/backuptool.sh", "/tmp/backuptool.sh");
package_extract_file("system/bin/backuptool.functions", "/tmp/backuptool.functions");
set_perm(0, 0, 0777, "/tmp/backuptool.sh");
set_perm(0, 0, 0644, "/tmp/backuptool.functions");
run_program("/tmp/backuptool.sh", "backup");
unmount("/system");
show_progress(0.500000, 0);
ui_print("************************************************");
ui_print("*                                              *");
ui_print("*            STS-Dev-Team presents             *");
ui_print("*                                              *");
ui_print("*         JELLY BEAN for OMAP4 devices         *");
ui_print("*                                              *");
ui_print("*     Follow us  @dhacker29 and @hashcode0f    *");
ui_print("*        Get the GApps at goo.im/gapps         *");
ui_print("*                                              *");
ui_print("************************************************");
ui_print(" ");
ui_print("Wiping cache...");
mount("ext3", "EMMC", "/dev/block/cache", "/cache");
delete_recursive("/cache");
ui_print("Wiping dalvik-cache...");
mount("ext3", "EMMC", "/dev/block/userdata", "/data");
delete_recursive("/data/dalvik-cache");
ui_print("Mounting your system...");
mount("ext3", "EMMC", "/dev/block/system", "/system");
ui_print("Deleting old system files...");
delete_recursive("/system");
ui_print("Starting Installation...");

If it makes a difference I know the developers who provide the source code and they are okay with other developers building roms from source and customizing it.

Thank you for reading. I hope this is not too complicated a task or annoying. I have been searching for a few days and finally broke down and decided to ask. Please be merciful :slight_smile:

Edit:
So if sed actually worked this way, I would set it up like this:

sts=findsts
ccore=replaceccore
sed -i "s/$sts/$ccore/g' updater-script

but it doesn't, so here I am, back at the drawing board.

What comes to mind is to delete these ui_print lines, and then copy in some new ui_lines.
However, without seeing more of the code, hard to say. For instance, are these ui_print lines always only eleven lines, and occurring at the same point?

In updater-script, the 11 line STS Team signature always is present on the 8th line, unless the rom base does not use the backup tool. In that case, the STS Team signature would start on line 2, after the show progress command.

head -7 myfile.txt

will extract the first 7 lines

tail -n +18 myfile.txt

will extract beginning at line 18

So, assuming you have your new message in a file called new.txt, you could do something like:

head -7 myfile.txt >t1.txt
tail -n +18 myfile.txt >t2.txt
cat t1.txt new.txt t2.txt >newfile.txt
1 Like

sed s command works on strings, not input files!
Further, the strings are regular expressions - not wanted here.

But there are other Unix tools:

diff findsts replacecore > patchfile
patch update-script patchfile

If you don't want the intermediate patchfile:

diff findsts replaceccore | patch updater-script
1 Like

This looks promising. I know now that sed likes literal strings and not input files :). I love learning code and am excited that there is a community here willing to share their knowledge when I have problems finding it on my own.

@Joeyg, I like your approach as well, this helps me to understand some more commands. Does the head and tail commands used in this context copy the selected lines to files t1 and t2, and then im a little lost what the cat command does to mytext.txt and newfile.txt, assuming that new.txt has the information in my replaceccore file.

---------- Post updated at 06:45 PM ---------- Previous update was at 06:32 PM ----------

diff findsts replaceccore | patch updater-script

This worked like a charm!

---------- Post updated at 06:55 PM ---------- Previous update was at 06:45 PM ----------

Will diff let me use functions?

Such as:

sts=~/dir/findsts
ccore=~/dir/replaceccore
oldsig=~/diffdir/updater-script
diff $sts $ccore | patch $oldsig

That is not a function, that is a variable.

Any shell program at all can use those, because that's the shell's job, not the program's.

1 Like

Hi.

Preprocessing with include files comes to mind ... cheers, drl

Okay, this is great. I have one more portion of this script I need to figure out. I was considering what command to use to have the user input a version number. I have a question setup to ask the build type, so when this is answered I will have the diff command replace whatever the label for the build currently is, replace it with my build name for that build type and add a V at the end for version. After the build name has been changed, I think I will use the read command to have user input version number, which will be right after the V.

I'll post back on here if I have any more issues with it. Thanks for your help folks, I sincerely appreciate it.