LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 12-05-2008, 04:05 AM   #1
bugg_deccan
LQ Newbie
 
Registered: Oct 2007
Posts: 6

Rep: Reputation: 0
awk comparing a column value with a stored variables value


hi,

I have a value stored in a variable. I want to call that variable's value and equate it with a column in awk.
Code:
dt=$(ls -ltr *.Z|head -1|awk '{print $7}')
ls -ltr|awk '($7==$dt){print "mv "$9" /ANOTHER/LOCATION"}'>mov_tzt.sh
But this is throwing an error. Can any one help me out please

Last edited by bugg_deccan; 12-05-2008 at 04:44 AM.
 
Old 12-05-2008, 05:25 AM   #2
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
To pass the value of a shell variable to awk you have to use the -v option. For example:
Code:
awk -v dt=$dt 'the_awk_program_here'
the reason is that you embed the awk program in single quotes and they prevent the shell to do any substitution, that is the string "$dt" is interpreted literally. Take a look here and here for more details.

Last edited by colucix; 12-05-2008 at 05:28 AM.
 
Old 12-05-2008, 05:39 AM   #3
bugg_deccan
LQ Newbie
 
Registered: Oct 2007
Posts: 6

Original Poster
Rep: Reputation: 0
Hi colucix

Thanx for the reply.

I edited the code
Code:
dt=$(ls -ltr *.Z|head -1|awk '{print $7}')
ls -ltr|awk '($7=='$dt'){print "mv "$9" /ANOTHER/LOCATION"}'>mov_tzt.sh
Now the script is workin cool.
 
Old 12-05-2008, 06:04 AM   #4
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
Very good!
 
Old 12-05-2008, 07:08 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by bugg_deccan View Post
hi,

I have a value stored in a variable. I want to call that variable's value and equate it with a column in awk.
Code:
dt=$(ls -ltr *.Z|head -1|awk '{print $7}')
ls -ltr|awk '($7==$dt){print "mv "$9" /ANOTHER/LOCATION"}'>mov_tzt.sh
But this is throwing an error. Can any one help me out please
Code:
ls -ltr *.txt|awk 'NR==1{dt=$7}$7==dt{print "mv "$9" /ANOTHER/LOCATION"}'
 
  


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
Multiplying data column in awk - can rounding be suppressed? johnpaulodonnell Linux - Newbie 2 02-28-2007 07:56 AM
where are the variables stored in the kernel? shendeswanand Linux - Software 1 12-30-2005 02:14 PM
awk column printing schneidz Programming 7 09-29-2005 06:14 AM
where are the environment variables stored? ojav Linux - Newbie 3 05-31-2005 04:00 PM
commands stored into variables? benne Programming 2 11-15-2004 07:20 PM

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

All times are GMT -5. The time now is 06:27 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