LinuxQuestions.org
Help answer threads with 0 replies.
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-31-2013, 07:14 PM   #1
readmore
LQ Newbie
 
Registered: Feb 2013
Location: on the park bench.
Distribution: Debian
Posts: 15

Rep: Reputation: Disabled
Cannot pass bash variable to awk (in order to cut out text)


Hi, I am trying to pass a variable to awk so awk can then cut out text to the left. Attempting this in a bash script.

Here's the script....

Code:
#!/bin/bash

var1=five

# this works ... static config 
echo "--manual"
grep "line2" file.txt | awk -F'five' '{print $2}'
echo "end manual"

echo

# this fails ... trying to pass variable from shell to awk
echo "--pass shell var to awk"
grep "line2" file.txt | awk -v awkvar=$var1 -F'awkvar' '{print $2}'
echo "end pass var"
This is file.txt

Code:
[win@lotto ~]$ cat file.txt 
line1     one     two     three
line2     four    five    six
line3     seven   eight   nine
This is my result....

Code:
win@lotto ~]$ ./script.sh 
--manual
    six
end manual

--pass shell var to awk

end pass var
[win@lotto ~]$
It works if I manually pass a value. But not if I try to pass a variable from the script.

I've googled'd a whole lot & tried different syntax. No luck. Hope someone can help.
 
Old 12-31-2013, 10:27 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
The answer to your question is you are doing it wrong. In your manual method you are assigning a value to FS (using -F) but then in second process you create an awk variable using -v and
assign the string 'awkvar' as FS.

The reason this did not work in the way you think is because awk variables are only visible to awk and before entering the single quotes of the awk script you are not in awk (hope that made sense)

So to do what you wanted:
Code:
grep "line2" file.txt | awk -F"$var1" '{print $2}'
That being said, this would seem to be the long way to do this.
Try:
Code:
awk -vawkvar="$var1" '$0 ~ awkvar && /line2/{print $NF}' file.txt
 
1 members found this post helpful.
Old 01-01-2014, 03:35 AM   #3
readmore
LQ Newbie
 
Registered: Feb 2013
Location: on the park bench.
Distribution: Debian
Posts: 15

Original Poster
Rep: Reputation: Disabled
[SOLVED] Cannot pass bash variable to awk (in order to cut out text)

Thanks for the info. I ended up going a different direction but your reply got me thinking.

After doing some grep & sed stuff to end up with one line, I was able to just do an awk for a random column. No idea why I was trying that other stuff (trying to cut left/right portions etc...

Anyway...I did this:
Code:
echo $passtoawk1 | awk -vawkvar1="$randomcol1" '{print $awkvar1}'
Thanks again.
 
Old 01-01-2014, 04:45 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Glad it worked

Please use the Thread Tools to mark as SOLVED
 
  


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
[SOLVED] Bash command to 'cut' text into another text file & modifying text. velgasius Programming 4 10-17-2011 04:55 AM
Pass a shell variable to an AWK command chogall Programming 1 12-23-2010 10:12 AM
how to pass an external variable to awk??? akitty Programming 7 11-13-2008 08:47 AM
Pass a variable as an agruement for cut chipmanchu Programming 1 05-25-2005 11:39 AM

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

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