LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-03-2008, 12:23 PM   #1
DoME69
Member
 
Registered: Jan 2008
Posts: 189

Rep: Reputation: 16
awk & variable...


how can i use variable at awk?

i try this..

set go = goooooo
awk '{print "aaa", $1, $go}' file1

but it doesn't work.
 
Old 10-03-2008, 02:15 PM   #2
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by DoME69 View Post
Code:
set go = goooooo
awk '{print "aaa", $1, $go}' file1
The single quotes surrounding your awk expression prevent it from accessing anything but its internal parameter variables (e.g. $1). You can just rewrite the awk line as
Code:
set go = goooooo
awk '{print "aaa", $1, $2}' file1 $go
 
Old 10-03-2008, 03:20 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by David1357 View Post
Code:
set go = goooooo
awk '{print "aaa", $1, $2}' file1 $go
Unless I'm totally drunk, this should look for a file named "goooooo". I'd use the -v option, instead:
Code:
awk -v go=$go '{print "aaa", $1, go}' file1
or eventually shell variable substitution through quoting:
Code:
awk '{print "aaa", $1, "'"$go"'"}' file1
but this is more difficult to deal with.
 
Old 10-03-2008, 05:49 PM   #4
DoME69
Member
 
Registered: Jan 2008
Posts: 189

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by colucix View Post
Unless I'm totally drunk, this should look for a file named "goooooo". I'd use the -v option, instead:
Code:
awk -v go=$go '{print "aaa", $1, go}' file1
or eventually shell variable substitution through quoting:
Code:
awk '{print "aaa", $1, "'"$go"'"}' file1
but this is more difficult to deal with.


Thanks a lot.
 
Old 10-10-2008, 02:47 AM   #5
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by colucix View Post
Unless I'm totally drunk, this should look for a file named "goooooo".
You are correct. My apologies. When I re-read my original answer I wondered what I was thinking. Thanks for picking up the ball I dropped.
 
Old 10-12-2008, 08:44 AM   #6
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
Moin,

another way to do it:
Code:
awk ' ...' go=$go file
Jan
 
Old 10-12-2008, 09:38 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Well, the difference between the two methods is that using the -v option the variable is assigned at the very beginning of the awk script, even before the execution of the BEGIN block (if any). The method suggested by jan61 is useful when you have to parse multiple files and use different values of the variable. The variable is assigned depending on its position between the input files, e.g.
Code:
awk 'some_awk_code_here' go=gooooo file1 go=staaaay file2
in this example the variable go will have the value "gooooo" before processing file1 and the value "staaaay" before processing file2.
 
Old 10-13-2008, 04:32 PM   #8
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
Moin,

Quote:
Originally Posted by colucix View Post
...
Code:
awk 'some_awk_code_here' go=gooooo file1 go=staaaay file2
in this example the variable go will have the value "gooooo" before processing file1 and the value "staaaay" before processing file2.
that's interesting. I never used it this way and didn't know about the difference. Thanks for the explanation.

Jan
 
Old 10-13-2008, 04:44 PM   #9
robel
Member
 
Registered: Oct 2008
Location: Norway
Distribution: Slackware
Posts: 77

Rep: Reputation: 19
Quote:
Originally Posted by DoME69 View Post
how can i use variable at awk?

i try this..

set go = goooooo
awk '{print "aaa", $1, $go}' file1

but it doesn't work.
Are you trying to access environment variables?

You can access environment variables via the built-in variable ENVIRON

Example:

awk 'BEGIN{ print ENVIRON["TERM"] }'

It would print xterm on my computer.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to make 2 variables from one variable value in awk intikhabalam Linux - General 1 07-30-2008 04:32 AM
How to tranfer variable out from awk? fanbin Programming 12 07-31-2007 07:21 AM
get variable out from awk sharathkv25 Programming 1 05-08-2007 06:33 PM
Bash Awk Variable problem _hadi_ Programming 5 12-13-2006 12:25 AM
awk on a variable onradius Programming 8 02-22-2006 08:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration