shell - 使用 awk 执行 shell 脚本在 AppleScript 中失败,但相同的 awk 命令在终端中有效
问题描述
使 shell 脚本在 AppleScript 中工作。
终端中的以下工作:
awk -F\" '/kMDItemTextContent/{print $2}' /Users/john/Desktop/PDFTags/mdimport.txt
但是同一行, with do shell script
,在脚本编辑器中引发错误。
do shell script "awk -F\" '/kMDItemTextContent/{print $2}' /Users/john/Desktop/PDFTags/mdimport.txt"
这是来自脚本编辑器中的回复:
tell current application do shell script "awk -F\" '/kMDItemTextContent/{print $2}' /Users/john/Desktop/PDFTags/mdimport.txt"
--> error "sh: -c: line 0: unexpected EOF while looking for matching `\"' sh: -c: line 1: syntax error: unexpected end of file" number 2
作为参考,这里是 mdimport.txt 的上下文
Imported '/Users/John/Desktop/PDFTags/Untitled.txt' of type 'public.plain-text' with plugIn /System/Library/Spotlight/RichText.mdimporter. 32 attributes returned {
":EA:_kMDItemUserTags" = (
);
":EA:kMDItemLastUsedDate" = "2020-11-07 08:17:13 +0000";
":MD:DeviceId" = 16777227;
":MD:kMDItemPath" = "/Users/john/Desktop/PDFTags/Untitled.txt";
"_kMDItemContentChangeDate" = "2020-11-05 23:24:57 +0000";
"_kMDItemCreationDate" = "2020-11-05 22:34:36 +0000";
"_kMDItemCreatorCode" = 0;
"_kMDItemDisplayNameWithExtensions" = {
"" = "Untitled.txt";
};
"_kMDItemFileName" = "Untitled.txt";
"_kMDItemFinderFlags" = 16;
"_kMDItemFinderLabel" = 0;
"_kMDItemFromImporter" = 1;
"_kMDItemIsExtensionHidden" = 1;
"_kMDItemIsFromImporter" = 1;
"_kMDItemLocked" = 1;
"_kMDItemOwnerGroupID" = 20;
"_kMDItemOwnerUserID" = 501;
"_kMDItemTextEncodingHint" = 134217984;
"_kMDItemTypeCode" = 0;
"com_apple_metadata_modtime" = "626311497.290109";
kMDItemAlternateNames = (
"Untitled.txt"
);
kMDItemContentCreationDate = "2020-11-05 22:34:36 +0000";
kMDItemContentModificationDate = "2020-11-05 23:24:57 +0000";
kMDItemContentType = "public.plain-text";
kMDItemContentTypeTree = (
"public.plain-text",
"public.text",
"public.data",
"public.item",
"public.content"
);
kMDItemDateAdded = "2020-11-07 08:56:45 +0000";
kMDItemDisplayName = {
"" = Untitled;
};
kMDItemDocumentIdentifier = 27837;
kMDItemKind = {
"" = NSStringPboardType;
ar = "\U0645\U0633\U062a\U0646\U062f \U0646\U0635\U064a \U0639\U0627\U062f\U064a";
ca = "Document de text sense format";
cs = "Prost\U00fd textov\U00fd dokument";
da = "Alm. tekstdokument";
de = "Reines Textdokument";
el = "\U0388\U03b3\U03b3\U03c1\U03b1\U03c6\U03bf \U03b1\U03c0\U03bb\U03bf\U03cd \U03ba\U03b5\U03b9\U03bc\U03ad\U03bd\U03bf\U03c5";
en = "Plain Text Document";
"en_AU" = "Plain Text Document";
"en_GB" = "Plain Text Document";
es = "Documento de texto sin formato";
"es_419" = "Documento de texto sin formato";
fi = "Pelkk\U00e4 teksti -dokumentti";
fr = "Document format texte";
"fr_CA" = "Document format texte";
he = "\U05de\U05e1\U05de\U05da \U05de\U05dc\U05dc \U05e4\U05e9\U05d5\U05d8";
hi = "\U092a\U094d\U0932\U0947\U0928 \U091f\U0947\U0915\U094d\U0938\U094d\U091f \U0926\U0938\U094d\U0924\U093e\U0935\U0947\U091c\U093c";
hr = "Dokument obi\U010dnog teksta";
hu = "Sima sz\U00f6veges dokumentum";
id = "Dokumen Teks Biasa";
it = "Documento di solo testo";
ja = "\U6a19\U6e96\U30c6\U30ad\U30b9\U30c8\U66f8\U985e";
ko = "\Uc77c\Ubc18 \Ud14d\Uc2a4\Ud2b8 \Ubb38\Uc11c";
ms = "Dokumen Teks Biasa";
nl = "Platte-tekstdocument";
no = "Dokument med ren tekst";
pl = "dokument tekstowy (zwyk\U0142y)";
pt = "Documento de Texto Simples";
"pt_PT" = "Documento de texto simples";
ro = "Document text simplu";
ru = "\U0414\U043e\U043a\U0443\U043c\U0435\U043d\U0442 \U043f\U0440\U043e\U0441\U0442\U043e\U0433\U043e \U0442\U0435\U043a\U0441\U0442\U0430";
sk = "Dokument s\U00a0oby\U010dajn\U00fdm textom";
sv = "Rent textdokument";
th = "\U0e40\U0e2d\U0e01\U0e2a\U0e32\U0e23\U0e02\U0e49\U0e2d\U0e04\U0e27\U0e32\U0e21\U0e18\U0e23\U0e23\U0e21\U0e14\U0e32";
tr = "D\U00fcz Metin Belgesi";
uk = "\U0414\U043e\U043a\U0443\U043c\U0435\U043d\U0442 \U043f\U0440\U043e\U0441\U0442\U043e\U0433\U043e \U0442\U0435\U043a\U0441\U0442\U0443";
vi = "T\U00e0i li\U1ec7u v\U0103n b\U1ea3n thu\U1ea7n t\U00fay";
"zh_CN" = "\U7eaf\U6587\U672c\U6587\U7a3f";
"zh_HK" = "\U7d14\U6587\U5b57\U6587\U4ef6";
"zh_TW" = "\U7d14\U6587\U5b57\U6587\U4ef6";
};
kMDItemLogicalSize = 345;
kMDItemPhysicalSize = 4096;
kMDItemTextContent = "Here are the text file contents that was used to test the routine.\n\nHash Tag Test Document\n\n#HashTag1 this is the first hash tag.\n#HashTag2 this is the second hash tag.\n\nThe following hash tag is inside and at the end of a paragraph:
#HashTag3\n\nThe next hash tag #HashTag4 is in the middle of a paragraph.\n\n#HashTag5\n#asdfasdfasdfasdfasfdasdfasd"; }
解决方案
查看错误消息的一部分:
寻找匹配的 `\"' 时出现意外的 EOF
它指的是由以下分配的字段分隔符:-F
在这种情况下,您需要单引号和转义单个双引号。
do shell script "awk -F'\"' '/kMDItemTextContent/{print $2}' /Users/john/Desktop/PDFTags/mdimport.txt"
结果:
"这里是用于测试例程的文本文件内容。\n\n哈希标记测试文档\n\n#HashTag1 这是第一个哈希标记。\n#HashTag2 这是第二个哈希标记。\n\nThe以下井号标签在段落的内部和结尾:"
推荐阅读
- sas - 如何用数据集中的SAS中的另一行值替换值
- java - 点燃扫描查询问题
- c# - 避免在 API 输出 XML 文档中出现两次循环
- javascript - 如何使用 react 或 javascript 根据其内容向 span 标签添加不同的样式?
- c# - 等待异步任务结束
- javascript - 刷新后执行 PHP 脚本
- angular - 将 Typescript 中的日期字段映射到后端 C# 中的日期时间字段
- c - 如何将输入从文件和命令行参数重定向到我的 C 程序到图形节点?
- python - 在 Gurobi 中使用 addVars 和 addConstrs
- php - Laravel AbstractVariables.php 第 54 行预期名称为字符串