Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
HELP : Shell script / binary file / grep / hexedit / replace
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
komp
n00b
n00b


Joined: 02 Jan 2008
Posts: 9

PostPosted: Sat Mar 08, 2008 12:13 pm    Post subject: HELP : Shell script / binary file / grep / hexedit / replace Reply with quote

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
View user's profile Send private message
trae
n00b
n00b


Joined: 22 Apr 2005
Posts: 54
Location: Finland

PostPosted: Sat Mar 08, 2008 1:23 pm    Post subject: Re: HELP : Shell script / binary file / grep / hexedit / rep Reply with quote

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
View user's profile Send private message
komp
n00b
n00b


Joined: 02 Jan 2008
Posts: 9

PostPosted: Sat Mar 08, 2008 1:33 pm    Post subject: Reply with quote

cool, will try now

thanks :)
Back to top
View user's profile Send private message
komp
n00b
n00b


Joined: 02 Jan 2008
Posts: 9

PostPosted: Sat Mar 08, 2008 6:47 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum