0

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

0

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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}' 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.