LinuxQuestions.org
Review your favorite Linux distribution.
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 01-15-2015, 08:00 PM   #1
lalith.145
LQ Newbie
 
Registered: Jan 2015
Posts: 1

Rep: Reputation: Disabled
problem in comparing awk field variable with stored value variable


Hi,

My value is stored in a variable j in unix.I want to compare it with $1 of awk like awk '$1 = $j'.I need first column values that matches with the value in j.Please suggest.

Thanks
Lalith
 
Old 01-15-2015, 08:19 PM   #2
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
With this InFile ...
Code:
4 7 2 15 6 9 1
1 3 55 77 9
1 2 3 4 6 7 8 9
2     5 5 5 5 5 5
... this code ...
Code:
j=5
awk -v j=$j '{k=index($0,j);
  if (k>0) print "In line",NR,"the first",j,"was in position",k;
  else print "In line",NR,"there was no match on",j'} $InFile >$OutFile
... produced this OutFile ...
Code:
In line 1 the first 5 was in position 8
In line 2 the first 5 was in position 5
In line 3 there was no match on 5
In line 4 the first 5 was in position 7
Daniel B. Martin
 
Old 01-15-2015, 08:46 PM   #3
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
With this InFile ...
Code:
4 7 2 15 6 9 1
1 3 55 77 9
1 2 3 4 6 7 8 9
2     5 5 5 5 5 5
... this code ...
Code:
j=55
awk -v j=$j '{for (k=1;k<=NF;k++)
  if ($k==j) {print "In line",NR,"the first column which equalled",j,"was number",k; break}
  if (k>NF)  print "In line",NR,"there was no column which equalled",j'} $InFile >$OutFile
... produced this OutFile ...
Code:
In line 1 there was no column which equalled 55
In line 2 the first column which equalled 55 was number 3
In line 3 there was no column which equalled 55
In line 4 there was no column which equalled 55
Daniel B. Martin
 
  


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
[SOLVED] Using cut or awk to strip a field from a variable.. HELP Kustom42 Programming 2 03-15-2012 03:47 PM
internal awk variable Field seperator casperdaghost Linux - Newbie 5 10-23-2011 11:12 AM
problem while comparing awk field variable with input variable entered using keyboard vinay007 Programming 12 08-23-2011 12:44 AM
AWK a variable Ouptut to a new variable and using the new variable with the old one alertroshannow Linux - Newbie 4 02-16-2009 12:08 AM
Scripting: accessing a variable stored in a variable? tomolesonjr Linux - Newbie 5 05-05-2006 08:47 PM

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

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