Using Variable in awk command [duplicate] - linux

This question already has answers here:
How do I use shell variables in an awk script?
(7 answers)
Closed 6 years ago.
I want to add a word at the end of each line in my text file which is stored in variable. whenever i execute shell script instead of concatenate content stored in variable variable itself get concatenated. Below is the example for same:
Input:
cat output2.txt
12345
att1=Ramesh^Mumbai
awk '{print $0"^$att1"}' output2.txt >output3.txt
output:
12345^att1
Desired Output:
12345^Ramesh^Mumbai

Try this:
awk -v att1='Ramesh^Mumbai' -v OFS='^' '{print $0,att1}'
-v option allows to pass variable to awk
OFS is the output field separator (that will replace the , in the print statement by ^)

man awk:
-v var=val
Assign the value val to the variable var, before execution of
the program begins. Such variable values are available to the
BEGIN block of an AWK program.
you can use this;
#!/bin/bash
att1=Ramesh^Mumbai
awk -v att1=$att1 '{print $0"^"att1}' output.txt > output3.txt
Example;
user#host:/tmp$ cat output.txt
12345
abab
dafadf
adfaf
user#host:/tmp$, ./test.sh
user#host:/tmp$ cat output3.txt
12345^Ramesh^Mumbai
abab^Ramesh^Mumbai
dafadf^Ramesh^Mumbai
adfaf^Ramesh^Mumbai

Related

how to print nth column in a file using awk [duplicate]

This question already has answers here:
How do I use shell variables in an awk script?
(7 answers)
Closed 5 years ago.
How to print nth column in a file using awk?To print the second column of file, I try:
#!/bin/bash
awk '{print $2}' file
But if n is a variable, how to print nth column of file using awk?
#!/bin/bash
n=2
n=2
awk -v var="$n" '{print $var}' file
Give a try this, notice the -v option:
#!/bin/bash
n=3
awk -v x=${n} '{print $x}' file
From the man page:
The option -v followed by var=value is an assignment to be done before
prog is executed; any number of -v options may be present.
For more examples, you could check Using Shell Variables in Programs

How to match to a shell variable in awk? [duplicate]

This question already has answers here:
How do I use shell variables in an awk script?
(7 answers)
Closed 6 years ago.
I am trying to match a shell variable with the second column separated by commas.
So I've set $INOW to ls and the command looks like this
awk -F"," '$2 == $INOW {print "yeah it's there"}' commands.csv
I have tried putting quotes around $INOW. no difference. How do i make a match to the shell variable?
You cant use shell variables directly in awk. Rather you can create an awk variable using -v
Example
$ a_variable=hello
$ awk -v var="$a_variable" "{print var}"
hello
So in your case you can write like,
$ awk -F"," -v inow="$INOW" '$2 == inow {print "yeah its there"}' commands.csv
awk -F"," '{if($2 == '$INOW') {print "yeah its there"}else{print "nope"}}' commands.csv

Assign output of a shell command to a variable [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 2 years ago.
I want to assign the output of a shell command to a variable.
If I directly echo the command, the code will execute correctly:
for ((i=0; i<${#result[#]}; i++)); do
echo ${result[$i]} | awk '{print $1}'
done
But, if I assign it to a variable,
size=`${result[$i]} | awk '{print $1}'`
echo $size
Or
size=$(${result[$i]} | awk '{print $1}')
echo $size
They are not working.
How can I fix it?
You missed the echo
size=$(echo ${result[$i]} | awk '{print $1}')
Here the output the the echo is passed as input to the awk
The $() or back ticks just run the command and assign it to a variable, so when you just write
${result[$i]} | awk '{print $1}'
it won't give you anything as nothing is passed as input to the awk command.

Not able to get any output from awk command in shell script [duplicate]

This question already has answers here:
Using awk with variables
(3 answers)
Closed 8 years ago.
I am trying to write a shell script to get certain data from below sample logs..
Below is a sample log:
2014-07-08 16:08:25,684: |ABC_130|1|10123ffffff2|P|489440201
2014-07-08 17:08:25,684: |ABC_130|1|aaaaaxxxxaab|P|489440201
2014-07-08 19:08:25,684: |ABC_130|1|aaaaababbaab|P|489440201
Below is a part of the script where I am facing issue, the issue I am facing is that the awk command doesn't give any output.
#!/bin/sh
DATE_HOUR="`date -d '1 hour ago' "+%Y-%m-%d %H"`"
awk -F ":" '{if ($1='"$DATE_HOUR"') print $0}' log.txt
Don't use shell variable like that in awk. Use -v name=val:
awk -F ":" -v dt="$DATE_HOUR" '$1==dt' log.txt
btw I reduced your awk command to '$1==dt' since print $0 is default action and also if condition can be moved out of curly braces.

Pass parameter to an awk script file

If I want to pass a parameter to an awk script file, how can I do that ?
#!/usr/bin/awk -f
{print $1}
Here I want to print the first argument passed to the script from the shell, like:
bash-prompt> echo "test" | ./myawkscript.awk hello
bash-prompt> hello
In awk $1 references the first field in a record not the first argument like it does in bash. You need to use ARGV for this, check out here for the offical word.
Script:
#!/bin/awk -f
BEGIN{
print "AWK Script"
print ARGV[1]
}
Demo:
$ ./script.awk "Passed in using ARGV"
AWK Script
Passed in using ARGV
You can use -v as a command-line option to provide a variable to the script:
Say we have a file script.awk like this:
BEGIN {print "I got the var:", my_var}
Then we run it like this:
$ awk -v my_var="hello this is me" -f script.awk
I got the var: hello this is me
your hash bang defines the script is not shell script, it is an awk script. you cannot do it in bash way within your script.
also, what you did : echo blah|awk ... is not passing paramenter, it pipes the output of echo command to another command.
you could try these way below:
echo "hello"|./foo.awk file -
or
var="hello"
awk -v a="$var" -f foo.awk file
with this, you have var a in your foo.awk, you could use it.
if you want to do something like shell script accept $1 $2 vars, you can write a small shellscript to wrap your awk stuff.
EDIT
No I didn't misunderstand you.
let's take the example:
let's say, your x.awk has:
{print $1}
if you do :
echo "foo" | x.awk file
it is same as:
echo "foo"| awk '{print $1}' file
here the input for awk is only file, your echo foo doesn't make sense. if you do:
echo "foo"|awk '{print $1}' file -
or
echo "foo"|awk '{print $1}' - file
awk takes two input (arguments for awk) one is stdin one is the file, in your awk script you could:
echo "foo"|awk 'NR==FNR{print $1;next}{print $1}' - file
this will print first foo from your echo, then the column1 from file of course this example does nothing actual work, just print them all.
you can of course have more than two inputs, and don't check the NR and FNR, you could use the
ARGC The number of elements in the ARGV array.
ARGV An array of command line arguments, excluding options and the program argument, numbered from zero to ARGC-1
for example :
echo "foo"|./x.awk file1 - file2
then your "foo" is the 2nd arg, you can get it in your x.awk by ARGV[2]
echo "foo" |x.awk file1 file2 file2 -
now it is ARGV[4] case.
I mean, your echo "foo"|.. would be stdin for awk, it could by 1st or nth "argument"/input for awk. depends on where you put the -(stdin). You have to handle it in your awk script.

Resources