2

If I am visiting a file in emacs I want to be able to run a simple awk command, eg

awk /Sam/ { print $0 }.

How would I construct this.

I tried:

m-! - to be able to run a shell cmd

awk '/Sam/ { print $0 }' test.txt

But then I get this error:

awk: c:/MinGW/msys/1.0/Sam/ { print }
awk:  ^ syntax error
awk: c:/MinGW/msys/1.0/Sam/ { print }
awk:                        ^ syntax error
errcount: 2

Running just awk '{ print }' test.txt works

I am running on Windows 7 using minGW and my shell is set to:

echo %SHELL%
C:/MinGW/msys/1.0/bin/bash.exe

How do I escape the /Sam/ ? It thinks it is part of the directory to the exe I think? I tried (trying gawk to see if get better diagnotic msg):

gawk '\/Sam\/ { print $0 }' test.txt

gawk: cmd. line:1: \/Sam\/ { print }
gawk: cmd. line:1: ^ backslash not last character on line 

test.txt file contains:

Angus
Lisa
Samuel
Annabel
Jack
Wookie
4

2 回答 2

2

The problem was related to my environment. I am running on Windows and using MSYS with fairly old, some from 1998, versions of awk, etc.

For anyone else who has a similar problem you might consider removing use of msys and using the gnu utilities from this link:

http://sourceforge.net/projects/ezwinports/

于 2013-09-18T14:15:21.327 回答
1

This is how to escape /Sam/, you need a \ before the /. PS you do not need {print $0}, its the default action in awk.

echo "/Sam/" | awk '/\/Sam\//'
/Sam/
于 2013-09-17T05:28:06.417 回答