LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-03-2012, 04:28 PM   #1
zimbot
Member
 
Registered: Nov 2005
Location: cincinnati , ohio . USA
Distribution: ubuntu , Opensuse , CentOS
Posts: 179

Rep: Reputation: 17
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.
 
Old 12-03-2012, 05:03 PM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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
 
1 members found this post helpful.
Old 12-03-2012, 06:33 PM   #3
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
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].
 
Old 12-04-2012, 09:49 AM   #4
zimbot
Member
 
Registered: Nov 2005
Location: cincinnati , ohio . USA
Distribution: ubuntu , Opensuse , CentOS
Posts: 179

Original Poster
Rep: Reputation: 17
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!
 
Old 12-05-2012, 10:21 AM   #5
zimbot
Member
 
Registered: Nov 2005
Location: cincinnati , ohio . USA
Distribution: ubuntu , Opensuse , CentOS
Posts: 179

Original Poster
Rep: Reputation: 17
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
 
Old 12-05-2012, 09:30 PM   #6
Z038
Member
 
Registered: Jan 2006
Location: Dallas
Distribution: Slackware
Posts: 910

Rep: Reputation: 174Reputation: 174
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.

Last edited by Z038; 12-05-2012 at 09:36 PM.
 
1 members found this post helpful.
Old 12-06-2012, 08:15 AM   #7
zimbot
Member
 
Registered: Nov 2005
Location: cincinnati , ohio . USA
Distribution: ubuntu , Opensuse , CentOS
Posts: 179

Original Poster
Rep: Reputation: 17
thanks z038
That seems clear.
I do appreciate

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Sed/awk/cut to pull a repeating string out of a longer string StupidNewbie Programming 4 09-13-2018 03:41 AM
[SOLVED] sed append string in variable to last line of file. SilversleevesX Linux - Newbie 7 11-27-2011 11:13 PM
how do i replace a text string in a file with a random string? (with sed etc) steve51184 Linux - Software 16 09-02-2010 11:05 AM
Sed/awk/grep search for number string of variable length in text file Alexr Linux - Newbie 10 01-19-2010 01:34 PM
Trying to change String using sed with a string \/home\/user\/Desktop icecoolcorey Programming 10 06-12-2008 11:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration