Using SED command to get values from absolute path - shell

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

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}'

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

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`

Related

handler: xyz.lambda_handler is a text and i want xyz.lambda_handler as output using sh script

i have "handler: xyz.lambda_handler" text in one file and i want "xyz.lambda_handler" i.e text present next to "handler:" as output using shell script, how can i do this.
I have tried
awk -F '${handler}' '{print $1}' filename | awk '{print $2}
grep handler filename
command but not getting correct output
as mentioned in qtn.
I combined two commands and i got my answer
grep Handler: filename | awk -F '${handler}' '{print $1}' | awk '{print $2}'
grep givepattern givefilename | awk -F '${givepattern}' '{print $1}' | awk '{print $2}'
It's grep, not greap. To print only the matched parts of a matching line, use option -o.
grep -o xyz.lambda_handler filename

Awk the command output

This is the command what i run.
ldap="$(ldapwhoami -x -H ldap://ABC.example.org -D "$user" -w "$pass")"
This is the output result:
u:ABC\1234567
May i know how to get the expected output ? like this 1234567
Thanks
1st Solution: Could you please try following.
echo "u:ABC\1234567" | awk -F'\' '{print $NF}'
OR
your_command | awk -F'\' '{print $NF}'
2nd solution: using awk's sub method.
your_command | awk '{sub(/.*\\/,"")} 1'
echo "u:ABC\1234567" | sed "s/[^0-9]//g"
There is a way with SED.

Bash Cut text from line with different delimiters

I have a variable with value like:
#capability_ids type="list">[LOADBALANCER]</capability_ids>#
And need to extract from this string type of equipment ( LOADBALANCER ).
I've tried to use cut, but don't know how write cut command with different delimiters.
DeviceType=$( echo $DeviceTypeDirty | cut -d'[' -f1)
Can enywone help me with right solution on bash?
use awk with regular expression: awk -F '[\\[\\]]' '{print $2}'
$ echo '#capability_ids type="list">[L3SWITCH]/capability_ids>#'|awk -F '[\\[\\]]' '{print $2}'
$ L3SWITCH
$ DeviceType=$( echo "$DeviceTypeDirty" | awk -F '[\\[\\]]' '{print $2}')
I tried and got to extract "LOADBALANCER"
Administrators-MacBook-Pro:~$ echo "\"list\">[LOADBALANCER]
</capability_ids>#"|awk -F '[][]' '{print $2}'
LOADBALANCER
Administrators-MacBook-Pro:~$
Hope that helps!
Using cut:
DeviceTypeDirty="#capability_ids type="list">[LOADBALANCER]</capability_ids>#"
DeviceType="$(echo "$DeviceTypeDirty" | cut -d'[' -f2 | cut -d']' -f1)"
Output:
echo "$DeviceType"
LOADBALANCER

Searching a pattern in linux

I have a file containing:
abc=1234.121
How to get only the numerical part?
1234.121
You can try this sed
sed 's/.*=//g'
You could use cut,
echo abc=1234.121 | cut -f2 -d'='|cut -f1 -d'.'
Here are some awk version:
echo "abc=1234.121" | awk -F= '$0=$2'
1234.121
echo "abc=1234.121" | awk -F= '{print $2}'
1234.121

save output of awk into a variable in a shell script

I need to save the output of this command into a variable
$scriptName | awk '{split($0,a,"_"); print a[1]}'
I tried to do this but it didn't work
schema=$( $scriptName | awk '{split($0,a,"_"); print a[1]}' )
can please someone tell me out to do that? thank you.
Here's a slightly simpler way:
schema=`echo $scriptName |awk -F_ '{print $1}'`
Try
schema="$( $scriptName | awk '{split($0,a,"_"); print a[1]}' )"
(with quotes). But how do you check if it works or not?
Third alternative:
$script | awk ' ... ' | read schema

Resources