Domanda

so there is my problem, it is a very trivial one I assume but I can't for the life of me find a solution.

I want to learn to use awk to manipulate text files. I know python and can use it to this effect but I've been told by my supervisor that i was to use Awk.

I am experimenting on cygwin at home but in the end I will work with UNIX

My probelm is with basic Awk syntax. I want to write my awk script in a text file and call it from the command line in the following way.

./scirptname filename

when I'm using a very simple Helloworld script there is no problem.
Now I want to try and search for a line containing the a keyword.
My script looks like this

#! /bin/awk

BEGIN {

}
{
    '/keyword/ {print}'
}

when I attempt to run it on a file know contains this keyword I get the following error message

gawk: cmd. line:1: ./test1
gawk: cmd. line:1: ^ syntax error
gawk: cmd. line:1: ./test1
gawk: cmd. line:1:   ^ unterminated regexp

So once again, sorry to bother you with this very simple and trivial question, but how do I feed a file name to my script and have my script perform the task I want it to perform on that file.
I don't want to make a one liner from shell, what I would like to do on those files is a bit too complex for that (in my ignorant uneducated opinion).
I would really really apreciate the help.

È stato utile?

Soluzione

Problem one:

You didn't understand awk's pattern and actions well. google some tutorial and read that part.

like : http://www.chemie.fu-berlin.de/chemnet/use/info/gawk/gawk_9.html

in your awk file, you should either have:

#! /bin/awk -f
BEGIN {} {if(/keyword/) {print}}

or

#! /bin/awk -f
BEGIN {} /keyword/ {print}

in fact, the above line could be shorten into (assume you need the BEGIN block):

#!/bin/awk -f
BEGIN {} /keyword/

Problem two:

the hashbang line need -f so :

#!/bin/awk -f

then you could under same dir, do:

./myawk.awk inputfile
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top