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

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.

Related

How to pass the awk variable in hdfs command [duplicate]

This question already has answers here:
How do I use shell variables in an awk script?
(7 answers)
Closed 4 years ago.
I am listing the files/directories which are greather than N days using the below commands
DATE=`date +%Y-%m-%d`
dt=`date --date "$dt" +%Y%m%d`
loop_dt=`date -I --date "$dt -1 day"`
*** output of loop_dt = 2018-02-25***
hdfs dfs -ls r /path/ | awk '$6 < "$loop_dt"'
I know the above hdfs command is wrong, But I want to pass the loop_dt varible in awk command, to know the list the files which are older than n days
Note: if I hardcode the date in awk command I am getting the results
To recap what I have said in the comments you need to fix your awk command in the following way:
$ cat file
2015-08-01
2015-08-13
$ awk -v var="2015-08-12" '{if( $1 < var"") print}' file
2015-08-01
Replace 2015-08-12 by your shell variable "$loop_dt" and it should work.
Explanations:
Use this syntax awk -v awkVarName="$shellVariable" 'BEGIN {print awkVarName}' to pass a variable to awk
In if( $1 < var"") the "" force the string comparison.

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

Linux operating system [duplicate]

This question already has answers here:
How to use variables in a command in sed?
(4 answers)
Closed 5 years ago.
I am a beginner at Linux and I'm trying to do a project which takes every line from a file.txt and replaces the third word with the first of each line. Here is my Shell code but it doesn't seem to work. It keeps replacing the third word with $field1 and not what's in it.
#!/bin/bash
while IFS=: read -r field1;do
sed -e 's/[^:]*[^:]/$field1/3'
done < file.txt
Try this, this will replace in the same file:
#!/bin/sh
while read -r line
do
first=`echo $line | awk -F':' '{ print $1 }'`
last=`echo $line | awk -F':' '{ print $3 }'`
echo $line | sed "s/$last/$first/"
done < file.txt
Input file :
ashish:is:good
navin:is:good
how:are:you
Output :
ashish:is:ashish
navin:is:navin
how:are:how
Make note of the single quotation marks. Place them around the field1 variable and so:
sed -e 's/[^:]*[^:]/'$field1'/3'

Using Variable in awk command [duplicate]

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

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

Resources