LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   sed to get a string into a variable (https://www.linuxquestions.org/questions/linux-general-1/sed-to-get-a-string-into-a-variable-4175439863/)

zimbot 12-03-2012 04:28 PM

sed to get a string into a variable
 
Friends,

I am rather new to sed and awk and could use a bit of help
I am loopng through a dir of jpegs and printing the MD5 hash values into a text file

here is what I have
#!/bin/bash
# ~~~~~~~~~~batch kri-mov [dv25] to mp4
# ~~~~~~~~-make 1 mp4 ** Hints the mp4 ***
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~V 1 ; 7.11.2012
#-----------------------------------alias on ssx2
#
#################################################################################

movDir=~/Pictures/0

echo $(date) >> $movDir/aaa_md5.txt

echo "STARTING "
####################------------------ the meat
cd $movDir

for f in *.jpg ; do
#for f in *.mp4 ; do
## sed 1 ---- sed 's/MD5//g' removed MD5
## sed 2 ---- sed 's/[)(]//g' removes the ( & )
##
md5 ${f%.*}.jpg | sed 's/MD5//g' | sed 's/[)(]//g' >> $movDir/aaa_md5.txt
done

this makes a line ( 1 for each jpg ) in the txt file: aaa_md5.txt

that looks like
Abstract-Art-Backgrounds.jpg = cbd84a06af6a021033039802f69d548d

before i did ANY sed it looked like
MD5 (Abstract-Art-Backgrounds.jpg) = cbd84a06af6a021033039802f69d548d

so this is better but Also have wish to print into a second txt file the following , just the hash value , like this for every line.
cbd84a06af6a021033039802f69d548d

so I was trying to do something like
** a sed that grabs only that which is to the right of "=" and that becomes $hash , basically the hash val only in a var called $hash
I tried expr ... but failed.

I am also thinking about entering the hash value into a database with a get
so -- since as i loop
I know what the file is - $f
if i also had the hash value - $hash
I am hoping I can do the sql call to shove that value into the db.
as i loop

thanks much!

also - aplolgies for calling sed twice ...i am sure there is a better way. ;)

jlinkels 12-03-2012 05:03 PM

To keep the overview, put the first sed result in a variable:
Code:

md5=$(md5 ${f%.*}.jpg | sed 's/MD5//g' | sed 's/[)(]//g')
So $md5 now holds the string:
MD5 (Abstract-Art-Backgrounds.jpg) = cbd84a06af6a021033039802f69d548d

Extract the part up to the '=':
Code:

hash=${md5##*= }
Note the omission of the '$' character in front of md5 in the ${...} expression.

If you still want to write the first line to a file you can do so by
Code:

echo $md5 >> afile
jlinkels

TobiSGD 12-03-2012 06:33 PM

For the future, please use code-tags for posting code. This will preserve the formatting, so that you don't have use hash-tags or something else for that.
Just enclose your code with [code][/code].

zimbot 12-04-2012 09:49 AM

Thanks much I am now able to write to a txt file in the form of :

this the entire
shupeJar.jpg = 2e89aab49e3b87ec26b4ea9e9ac918aa

this only the hash
2e89aab49e3b87ec26b4ea9e9ac918aa

this is the file of concern
shupeJar.jpg

baseNameValue of file of concern
shupeJar

by using the code of

Code:


#!/bin/bash
# ~~~~~~~~~~#
#################################################################################

movDir=~/Pictures/0

echo $(date)  >> $movDir/aaa_md5.txt

echo "STARTING  STARTING  STARTING  STARTING  STARTING  STARTING  STARTING  STARTING "
####################------------------ the meat

cd $movDir

for f in *.jpg ; do
## sed 1 ---- sed 's/MD5//g'  removed MD5
##
##md5 ${f%.*}.jpg | sed 's/MD5//g' | sed 's/[)(]//g' >> $movDir/aaa_md5.txt
md5=$(md5 ${f%.*}.jpg | sed 's/MD5//g' | sed 's/[)(]//g')
hash=${md5##*= }
echo this the  entire >> $movDir/aaa_md5.txt
echo $md5 >> $movDir/aaa_md5.txt

echo this only the hash >> $movDir/aaa_md5.txt
echo $hash >> $movDir/aaa_md5.txt

echo this is the file of concern >> $movDir/aaa_md5.txt
echo ${f%.*}.jpg >>  $movDir/aaa_md5.txt
echo baseNameValue of file of concern >> $movDir/aaa_md5.txt
echo ${f%.*}  >> $movDir/aaa_md5.txt



done

echo $(date)  >> $movDir/aaa_md5.txt
echo -------------------------------------------  >> $movDir/aaa_md5.txt
echo -                                                  >> $movDir/aaa_md5.txt

I like that code tag thingie also --- again THAnKs!

zimbot 12-05-2012 10:21 AM

frist I am able to do what i wish to do so --happy
but I am trying to have true understanding of how this works

specifically what is happening in
hash=${md5##*= }
what do the ##*= do
how does that sift out the Num from $md5

i have looked at The Advanced Bash Scripting Guide

thank again

Z038 12-05-2012 09:30 PM

The ## is one of the many bash string manipulation operators. Here is a reference:

http://tldp.org/LDP/abs/html/string-manipulation.html

Code:

${string##substring}

    Deletes longest match of $substring from front of $string.

Take a look at the example on that page above.

So in your example, ${md5##*= }, the longest match of any characters (indicated by the asterisk) that ends in an equal sign and a blank will be stripped out of the md5 variable, starting from the beginning.

What's it's doing is stripping off everything from the left up to and including the "= ", leaving just the md5 hash number.

zimbot 12-06-2012 08:15 AM

thanks z038
That seems clear.
I do appreciate

( i think i am going to be using that!)


All times are GMT -5. The time now is 01:01 PM.