LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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-14-2006, 02:47 PM   #1
sharapchi
Member
 
Registered: Nov 2004
Location: Turkey
Distribution: slackware
Posts: 119

Rep: Reputation: 15
can awk see bash script arguments ?


For example i want to write a script names "example.sh". I want to pass any arguments to this script. How can i use this argument for example "$1" in awk?

awk "{print $1}" #?

thanks
 
Old 12-14-2006, 03:10 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,378

Rep: Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963
you need to pass it through to awk using the -v option, e.g.
Code:
awk -v awkvarname=$BASHVARIABLE "{print 0.51 (awkvarname)}"
 
Old 12-14-2006, 03:48 PM   #3
sharapchi
Member
 
Registered: Nov 2004
Location: Turkey
Distribution: slackware
Posts: 119

Original Poster
Rep: Reputation: 15
For example we have a sentence in a file like "word1 word2 word3 word4." We can get columbs with 'awk "{print $1 $2}"' command. Now i want to write a bash script that gets specified word from sentence. We suppose that the sentence is in the file named "file".

bash> ./example.sh file 3

this command should write the third word.

#this is example.sh

awk "{print $2}" #?

Last edited by sharapchi; 12-14-2006 at 03:58 PM.
 
Old 12-14-2006, 04:21 PM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,378

Rep: Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963
that example appears to make very little sense, but i still think you're after the kind of thing i'm suggesting... maybe.
 
Old 12-14-2006, 05:31 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
Quote:
Originally Posted by sharapchi
For example we have a sentence in a file like "word1 word2 word3 word4." We can get columbs with 'awk "{print $1 $2}"' command. Now i want to write a bash script that gets specified word from sentence. We suppose that the sentence is in the file named "file".

bash> ./example.sh file 3

this command should write the third word.

#this is example.sh

awk "{print $2}" #?

Code:
#!/bin/bash
awk -v number=$2 '{print $number}' $1
Something like that?


Cheers,
Tink

Last edited by Tinkster; 12-14-2006 at 05:33 PM.
 
Old 12-14-2006, 07:23 PM   #6
sharapchi
Member
 
Registered: Nov 2004
Location: Turkey
Distribution: slackware
Posts: 119

Original Poster
Rep: Reputation: 15
It did not worked.

awk -v awkarg=$2 "{ print $awkarg }" $1

this writes all contents of the file given with $1.
 
Old 12-14-2006, 07:48 PM   #7
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 62
use 'single quotes', not "double quotes". The double quotes allow the shell to expand the value of $awkarg before awk sees it, which gives you the wrong result. awk must see the $awkark as a literal string.
 
Old 12-14-2006, 08:03 PM   #8
sharapchi
Member
 
Registered: Nov 2004
Location: Turkey
Distribution: slackware
Posts: 119

Original Poster
Rep: Reputation: 15
what a mistake.. Thanks for all.
 
  


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
BASH Script: variable values referencing for console arguments sadarax Programming 1 11-14-2005 05:23 PM
Bash script question (grep and awk) hamish Linux - Software 6 04-06-2005 03:14 PM
Awk command-line arguments lowpro2k3 Programming 1 03-28-2005 09:09 PM
cannot export result from awk into a variable in a bash script Emmanuel_uk Linux - Newbie 4 03-07-2005 01:54 AM
bash arguments stuckinhell Programming 6 08-13-2004 05:10 AM


All times are GMT -5. The time now is 07:50 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration