2

I have a file that contains strings in this format:

MD5 (TestImages/IMG_0627.JPG) = 6ed611b3e777c5f7b729fa2f2412d656

I am trying to figure out a way to extract the file path, so that I would get a string like this:

TestImages/IMG_0627.JPG

For a different part of my script, I am using this code to remove everything before and after the brackets, and I could of course do something similar, however I'm sure there is a better way?

shortFile=${line#*MD5 }
shortFile=${shortFile%%)*}

Anyone have any suggestions?

1
  • How about using sed? Commented Nov 24, 2015 at 0:27

4 Answers 4

2

You could use sed but that has the overhead of starting a new process.

echo $line | sed -r 's/MD5 \((.*)\).*/\1/'
Sign up to request clarification or add additional context in comments.

1 Comment

Running sed once: sed -r 's/MD5 \((.*)\).*/\1/' <PATH/TO/FILE>
2

Just to throw a non-sed answer onto the pile. (Also slightly cheaper since it avoids the pipeline and sub-shell.)

awk -F '[()]' '{print $2}' <<<"$line"

That said the substring expansion option is a reasonable one if it does what you need. (Though it looks like you missed the ( in the first expansion.)

Comments

1

Another way with cut can be :

echo $line|cut -d "(" -f2|cut -d ")" -f1

Comments

1
sed -e 's/^.*(\([^)]*\)).*$/\1/' < infile.txt

1 Comment

I didn't at the time; I did now and updated to correct it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.