LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 12-09-2013, 11:46 PM   #1
mhicky05
LQ Newbie
 
Registered: Dec 2013
Posts: 2

Rep: Reputation: Disabled
Question Passing a variable to AWK


Hello Experts,

I am newbie in UNIX shell scripting. I am more of an application administrator but I find shell scripting very good specially for automation of some stuff. I hope my question is valid.

I am having trouble with AWK command, I never thought this 3 letter command will have that complexity.

My logic is to pass a variable as an FNR value in awk

Code:
#!/usr/bin/sh
       
cFile1=`wc -l ${File1}|awk '{print$1}'`

echo "`awk -v m=${cFile1} 'FNR == m {print $1}' ${FILE1}`"
However, the "echo" part of the code seems not working. Is there anyone who can help me on this?

Thank you so much in advance! :-)

Last edited by colucix; 12-10-2013 at 01:46 AM. Reason: Added CODE tags
 
Old 12-10-2013, 12:40 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
what does it really mean:
echo "`awk command`"
you could simply write
awk command.
 
Old 12-10-2013, 01:49 AM   #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
The main problem in your code is that you reference FILE1 instead of File1 in the echo/awk statement. Provided this is the main caveat, it looks like you want to extract the first field from the last line of a file, so why not simply do something like this?
Code:
#!/usr/bin/sh
awk 'END{print $1}'
 
Old 12-10-2013, 02:04 AM   #4
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 665

Rep: Reputation: Disabled
@colucix and @pan_64 have already posted the relevant info so for further needs, try telling us what you are trying to achieve with this ?
 
Old 12-10-2013, 04:02 AM   #5
mhicky05
LQ Newbie
 
Registered: Dec 2013
Posts: 2

Original Poster
Rep: Reputation: Disabled
Lightbulb

Hi Colucix,

Very great! That is the culprit! BTW, thank you for your suggestion RE: my syntax.

This is just part of my logic. The real program should be able to check a string from FILE1 to FILE2 and compare if they have the same values. If not, then it will state which line of the text the string is located. I need to do this to automate the checking of the parameters of our application.

I didn't put the whole program because someone might say that I'm asking to do my homework :-).

Thanks to other contributors as well. Happy holidays!
 
Old 12-10-2013, 07:03 PM   #6
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 665

Rep: Reputation: Disabled
Quote:
Originally Posted by mhicky05 View Post
Hi Colucix,

I didn't put the whole program because someone might say that I'm asking to do my homework :-).

Thanks to other contributors as well. Happy holidays!
Well it would be great if you have shared what you tried till now, anyhow we will be asking for it !! :P we will not do your homework but we can help suggesting best way to achieve your task out of what you tried.
 
  


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
problem while comparing awk field variable with input variable entered using keyboard vinay007 Programming 12 08-23-2011 12:44 AM
[SOLVED] awk: how can I assign value to a shell variable inside awk? quanba Programming 6 03-23-2010 02:18 AM
passing awk output to $variable for shell script? NewnanNOC Programming 3 10-23-2008 02:08 PM
passing a shell variable into awk - syntax for correct interpretation? johnpaulodonnell Programming 3 06-19-2007 12:13 PM
passing passing variable in Java as reference djgerbavore Programming 3 11-10-2004 02:18 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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