2

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; 
1
  • Combine bash and awk? It's called Perl :P Seriously now, I am not too sure what you're asking... What are you trying to do? Commented Dec 18, 2011 at 12:45

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

5 Comments

. only means for current directory, you can run any script/program using absolute path
Don't confuse the command . with the directory .; neither actually means current directory! You'll find the command . accepts any path, and the directory . exists in any directory; but pathnames that don't start with a / will start from the current directory, so . actually refers to the current directory from within itself.
Dot . always means current directory, if you run ls -la: you will receive: . for current directory, and .. for the up directory. This is simple observation.
No, . means the containing directory. Try ls -di . $PWD .. ../. /. / and compare the inode numbers. There's a . everywhere.
As an aside, MS-DOS doesn't actually have Unix-style . or .. but emulates them; it also has the notion of a current working directory per device. Things look quite a bit different then.
2

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.

Comments

1

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.

Comments

1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.