Awk the command output - shell

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.

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

Using SED command to get values from absolute path

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`

Running last in awk [Solaris]

How can i run last command for every matched line in awk?
ypcat passwd | awk -F":" '/:John / { system("last" $1) }'
I'm trying to execute the last command for every user that is named John but It does not print anything.
Insert a whitespace after last:
ypcat passwd | awk -F":" '/:John / {system("last " $1)}'
Your approach is wrong, awk is not shell. awk is designed to manipulate text not to call other tools from, that is what a shell is for This may be what you want, depending on what last is/does:
ypcat passwd | awk -F":" '/:John /{print $1}' | xargs last
or:
ypcat passwd | awk -F":" '/:John /{print $1}' | xargs -n 1 last

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

No output when using awk inside bash script

My bash script is:
output=$(curl -s http://www.espncricinfo.com/england-v-south-africa-2012/engine/current/match/534225.html | sed -nr 's/.*<title>(.*?)<\/title>.*/\1/p')
score=echo"$output" | awk '{print $1}'
echo $score
The above script prints just a newline in my console whereas my required output is
$ curl -s http://www.espncricinfo.com/england-v-south-africa-2012/engine/current/match/534225.html | sed -nr 's/.*<title>(.*
?)<\/title>.*/\1/p' | awk '{print $1}'
SA
So, why am I not getting the output from my bash script whereas it works fine in terminal am I using echo"$output" in the wrong way.
#!/bin/bash
output=$(curl -s http://www.espncricinfo.com/england-v-south-africa-2012/engine/current/match/534225.html | sed -nr 's/.*<title>(.*?)<\/title>.*/\1/p')
score=$( echo "$output" | awk '{ print $1 }' )
echo "$score"
Score variable was probably empty, since your syntax was wrong.

Resources