save output of awk into a variable in a shell script - shell

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

Related

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.

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`

shell scripting: how to cut line in variable

I'm new to shell scripting. I've tried to search online, but I couldn't find what I was looking for - how do I cut a variable to get the value I'm looking for?
for example I have:
Result=`awk -F : -v "Title=" -v "Author=" 'tolower() == tolower(Title) && tolower() == tolower(Author)' BookDB.txt`
//which will return:
//Result= Black:Hat:12.30:20:30
I've tried doing this, but it won't work:
PRICE= cut -d ":" -f 3 $Result
Any help would be appreciated, thanks!
Your code is not wrong ... well, at least most of it!
Doing echo 'Result= Black:Hat:12.30:20:30' | cut -d ":" -f 3 will give the result 12.30.
The issue is that you probably want to use it on a shell script.
To do that just try the following:
PRICE=`cut -d ":" -f 3 $Result`
What I did was basically putting ` before and after the expression that you want to store in your variable.
Reference to learn more: http://www.freeos.com/guides/lsst/ch02sec08.html
Best of luck!
How about using awk...
input="Black:Hat:12.30:20:30"
first=$(echo input | awk -F":" '{print $1}')
echo $first
second=$(echo $input | awk -F":" '{print $2}')
echo $second
date=$(echo $input | awk -F":" '{print $3 ":" $4 ":" $5}')
echo $date
You might have to edit to fit your exact requirements.

How to replace the nth column/field in a comma-separated string using sed/awk?

assume I have a string
"1,2,3,4"
Now I want to replace, e.g. the 3rd field of the string by some different value.
"1,2,NEW,4"
I managed to do this with the following command:
echo "1,2,3,4" | awk -F, -v OFS=, '{$3="NEW"; print }'
Now the index for the column to be replaced should be passed as a variable. So in this case
index=3
How can I pass this to awk? Because this won't work:
echo "1,2,3,4" | awk -F, -v OFS=, '{$index="NEW"; print }'
echo "1,2,3,4" | awk -F, -v OFS=, '{$($index)="NEW"; print }'
echo "1,2,3,4" | awk -F, -v OFS=, '{\$$index="NEW"; print }'
Thanks for your help!
This might work for you:
index=3
echo "1,2,3,4" | awk -F, -v OFS=, -v INDEX=$index '{$INDEX="NEW"; print }'
or:
index=3
echo "1,2,3,4" | sed 's/[^,]*/NEW/'$index
Have the shell interpolate the index in the awk program:
echo "1,2,3,4" | awk -F, -v OFS=, '{$'$index'="NEW"; print }'
Note how the originally single quoted awk program is split in three parts, a single quoted beginning '{$', the interpolated index value, followed by the single quoted remainder of the program.
Here's a seductive way to break the awkwardness:
$ echo "1,2,3,4" | sed 's/,/\n/g' | sed -e $index's/.*/NEW/'
This is easily extendable to multiple indexes just by adding another -e $newindex's/.*/NEWNEW/'
# This should be faster than awk or sed.
str="1,2,3,4"
IFS=','
read -a f <<< "$str"
f[2]='NEW'
printf "${f[*]}"
With plain awk (I.E. Not gawk etc) I believe you'll have to use split( string, array, [fieldsep] ); change the array entry of choice and then join them back together with sprintf or similar in a loop.
gawk allows you to have a variable as a field name, $index in your example. See here.
gawk is usually the default awk on Linux, so change your invocation to gawk "script" and see if it works.

Problem with backticks in shellscript

I am having a problem getting my shellscript working using backticks. Here is an example version of the script I am having an issue with:
#!/bin/sh
ECHO_TEXT="Echo this"
ECHO_CMD="echo ${ECHO_TEXT} | awk -F' ' '{print \$1}'"
result=`${ECHO_CMD}`;
echo $result;
result=`echo ${ECHO_TEXT} | awk -F' ' '{print \$1}'`;
echo $result;
The output of this script is:
sh-3.2$ ./test.sh
Echo this | awk -F' ' '{print $1}'
Echo
Why does the first backtick using a variable for the command not actually execute the full command but only returns the output of the first command along with the second command? I am missing something in order to get the first backtick to execute the command?
You need to use eval to get it working
result=`eval ${ECHO_CMD}`;
in place of
result=`${ECHO_CMD}`;
Without eval
${ECHO_TEXT} | awk -F' ' '{print \$1}
which will be expanded to
Echo this | awk -F' ' '{print \$1}
will be treated as argument to echo and will be output verbatim. With eval that line will actually be run.
You Hi,
you need to know eval command.
See :
#!/bin/sh
ECHO_TEXT="Echo this"
ECHO_CMD="echo ${ECHO_TEXT} | awk -F' ' '{print \$1}'"
result="`eval ${ECHO_CMD}`"
echo "$result"
result="`echo ${ECHO_TEXT} | awk -F' ' '{print $1}'`"
echo "$result"
Take a look to the doc :
help eval
In your first example echo is parsing the parameters - the shell never sees them. In the second example it works because the shell is doing the parsing and knows what to do with a pipe. If you change ECHO_CMD to be "bash echo ..." it will work.
Bash is escaping your command for you. Try
ECHO_TEXT="Echo this"
ECHO_CMD='echo ${ECHO_TEXT} | awk -F" " "'"{print \$1}"'"'
result=`${ECHO_CMD}`;
echo $result;
result=`echo ${ECHO_TEXT} | awk -F' ' '{print \$1}'`;
echo $result;
Or even better, try set -x on the first line, so you see what bash is doing

Resources