Unable to pass shell variables into awk [duplicate] - bash

This question already has answers here:
How to use awk variables in regular expressions?
(5 answers)
Closed 6 years ago.
I am trying to get data between sequences of numbers, for instance:
100000000
1 2 3 4
5 6 7 8
....
100001000
....
200000000
I match the patterns using awk and I can use it successfully without using variables, for instance:
awk '/10000000 /{flag=1;next}/10001000/{exit}flag' input.dat
However when I try to use shell variables within this command, it gives no output whatsoever:
for i in {1..4}
do
step1=$(($i*10000000))
step2=$(($step1+1000))
awk -v arg1="$step1" -v arg2="$step2" '/arg1 /{flag=1;next}/arg2 /{exit}flag' input.dat
done
Is there something obvious I'm missing?

Regex literal /.../ doesn't allow variables. You can use ~ operator for regex matching in regex with variables:
awk -v arg1="$step1" -v arg2="$step2" '$0 ~ arg1 " "{flag=1; next}
$0 ~ arg2{exit} flag' input.dat

Related

Is there any way to pass shell variable in awk pattern command like below?

Here's generic command I want to pass variable at DEFINE keyword place but failing
awk '/DEFINE/,/REPLACE/' file
Here's what I have tried
line=DEFINE
awk -v myvar=$line '/myvar/,/REPLACE/' file
The following should work:
line=DEFINE
awk -v myvar="$line" '($0~myvar),/REPLACE/{print}'
Example:
$ seq 1 10 | awk -v x=5 '($0~x),/8/'
5
6
7
8
See Passing variables to range patterns in awk for more details

Why does awk always print full lines? [duplicate]

This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed 3 years ago.
When attempting to use awk to get the process ID from the output of ps aux like so:
ps aux | awk "{ print $2 }"
No matter what number of row I attempt to print, awk always outputs the full line. I've never managed to get it to work properly. I'm using macOS which apparently uses a different type/version of awk, but I can't find an alternative syntax which might work.
Any advice would be greatly appreciated!
ps aux | awk '{ print $2 }'
Try that one

Extract Information From File Name in Bash [duplicate]

This question already has answers here:
How to split a string into an array in Bash?
(24 answers)
Closed 7 years ago.
Suppose I have a file with a name ABC_DE_FGHI_10_JK_LMN.csv. I want to extract the ID from the file-name i.e. 10 with the help of ID position and file-name separator. I have following two inputs
File-name_ID_Position=4; [since 10 is at fourth position in file-name]
File-name_Delimiter="_";
Here ID can be numeric or alpha-numeric. So how extract the 10 from above file with the help of above two inputs. How to achieve this in bash?
Instead of writing a regex in bash, I would do it with awk:
echo 'ABC_DE_FGHI_10_JK_LMN.csv' | awk -F_ -v pos=4 '{print $pos}'
or if you want the dot to also be a delimiter (requires GNU awk):
echo 'ABC_DE_FGHI_10_JK_LMN.csv' | awk -F'[_.]' -v pos=4 '{print $pos}'

How to write a bash and define awk constants in command line [duplicate]

This question already has answers here:
Using awk with variables
(3 answers)
Closed 8 years ago.
As a part of my bash, I want to pass some constant from command line to awk. For example, I want to subtract constant1 from column 1 and constant2 from column 5
$ sh bash.sh infile 0.54 0.32
#!/bin/bash
#infile = $1
#constant1 = $2
#constant2 = $3
cat $1 | awk '{print $1"\t"$2"\t"$3"\t"$4"\t"$5"\t"$6}'
thank you very much for your help
As awk is it's own language, by default it does not share the same variables as Bash. To use Bash variables in an awk command, you should pass the variables to awk using the -v option.
#!/bin/bash
awk -v constant1=$2 -v constant2=$3 '{print($1-constant1),($5-constant2)}' $1
You'll notice I removed cat as there is no need to pipe cat into awk since awk can read from files.
you need to remove gaps when defining vaariables:
#!/bin/bash
infile=$1
constant1=$2
constant2=$3
cat $1 | awk '{print $1 $2 $3 $4 $5 $6}'

How to pass variable to awk [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Using awk with variables
The following command is wrong, the point is I want to use $curLineNumber in awk, how can I do it? Any solution?
curLineNumber = 3
curTime=`ls -l | awk 'NR==$curLineNumber {print $NF}'`
Thanks
curTime=$(ls -l | awk -v line=$curLineNumber 'NR == line { print $NF }'
The -v option is used to specify variables initialized on the command line. I chose the name line for the awk variable.

Resources