Reputation: 1079
I'm new to shell scripting. I need to get the value "test" from the below absolute path and print it. How can this be achieved using SED command. Please help.
/home/path/test_script/logs
Upvotes: 0
Views: 100
Reputation: 3268
In my opinion awk performs better this task. Using -F you can use multiple delimiters such as "/" and "_":
echo /home/path/test_script/logs | awk -F'/|_' '{print $4}'
Upvotes: 1
Reputation: 163
You can try awk command:
echo /home/path/test_script/logs | awk -F"/" '{print $4}' | cut -c1-4
Your output should be 'test'. You can also assign the 'test' value to a variable by doing the below:
var1=`echo /home/path/test_script/logs | awk -F"/" '{print $4}' | cut -c1-4`
Upvotes: 0
Reputation: 8446
sed
code to first remove the _
and what's to the right of it, then the /
s and what's to the left of those, leaving only "test".
echo /home/path/test_script/logs | sed 's/[_].*//;s,.*/,,g'
Output:
test
Upvotes: 0