简体   繁体   English

在col 1中查找字符串,在awk中打​​印col 2

[英]Find string in col 1, print col 2 in awk

I'm on a Mac, and I want to find a field in a CSV file adjacent to a search string 我在Mac上,想在CSV文件中的搜索字符串旁边找到一个字段

This is going to be a single file with a hard path; 这将是一个具有硬路径的单个文件。 here's a sample of it: 这是一个示例:

84:a5:7e:6c:a6:b0, AP-ATC-151g84
84:a5:7e:6c:a6:b1, AP-A88-131g84
84:a5:7e:73:10:32, AP-AG7-133g56
84:a5:7e:73:10:30, AP-ADC-152g81
84:a5:7e:73:10:31, AP-D78-152e80

so if my search string is "84:a5:7e:73:10:32" I want to get returned "AP-AG7-133g56" 因此,如果我的搜索字符串是"84:a5:7e:73:10:32"我想返回"AP-AG7-133g56"

I had been working within an Applescript, but maybe a shell script will do. 我一直在使用Applescript,但也许可以使用Shell脚本。

I just need the proper syntax for opening the file and having awk search it. 我只需要适当的语法来打开文件并用awk搜索它。 Again, I'm weak conceptually on how shell commands run, how they must be executed, etc 再次,从概念上讲,我在shell命令如何运行,如何执行等方面较弱

This errors, gives me ("command not found"): 这个错误给我(“找不到命令”):

set the_file to "/Users/Paw/Desktop/AP-Decoder 3.app/Contents/Resources/BSSIDtable.csv"
set the_val to "70:56:81:cb:a2:dc"
do shell script "'awk $1 ~ the_val {print $2} the_file'"

Thank you for coddling me... 谢谢你爱我...

This is a relatively simple: 这是一个相对简单的:

awk '$1 == "70:56:81:cb:a2:dc," {print "The answer is "$2}' 'BSSIDtable.csv'

(the "The answer is " text can be omitted if you only wish to see only the data, but this shows you how to get more user-friendly output if desired). (如果您只希望仅查看数据,则可以省略"The answer is "文本,但这将向您展示如何在需要时获得更多用户友好的输出)。

The comma is included since awk uses white space for separators so the comma becomes part of column 1. 由于awk使用空白作为分隔符,因此包含逗号,因此逗号成为第1列的一部分。

If the thing you're looking for is in a shell variable, you can use -v to provide that to awk as an awk variable: 如果您要查找的内容在shell变量中,则可以使用-v将其作为awk变量提供给awk

lookfor="70:56:81:cb:a2:dc,"
awk -v mac=$lookfor '$1 == mac {print "The answer is "$2}' 'BSSIDtable.csv'

As an aside, your AppleScript solution is probably not working because the $1/$2 are being interpreted as shell variable rather than awk variables. 顺便说一句,您的AppleScript解决方案可能无法正常工作,因为$1/$2被解释为shell变量而不是awk变量。 If you insist on using AppleScript, you will have to figure out how to construct a shell command that quotes the awk commands correctly. 如果您坚持使用AppleScript,则必须弄清楚如何构造一个正确引用awk命令的shell命令。

My advice is to just use the shell directly, the number of people proficient in that almost certainly far outnumber those proficient in AppleScript :-) 我的建议是只直接使用的外壳,人在那熟练的数量几乎肯定远远多于那些的AppleScript精通:-)

if sed is available (normaly on mac, event if not tagged in OP) 如果sed可用(在Mac上正常,在OP中未标记的事件)

simple but read all the file 简单但读取所有文件

sed -n 's/84:a5:7e:73:10:32,[[:blank:]]*//p' YourFile

quit after first occurence (so average of 50% faster on huge file) 第一次出现后退出(因此,在大型文件上平均快50%)

sed -n -e '/84:a5:7e:73:10:32,[[:blank:]]*/!b' -e 's///p;q' YourFile

awk AWK

awk '/^84:a5:7e:73:10:32/ {print $2}'
# OR using a variable for batch interaction
awk -v Src='84:a5:7e:73:10:32' '$1 == Src {print $2}'
# OR assuming that case is unknow
awk -v Src='84:a5:7e:73:10:32' 'BEGIN{IGNORECASE=1} $1 == Src {print $2}'

by default it take $0 as compare test if a regex is present, just add the ^ to take first field content 默认情况下,如果存在正则表达式,则将$ 0作为比较测试,只需添加^以获取第一个字段内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM