View previous topic :: View next topic |
Author |
Message |
komp n00b
Joined: 02 Jan 2008 Posts: 9
|
Posted: Sat Mar 08, 2008 12:13 pm Post subject: HELP : Shell script / binary file / grep / hexedit / replace |
|
|
Hi Gurus
I have a file that I would like to search for a specific string (easy enough). I would then like to take the 1st found reference and put that into a variable, this variable will be referenced later in the script again for comparison.
There are only 2 entries that could be found so I would expect only 2 references. (I want to just take the first found reference)
input file is `mysource` and the string I want to find and put into variable is `BlockAddition`
......
After that I have some other stuff to process which I am fine with.
......
I then need to do a search and replace on a binary file within the same script (this is using the variable I just created above as a `basename`. The replacement is on the first line only. Lets call the binary file `wizwoz.bin`
00000000 00 00 00 01 63 22 AE 33 AC 34 E4 01 40 16 E8 40 00 00 FA 40 00 2E E0 23 C6 0C 64 80
0000001C ............................................
What I want to do is replace 63 22 AE 33 with 63 22 AE 11
then save the changed file to somename.
I really need to automate this (I can do it manually fine).
Anyone have any pointers please ?
Thanks guys and gals
John |
|
Back to top |
|
|
LemurFromTheId n00b
Joined: 22 Apr 2005 Posts: 64 Location: Finland
|
Posted: Sat Mar 08, 2008 1:23 pm Post subject: Re: HELP : Shell script / binary file / grep / hexedit / rep |
|
|
komp wrote: | Hi Gurus
I have a file that I would like to search for a specific string (easy enough). I would then like to take the 1st found reference and put that into a variable, this variable will be referenced later in the script again for comparison.
There are only 2 entries that could be found so I would expect only 2 references. (I want to just take the first found reference)
input file is `mysource` and the string I want to find and put into variable is `BlockAddition`
|
I'm not sure I understood. If you know exactly what the string is, is it not enough just to see whether the file has the string in it or not? Like this:
Code: | file="mysource"
str="BlockAddition"
match=
if [ "$(grep -c $str $file)" -gt 0 ]; then match=$str; fi
|
Quote: | I then need to do a search and replace on a binary file within the same script (this is using the variable I just created above as a `basename`. The replacement is on the first line only. Lets call the binary file `wizwoz.bin`
00000000 00 00 00 01 63 22 AE 33 AC 34 E4 01 40 16 E8 40 00 00 FA 40 00 2E E0 23 C6 0C 64 80
0000001C ............................................
What I want to do is replace 63 22 AE 33 with 63 22 AE 11
then save the changed file to somename.
I really need to automate this (I can do it manually fine).
Anyone have any pointers please ? |
How about this:
Code: |
sed wizwiz.bin $'1s/\x63\x22\xae\x33/\x63\x22\xae\x11/' > new_wizwiz.bin
|
|
|
Back to top |
|
|
komp n00b
Joined: 02 Jan 2008 Posts: 9
|
Posted: Sat Mar 08, 2008 1:33 pm Post subject: |
|
|
cool, will try now
thanks |
|
Back to top |
|
|
komp n00b
Joined: 02 Jan 2008 Posts: 9
|
Posted: Sat Mar 08, 2008 6:47 pm Post subject: |
|
|
Noooo.
I have overcome the binary part with some perl.
The secondary part is annoying me... (i know its simple but myabe i cannot see the wood for the trees )
I need to grep for the 2 only references called BlockAddition in the file. They only appear twice.
1 will be BlockAddition=meh
2 will be BlockAddition=woot
BUT.
They are not in that always in that order and maybe reversed.
LEt me write it how I think it.
grep BlockAddition filename
if BlockAddition=meh
then
$firstone=meh
$secondone=woot
...
...
...
...
some more scripting
..
use $firstone as argument to another executable (i cant run this executable against another type)
...
end
does that make more sense. meh is audio and woot is subtitles for instance. So i cannot run a subtitle script against an audio file... see where I am going?
Thanks |
|
Back to top |
|
|
|