The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Accessing Shell Variables in awk or sed nasersh Shell Programming and Scripting 3 05-05-2008 04:44 AM
shell variables and sed substitution duderonomy UNIX for Advanced & Expert Users 2 05-04-2008 11:54 PM
variables in shell viko Shell Programming and Scripting 2 03-03-2008 08:09 PM
Shell Script Variables ragha81 Shell Programming and Scripting 3 08-22-2006 12:43 AM
bash shell variables xNYx UNIX for Dummies Questions & Answers 3 04-15-2002 01:36 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 08-22-2006
Registered User
 

Join Date: Aug 2006
Location: Hobart Tasmania
Posts: 58
Using shell variables In awk

Oh its not my day for syntax...

Code:
cat gzipsize.txt | awk '{print "echo",$1,$2} > master.txt
I have read a lot about the awk -v but haven't been able to get it to work. I have a variable in my script and I'm looking just to push it into the awk after the $2 (or anywhere would do)!!!

Every example of this has led me down a merry road and I'm wondering if anyone can actually explain this finer point to me?

let's say there is a previous

var=filecontent
Reply With Quote
Forum Sponsor
  #2  
Old 08-23-2006
Registered User
 

Join Date: Aug 2006
Location: Hobart Tasmania
Posts: 58
Question

I have found that the server I'm operating on has some anomolies in its implementation of UNIX so if I could possibly ask a favour of someone out there with awk skills...

would it be possible to write a small piece of example code 'which works' where a similar variable is imported and used in an awk function?

i ask this because I have every feeling that once again this code will work on every other unix installation and not the one we have here... if so I need to sort it out with the sys admin asap

but without some real working code as an example I won't have a chance of explaining.

So could anyone possibly run a working example past me?

possibly

var=varname

echo 10 12 | awk 'BEGIN {VAR=var}{print "something",$1,$2,$VAR}'


or something similar?
Reply With Quote
  #3  
Old 08-23-2006
Registered User
 

Join Date: Jun 2006
Location: Delhi, India
Posts: 88
I think you want this:


Code:
var="hello"
var2="myname"

echo $var2 | awk -v variable=${var} '{ print variable,$1}'
Output:
hello myname

your code shoule be

echo 10 12 | awk -v VAR=${var} '{print "something",$1,$2,VAR}'

Output:
something 10 12 hello
Reply With Quote
  #4  
Old 08-23-2006
Registered User
 

Join Date: Aug 2006
Location: Hobart Tasmania
Posts: 58
Thumbs up

thanks vish, its as I feared then and once again I find I've wasted a day because our beloved server is set up wrong...

my output from your code is

var=”hello”: command not found
var=”myname”: command not found
var2: undefined variable

and the output from your improvement on my line is

var: undefined variable

So I've sat here another full day trying to get things to work because our sys admin has rocks in his head... very sad and frustrating indeed.

thanks a million for that snippet of code, without which I couldn't properly explain the anomoly to the tech support and sys admins - so greatly appreciated.

i think i need to go to bed now.
Reply With Quote
  #5  
Old 08-23-2006
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,297
what shell are you using? Most of the examples you got used /usr/bin/sh or /usr/bin/bash or /usr/bin/ksh.
Please show output from:
Code:
usr/bin/echo "$SHELL"
usr/bin/echo "$0"
Reply With Quote
  #6  
Old 08-23-2006
reborg's Avatar
Administrator
 

Join Date: Mar 2005
Location: Ireland
Posts: 3,638
these are csh/tcsh responses

Quote:
Originally Posted by nortypig
var=”hello”: command not found
var=”myname”: command not found
var2: undefined variable

and the output from your improvement on my line is

var: undefined variable
Reply With Quote
  #7  
Old 08-23-2006
Registered User
 

Join Date: Aug 2006
Location: Hobart Tasmania
Posts: 58
Thumbs up

I've had my brother in law look at this - its based around a university assignment... there are some basic stuff being fooled with like not being able to set variables

var="troy"

but i can put them in a text file and make them variables with a cat into the variable

anyway we've experimented and it appears the shell has been limited for some reason... in ksh

var="troy"

has no problem

so sorry for the bother everyone - this one seems to be something I need to take back to the lecturers. I've found ways to get around the limitations of what's going on but the time factor is killing me from the business end... had to knock back a good contract yesterday.

thanks for the help, it was crucial to my understanding and invaluable to my being able to successfully get past the hurdles.

no doubt i will have more questions on the weekend
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 12:35 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0