Assign AWK result to variable
The following works correctly on bash:
a=$(echo '111 222 33' | awk '{print $3;}' )
echo $a # result is "33"
Another option would be to convert the string to an array:
a="111 222 333"
b=($a)
echo ${b[2]} # returns 333
How to assign awk result to an variable
You need to avoid spaces around the equal symbol. Since backticks are deprecated, you could put your code inside $(..)
block.
variable=$(awk -F: '$1=="{root}" {print $3}' /etc/passwd)
How to assign awk result variable to an array and is it possible to use awk inside another awk in loop
This may be what you're trying to do (using gawk for FPAT as you already were doing) but without more representative sample input and the expected output it's a guess:
$ cat tst.sh
#!/usr/bin/env bash
awk '
BEGIN {
OFS = ","
FPAT = "[^"OFS"]*|\"[^\"]*\""
}
NR > 1 {
n = split($5,name,/\s*/)
$7 = tolower(substr(name[1],1,1) name[n]) "@example.com"
print
}
' "${@:--}"
$ ./tst.sh test.csv
1,1,,,Name surname,department1 department2 department3,nsurname@example.com,
2,1,,,name Surname,department1,nsurname@example.com,
3,2,,,Name Surname,"department1 department2, department3",nsurname@example.com,
I put the awk script inside a shell script since that looks like what you want, obviously you don't need to do that you could just save the awk script in a file and invoke it with awk -f
.
Assign awk output to variable
You should use command substitution like follows:
#!/bin/sh
KEY="title"
statu=$(curl https://jsonplaceholder.typicode.com/todos/1 | awk -F'[,:}]'
'{for(i=1;i<=NF;i++){if($i~/'$KEY'\042/){print $(i+1)}}}' | tr -d '"')
echo "This is status" "$statu"
Saving awk output to variable
#!/bin/bash
variable=`ps -ef | grep "port 10 -" | grep -v "grep port 10 -" | awk '{printf $12}'`
echo $variable
Notice that there's no space after the equal sign.
You can also use $()
which allows nesting and is readable.
Assigning output from awk to variable
Use command substitution to assign the output of a command to a variable.
The syntax is: var1=$(command)
.
result=$(awk 'BEGIN{s=100} END {print s}' /dev/null)
echo "The result is $result"
Assign awk output to awk variable
You can try using getline
into a variable ( http://www.gnu.org/software/gawk/manual/gawk.html#Getline_002fVariable_002fPipe )
("/production/bin/title_case.awk "old_name) | getline new_name
How to pass AWK output into variable?
Use command substitution to capture the output of a process.
#!/bin/sh
VAR="$(awk 'BEGIN{RS=ORS="\n\n";FS=OFS="\n"}/FileHeader/' /root/Desktop/logs/Default.log)"
echo "$VAR"
some general advice with regards to shell scripting:
- (almost) always quote every variable reference.
- never put spaces around the equals sign in variable assignment.
UNIX for loop using awk to assign a variable
$ awk '{print $1, "and", $2}' file
xyz and 123
abc and 456
pqr and 789
$ sed 's/ / and /' file
xyz and 123
abc and 456
pqr and 789
If that's not all you want then edit your question to clarify your requirements and provide a more truly representative example.
Related Topics
Where Is the System Call Table in Linux Kernel
Sys_Execve System Call from Assembly
How to Programmatically Create Videos
Execute Sudo Command on Linux from Plink.Exe on Windows
Combine a Shell Script and a Zip File into a Single Executable for Deployment
Easy Way of Installing Eclipse Plugins on Ubuntu
Getting Disconnection Notification Using Tcp Keep-Alive on Write Blocked Socket
Does Creating a Symbolic Link to Another Symbolic Link Have Any Side-Effects
Error: Ld.So: Object Ld_Preload Cannot Be Preloaded: Ignored
How to Reinstall the Latest Cmake Version
Use Ssh to Start a Background Process on a Remote Server, and Exit Session
Difference Between Patch and Diff Files
Error: Service "Xxx" Uses an Undefined Network "Xxx"
How to Install Anaconda/Miniconda on Linux Silently
Embedding an Icon in a Linux Executable
Centos Error - Sudo: Effective Uid Is Not 0, Is Sudo Installed Setuid Root