-1

I do not want to just print value of variables using awk. Actually i want to print value of variables in specific columns of a file. I already tried the link "How to use shell variables in an awk script"

Below is my requirement.

I have two variables and i want to print the values of these variables in specific columns. I have tried below to print the value of variables but it is not giving the desired result.

awk '$2 = $2 FS "$test" infile

From link "How to use shell variables in an awk script" i can understand how to print value of variables by my requirement is to print the values of two variables in specific columns. For example i have two variables and i need to print the value of both variables in column 3 and 4.

Below is the example.

infile

2017/03/31-465       17:00:22 01:20:18 Completed                        63249705
2017/03/30-490       17:00:19 00:15:17 Completed                        12205725
2017/03/29-502       17:00:19 00:15:12 Completed                        12108819
2017/03/28-496       17:00:20 00:14:58 Completed                        12060389
2017/03/27-487       17:00:24 00:15:29 Completed                        11990615
2017/03/26-76        17:00:22 32:05:10 Completed                        62974897
2017/03/24-465       17:00:22 01:24:26 Completed                        62974897
2017/03/23-494       17:00:16 00:14:30 Completed                        11970151
2017/03/22-489       17:00:19 00:14:49 Completed                        11923575
2017/03/21-472       17:00:19 00:14:52 Completed                        11878081
2017/03/20-463       17:00:20 02:21:25 Aborted                                 0
2017/03/12-78        17:00:19 32:05:16 Completed                        62466091

I have below 2 variables and i want to print the values of both variables in column say 3 and 4.

variables : - test and test2

I tried using below code . But it print $test instead of values of variables test. When i gone through link "How to use shell variables in an awk script" and able to print the values of variable but not able to print in specific columns of file.

awk '$3 = $3 FS "$test"' infile
Thor
  • 45,082
  • 11
  • 119
  • 130

1 Answers1

0

If you want to use the value of shell variable in awk, you should do in this way:

awk -v value="$test" '$3=$3 FS value ....' input

If you used $test directly in awk, awk think that it references a column, the column index is in (awk variable) test, default it is 0, which would be $0(the whole line).

Kent
  • 189,393
  • 32
  • 233
  • 301