You are not logged in.

#1 2011-11-27 14:57:00

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,677
Website

awk - insert an external variable [solved]

I want to incorporate the variable in the for statement as a column of my processed file.  In the INCORRECT example below, it is $i which corresponds to the i in my for loop:

for i in x86_64 i686; do
  awk '{ print $1" "$4" "$5" "$i }' $file-$i > processed-$i.log
done

Thanks!

EDIT: finally found the solution (choice of key words is critical to success) in this old thread.

for i in x86_64 i686; do
  awk -v arch=$i '{ print $1" "$4" "$5" "arch } ' $file-$i > processed-$i.log
done

Last edited by graysky (2011-11-27 15:11:08)


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#2 2011-11-27 15:21:13

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: awk - insert an external variable [solved]

I don't get your use of quotes. Wouldn't

awk -v arch=$i '{ print $1 $4 $5 arch }'

work?
"$5"  prints a literal "$5" here instead of what's in the 5th column.

Last edited by karol (2011-11-27 15:22:36)

Offline

#3 2011-11-27 15:26:26

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: awk - insert an external variable [solved]

The quotes are between the vars, not around them... better to use commas to separate:

awk -v arch=$i  '{ print $1, $4, $5, arch }'

Last edited by falconindy (2011-11-27 15:26:50)

Offline

#4 2011-11-27 15:28:46

keenerd
Package Maintainer (PM)
Registered: 2007-02-22
Posts: 647
Website

Re: awk - insert an external variable [solved]

What are you doing in a larger context?  I can't tell what is going on here, but if you are working with a list of packages then Expac would be better suited.

Offline

#5 2011-11-27 15:30:23

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: awk - insert an external variable [solved]

falconindy wrote:

The quotes are between the vars, not around them... better to use commas to separate:

awk -v arch=$i  '{ print $1, $4, $5, arch }'

A great lesson in perspective and code readability.
Thanks!

Offline

#6 2011-11-27 16:35:10

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,677
Website

Re: awk - insert an external variable [solved]

@falconindy - I thought of that but I wanted to use spaces for this application.  As you pointed out, the quotes are not to protect the vars, but to add the space between them.

@keenerd - just analyzing my unofficial repo log files to generate some statistics for users.  BTW, expac looks pretty slick!


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#7 2011-11-27 16:53:32

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: awk - insert an external variable [solved]

graysky wrote:

@falconindy - I thought of that but I wanted to use spaces for this application.

...and unless you mess with the OFS, that's exactly what it does.

Offline

Board footer

Powered by FluxBB