Does it possible combine bash and awk script files?

I have some bash script where I get values of variable, that I would like use in awk. Does it possible include whole awk (like it possible with bash script files) file in bash e.g.:

#!/bin/sh
 var1=$1
source myawk.sh

and myawk.sh:

print $1;

Answers


first of all, if you're writing bash don't use #!/bin/sh that will put you in compatibility mode which is only necessarly if you're writing for portability (and then you have to adhere to the POSIX normative).

now regarding your question you just have to run awk from inside your bash script, like this:

#!/bin/bash
var1=$1
awk -f myawk.sh

also you should use .awk as extension I guess.


Bash and awk are different languages, each with their own interpreter of the same name. The tiny sample you show is stripped down too far to make much sense:

  1. You've marked both files as shell scripts; one using the shebang #!/bin/sh and the other using the extension .sh. Obviously the shell can read shell script, and the command to do so is called . in Bourne shell (or source in csh and bash).
  2. The shell script assigns a variable, but you're not using it anywhere. Did you mean passing it on to the awk script?
  3. Both the awk and shell script use $1, which has different meanings for them (in bash, it's from the command line or a set command; in awk, it's from a parsed input line).

The two tools are often used in tandem, as the shell is better at combining separate programs and awk is better at reformatting tabular or structured text. It was so common that a whole language evolved to combine the tasks; Perl's roots are as a combination of shell, awk and sed.

If you just wanted to pass a variable from the shell script into an awk script, use -v. The man page is your friend.


Or, many ppl do sth like this:

#!/bin/env bash
#Bash things start
 ...
var1=$1
#Bash things stop
#Awk things start, 
#may use pipes or variable to interact with bash 
awk -v V1=var1 '
#AWK program, can even include awk scripts here.
'
#Bash things

I suggest this page here by Bruce Barnett:

http://www.grymoire.com/Unix/Awk.html#uh-3

You can also use double quote to make use of shell's extract feature but it is confusing.

Personally I just try to avoid those fancy gnu additions of bash or awk and make my scripts ksh+(n)awk compatible.


As an hardcore AWK user, I soon realized that doing the following was really a huge help :

  1. Defining and exporting an AWK_REPO variable in my bashrc #Content of bashrc export AWK_REPO=~/bin/AWK
  2. Storing there every AWK script I write using the .awk extension.
  3. You can then call it from anywhere like this : awk -f $AWK_REPO/myScript.awk $file or even, using Shebangs and adding AWK_REPO to PATH (with export PATH=${AWK_REPO}:${PATH}) myScript.awk $file

Need Your Help

Using $.ajax to fetch static html files in phonegap and Windows Phone 7

javascript windows-phone-7 cordova requirejs

We're using Phonegap/Cordova 2.3.0 to deploy an app to Windows Phone 7. The application uses Require.js to dynamically load all modules. One module is called 'router' and is responsible for fetching

How to mock request and response in nodejs to test middleware/controllers?

node.js testing express integration-testing mocha

My application has several layers: middleware, controllers, managers. Controllers interface is identical to middlewares one: (req, res, next).

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.