Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedding awk in a shell script

Tags:

bash

awk

I've been using a bash script (script.sh) which uses various awk scripts (script1.awk,script2.awk) which are tailored at "runtime", by replacing values for instance. I've been looking for ways to embed them completely within the first bash script. Ideally, I would like to have a file looking like this :

################################
# AWK scripts                  #
################################
read -d '' scriptVariable <<'EOF'
'
{my block commands;}
'
EOF
################################
# End of AWK Scripts           #
################################
awk $scriptVariable ${inputfile} # This line obviously doesn't work

instead of the traditional :

awk '{
my script commands
' ${intputfile}

Of course, I could write them to a file but the whole point is not to. Any suggestions ?

EDIT : Although dogbane answers works fine, the next problem is that with the <<'HERE' tags, newline characters are not read.I can't unquote it since, otherwise, he's trying to interpret the awk script then encountering a $ sign within (and there are). And with no newlines, I can't comment anything within the awk script (without commenting half the script when the newlines characters are being removed ...). Anyone ?

<< 'EOF'
BEGIN{#Hello
print $1
}
EOF # Is read as BEGIN{#Hello print $1} by awk; ie BEGIN{

<< EOF
BEGIN{#Hello
print $1
}
EOF #Is read correctly by awk but Bash tried to express $1 and fails
like image 967
Bertrand Caron Avatar asked Feb 22 '13 08:02

Bertrand Caron


People also ask

Can we use awk in shell script?

Awk is an excellent tool for building UNIX/Linux shell scripts. AWK is a programming language that is designed for processing text-based data, either in files or data streams, or using shell pipes. In other words you can combine awk with shell scripts or directly use at a shell prompt.

How do you use awk shell?

awk ScriptsTell the shell which executable to use to run the script. Prepare awk to use the FS field separator variable to read input text with fields separated by colons ( : ). Use the OFS output field separator to tell awk to use colons ( : ) to separate fields in the output. Set a counter to 0 (zero).

How do you call an awk script?

In order to tell awk to use that file for its program, you type: awk -f source-file input-file1 input-file2 … The -f instructs the awk utility to get the awk program from the file source-file (see Command-Line Options). Any filename can be used for source-file .

Can we use awk in C?

You can add input files to be processed by the AWK program. In this case, we add argv[1] , the first argument that was received by the C program. The AWK program gets executed. By default, the output is sent to stdout .


1 Answers

Remove the single quotes around the awk script and enclose the script variable in double-quotes. This works for me:

################################
# AWK scripts                  #
################################
read -d '' scriptVariable << 'EOF'
BEGIN {
    print "start"
}
{
    print $0
}
END{
    print "hello"
}
EOF
################################
# End of AWK Scripts           #
################################

awk "$scriptVariable" ${inputfile}
like image 125
dogbane Avatar answered Sep 23 '22 02:09

dogbane