Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Awk command on terminal

Hello,

I am new to the mac terminal. I am trying to make an awk command that searches through a series of .txt files for a specific word "Deet" . Since the files contain this word multiple times, I want the command to find the last "Deet" word and print the file name along with the word. How can I write this code? please Help?

MacBook Air (11-inch, Early 2014), iOS 9.1

Posted on Dec 1, 2015 12:30 PM

Reply
Question marked as Best reply

Posted on Dec 1, 2015 9:36 PM

Ordinarily, I stick with what is installed with OS X. This awk example uses GNU awk (gawk), and I have installed homebrew (brew) package manager on my OS X El Capitan, and then used brew install gawk to put it into /usr/local/bin.


Although awk on OS X accepts the ENDFILE syntax without error, it does nothing when the end of an individual file is reached. It does work in gawk though.


Here is the awk script that you run in the Terminal, and provide *.txt files on the command line. It will process every line in these files looking for Deet, and when it reaches the end of the given file (ENDFILE), it will print out the last captured DEET, and the current filename. I called the following script deet.awk, and you run it as:


./deet.awk *.txt


#!/usr/local/bin/gawk -f

#

# capture and print the last match for word in its file



function usage()

{

printf "Usage: %s *.txt\n", ENVIRON["_"]

}



BEGIN {

if (ARGC < 2) {

usage()

exit 1

}

}

/Deet/ { match($0, /(Deet)/, z);a = z[1];b=FILENAME }

ENDFILE { printf "%s\t%s\n", a, b }

END { exit }


Output:

odin: ~/awkism$ ./deet.awk *.txt

Deet test10.txt

Deet test25.txt

Deet test30.txt

Deet test45.txt

8 replies
Question marked as Best reply

Dec 1, 2015 9:36 PM in response to benyben23

Ordinarily, I stick with what is installed with OS X. This awk example uses GNU awk (gawk), and I have installed homebrew (brew) package manager on my OS X El Capitan, and then used brew install gawk to put it into /usr/local/bin.


Although awk on OS X accepts the ENDFILE syntax without error, it does nothing when the end of an individual file is reached. It does work in gawk though.


Here is the awk script that you run in the Terminal, and provide *.txt files on the command line. It will process every line in these files looking for Deet, and when it reaches the end of the given file (ENDFILE), it will print out the last captured DEET, and the current filename. I called the following script deet.awk, and you run it as:


./deet.awk *.txt


#!/usr/local/bin/gawk -f

#

# capture and print the last match for word in its file



function usage()

{

printf "Usage: %s *.txt\n", ENVIRON["_"]

}



BEGIN {

if (ARGC < 2) {

usage()

exit 1

}

}

/Deet/ { match($0, /(Deet)/, z);a = z[1];b=FILENAME }

ENDFILE { printf "%s\t%s\n", a, b }

END { exit }


Output:

odin: ~/awkism$ ./deet.awk *.txt

Deet test10.txt

Deet test25.txt

Deet test30.txt

Deet test45.txt

Dec 1, 2015 11:41 PM in response to benyben23

Hello


You may try something like this:



#!/bin/bash # print filename and query text awk -v q=Deet ' $0 ~ q { a[FILENAME] = $0 } END { for (f in a) { printf "%s\t%s\n", f, q } } ' *.txt




or this:



#!/bin/bash # print filename and the last matching line in the file awk -v q=Deet ' $0 ~ q { a[FILENAME] = $0 } END { for (f in a) { printf "%s\t%s\n", f, a[f] } } ' *.txt




Regards,

H

Dec 2, 2015 12:04 AM in response to Hiroto

PS. If you wish, you may create a file named find_last.awk with the following contents



#!/usr/bin/awk -f # # file: # find_last.awk # usage: # ./find_last.awk -v q=Deet *.txt # ./find_last.awk q=Deet *.txt # $0 ~ q { a[FILENAME] = $0 } END { for (f in a) { printf "%s\t%s\n", f, a[f] } }




and use it like this:



#!/bin/bash ./find_last.awk -v q=Deet *.txt




or even this:



#!/bin/bash ./find_last.awk q=Deet *.txt




Good luck,

H


PPS. Changed command name from find.awk to find_last.awk.

Dec 2, 2015 11:54 AM in response to VikingOSX

For some odd reason, -Ho didn't work when I tested it (thought -o was overriding -H), but now it's working (previous typo on my part, I guess)

(I think you need to remove the () in your example)


Lots more options for the OP in man grep (i.e. -i for case insensitive searches)

grep seems the better solution for this problem.

Dec 2, 2015 12:31 PM in response to Tony T1

I keep forgetting that in my .bashrc file, I have the following set, so I don't have to explicitly use grep -E, or egrep for extended regular expressions:

export GREP_OPTIONS='--color=auto --extended-regexp'


Still, even when I unset GREP_OPTIONS, and follow that with your syntax, whole lines are printed. One can omit the capture parentheses, keep the -Ho arguments, and the last "Deet" in the line is shown.

Dec 2, 2015 1:09 PM in response to VikingOSX

In .bash_profiile I use: alias grep='grep —color=auto —line-number —ignore-case'

instead of export GREP_OPTIONS='—color=auto —line-number —ignore-case'

…so when I want to run without options. I just escape grep: \grep


Not sure why OP was searching for the last occurrence of Deet.

Without context or line number, it's the same output as the 1st occurrence of Deet

Awk command on terminal

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.