LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-15-2010, 03:03 PM   #1
ghantauke
Member
 
Registered: Nov 2010
Posts: 114

Rep: Reputation: 6
awk question


How do i convert variable to string?

For example:

awk '$1 == "of"' {print $2} file.txt
This works since if the file has the word of in the first column.

awk '$1 == $'x' {print $2}' file.txt
This doesn't work even if the variable x=of. even if x="of".

So how do i make the code with variable work?
 
Old 11-15-2010, 03:10 PM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
In awk, unless the variable is referring to a field in the input record, there is no "$" needed to reference it. So if you have a variable X in your awk program, then you could say:
Code:
awk '$1 == X {print $2}'
However, if you put the "X" into double quotes, it becomes a string:
Code:
awk '$1 == "X" {print $2}'
So we're comparing $1 to the string "X".

Now, let's say that X has a value of 2, set earlier in the program:
Code:
awk '$1 == $X {print $3}'
Now, we're comparing fields one and two ($1 and $2) for equality; if they are equal, print the third field..
 
Old 11-15-2010, 03:20 PM   #3
ghantauke
Member
 
Registered: Nov 2010
Posts: 114

Original Poster
Rep: Reputation: 6
Quote:
Originally Posted by GrapefruiTgirl View Post
In awk, unless the variable is referring to a field in the input record, there is no "$" needed to reference it. So if you have a variable X in your awk program, then you could say:
Code:
awk '$1 == X {print $2}'
However, if you put the "X" into double quotes, it becomes a string:
Code:
awk '$1 == "X" {print $2}'
So we're comparing $1 to the string "X".

Now, let's say that X has a value of 2, set earlier in the program:
Code:
awk '$1 == $X {print $3}'
Now, we're comparing fields one and two ($1 and $2) for equality; if they are equal, print the third field..
Sorry my question was unclear. I set the value of variable x=of before entering awk. I'm using bash.

bash-4.1$ x=of
bash-4.1$ awk '$1 == $'x' {print $2}' file.txt

Assume the file contains the following data.

of yes
am yes
do yes


I want awk to print out yes (which is the 2nd column) if the first column is of.
 
Old 11-15-2010, 03:50 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Awk doesn't know about shell variables.

You need to do THIS:
Code:
x=of
awk -v x=$x '$1==x {print $2}' file.txt


Cheers,
Tink

Last edited by Tinkster; 11-15-2010 at 03:51 PM.
 
Old 11-15-2010, 03:58 PM   #5
ghantauke
Member
 
Registered: Nov 2010
Posts: 114

Original Poster
Rep: Reputation: 6
Quote:
Originally Posted by Tinkster View Post
Awk doesn't know about shell variables.

You need to do THIS:
Code:
x=of
awk -v x=$x '$1==x {print $2}' file.txt


Cheers,
Tink
Just tried it your way. Unfortunately it didn't work. Also awk does recognise the variable from shell if you use $'variablename'.
I'm guessing the problem lies in the comparasion of strings. In java when we try and compare two variables containing the same string we have to use the "equals" operator. It doesn't work if you use == operator. Correct me if I'm wrong.
 
Old 11-15-2010, 04:12 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by ghantauke View Post
Just tried it your way. Unfortunately it didn't work. Also awk does recognise the variable from shell if you use $'variablename'.
No version of awk I've ever encountered supports the
notation you're suggesting. Which version of awk are
you using?

And my way (using bash & awk) works just fine:
Code:
$ awk --version
GNU Awk 3.1.8
$ bash --version
GNU bash, version 4.1.7(2)
$ cat gauth 
of yes1
am yes2
do yes3
$ x=of
$ awk -v x=$x '$1==x {print $2}' gauth
yes1

Quote:
Originally Posted by ghantauke View Post
I'm guessing the problem lies in the comparasion of strings. In java when we try and compare two variables containing the same string we have to use the "equals" operator. It doesn't work if you use == operator. Correct me if I'm wrong.
I think you're wrong.


Cheers,
Tink
 
Old 11-15-2010, 04:33 PM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
The method suggested by Tinkster works. Another one is by using quotes properly (more difficult and less readable anyway), for example the following should work:
Code:
awk '$1 == "'$x'" {print $2}' file.txt
where the awk program is made of three concatenated strings, the second being the value of the expanded shell variable x. Please, take a look at http://www.gnu.org/manual/gawk/gawk....hell-Variables for details.
 
Old 11-15-2010, 04:54 PM   #8
ghantauke
Member
 
Registered: Nov 2010
Posts: 114

Original Poster
Rep: Reputation: 6
Quote:
Originally Posted by colucix View Post
The method suggested by Tinkster works. Another one is by using quotes properly (more difficult and less readable anyway), for example the following should work:
Code:
awk '$1 == "'$x'" {print $2}' file.txt
where the awk program is made of three concatenated strings, the second being the value of the expanded shell variable x. Please, take a look at http://www.gnu.org/manual/gawk/gawk....hell-Variables for details.
Yes I messed up the syntax of the '$x' thingy. No wonder it wasn't working. Thanks a bunch. Also the method suggested by tinkster works too. I messed up his syntax while testing (yet again). Thanks a lot to both of u.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
awk question on handling *.CSV "text fields" in awk jschiwal Programming 8 05-27-2010 07:23 AM
[SOLVED] awk question vikas027 Programming 2 06-05-2008 04:18 AM
an awk question zoshr Programming 5 04-13-2007 06:29 AM
awk question puishor Programming 2 08-26-2005 10:44 AM
awk question denalitastic Linux - Newbie 1 06-07-2005 11:42 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12: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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration