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?
    – jia103
    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/'
1
  • Running sed once: sed -r 's/MD5 \((.*)\).*/\1/' <PATH/TO/FILE>
    – SergA
    Commented Dec 5, 2015 at 6:30
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.)

1

Another way with cut can be :

echo $line|cut -d "(" -f2|cut -d ")" -f1
1
sed -e 's/^.*(\([^)]*\)).*$/\1/' < infile.txt
1
  • I didn't at the time; I did now and updated to correct it.
    – jia103
    Commented Dec 6, 2015 at 5:30

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.