Solved

Passing command line arguments to awk

Posted on 1998-06-23
5
5,844 Views
Last Modified: 2013-12-26
How can I pass a command line argument such as $1 or $2 into an awk script?

I have written a shell script that includes some awk programming and i need to be able to call that program by typing ./program inputword and then search for inputword within awk.

For example, if I had a datafile with the following contents:

a:b:c:d:e:f:g:
g:f:e:d:c:b:a:

and i wanted to write a script to search through that file for a name that I give at the command line

awk '
$4 ~ /b/ {print $4} ' datafile

i would like to do it like this

awk '
$4 ~ /input from command line such as $1/ {print $4}' datafile

I have tried this:

awk '
$4 ~ /inputword/ {print $4}' inputword=$1 datafile

without any luck

I have to be able to run the program without having to type something like

awk -f scriptfile datafile

as it gets to confusing.

Can anyone help?


0
Comment
Question by:ianfoster
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 51

Accepted Solution

by:
ahoffmann earned 100 total points
ID: 1294239
2 possibilities:

1. use gawk -v varname=inputword -f awkscript datafile
   (use  varname in you awk script)

2. using traditional awk is somehow triky, you must pass in you
   strings via the data stream and then identify it uniqely
   example:

   /^SPECIAL_VALUE/ { inputword = $2; next; }
   {   # rest of script }

   call this script as follows:

   (echo SPECIAL_VALUE "your_value"; cat datafile)|awk -f script

   Keep in mind that SPECIAL_VALUE *must not* appear in datafile.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 1294240
(echo SPECIAL_VALUE inputword; cat datafile) | awk '/^SPECIAL_VALUE/ { pattern=$2;} $4 ~ pattern {print $4}'
0
 
LVL 84

Expert Comment

by:ozo
ID: 1294241
awk "\$4 ~ /$1/ {print \$4}"  datafile
0
 

Author Comment

by:ianfoster
ID: 1294242
I've installed gawk on the machine but still no luck :(


Here's another example:


--------------------

#!/bin/sh

echo "Here we go!"

# the rest of the shell script goes here


gawk -v input=$1 -F: '
$1 ~ /input/ {print $1}' datafile

--------------------


datafile contents as follows

a:b:c:d:
a:b:c:d:
a:b:c:d:

I call the program as follows

/gawkprog a

it should print out "a" for each line but I get nothing.

it does read in the $1 part because I can do this                  

gawk -v input=$1 -F: '
{print input} ' datafile

and run it ./gawkprog a

and it will print out

a
a
a

so the problem seems to be that gawk doesnt like matching things with
the input part.

any ideas?
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 1294243
gawk -v input=$1 -F: '$1 ~ input {print $1}' datafile

BTW, ozo's solution works also inside a shell script
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
maven archtype selection in eclipse 1 59
How to convert MFC::CString to UTF8 wchar_t* 10 449
Annoying "thing" blocks my view 4 145
Message not shown 5 103
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question