I am trying to use sed to extract a specific string from a line within a file. Currently I am reading in a file with a while loop and searching for a specific string. When that string is found I am extracting it, but I then need to use sed to parse the output so that I only get the string between two slashes (Its a directory name, so I need to keep both the starting and trailing slashes if possible). Here is the loop I am running to search for a file:
#!/bin/sh
file=configFile.conf
while read line
do
if echo "$line" | grep -q "directory_root"
then DIR_ROOT="$line"
fi
done < "$file"
echo $DIR_ROOT
exit 0
The while loop works and echoes the following string:
directory_root /root/config/data/
I then need to use sed in order to get the following output in order to pass the correct directory name in to another script:
/root/
Is it possible to use sed and regular expressions to extract only the above from the echoed output?
Thanks