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. ;) |
To keep the overview, put the first sed result in a variable:
Code:
md5=$(md5 ${f%.*}.jpg | sed 's/MD5//g' | sed 's/[)(]//g') MD5 (Abstract-Art-Backgrounds.jpg) = cbd84a06af6a021033039802f69d548d Extract the part up to the '=': Code:
hash=${md5##*= } If you still want to write the first line to a file you can do so by Code:
echo $md5 >> afile |
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]. |
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:
|
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 |
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} 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. |
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. |