所以我在AppleScript中创建一个需要写入CSV列的脚本。我已经从CSV中读过,我有关于如何写入CSV的代码,但它根本不写。我想写第22列。目前,我可以从该列读取,但我不确定如何编辑列。或者有没有办法在像Excel这样的excel中编辑某个框来说“是”。我在下面附上了我的代码。
set theFile to (choose file)
set f to read theFile
-- break the file into paragraphs (c.f. rows)
repeat with row in (paragraphs of f)
-- parse the row into comma-delimited fields
set fields to parseCSV(row as text)
-- now you have your data:
set verifyAccountStatus to item 22 of fields
set theResult to writeTo(verifyAccountStatus, "Yes", text, true)
end repeat
on parseCSV(theText)
set {od, my text item delimiters} to {my text item delimiters, ","}
set parsedText to text items of theText
set my text item delimiters to od
return parsedText
end parseCSV
on writeTo(targetFile, theData, dataType, apendData)
-- targetFile is the path to the file you want to write
-- theData is the data you want in the file.
-- dataType is the data type of theData and it can be text, list, record etc.
-- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
try
set openFile to open for access file targetFile with write permission
if apendData is false then set eof of openFile to 0
write theData to openFile starting at eof as dataType
close access openFile
return true
on error
try
close access file targetFile
end try
return false
end try
end writeTo
编辑: 出于某种原因,当我运行它时它不起作用。
set theFile to (choose file)
set openFile to open for access theFile with write permission
do shell script "awk -F, '$1 ~ /Line 7/{$22=\"hello\"} 1' openFile"
close access openFile
它说它无法打开文件openFile
答案 0 :(得分:2)
也许考虑使用内置于OS X的awk
来做这件事。假设您的文件名为file.csv
,并且以逗号分隔,如下所示:
Line 0,field2,field3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f22,f23,f24,16
Line 1,field2,field3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f22,f23,f24,44
Line 2,field2,field3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f22,f23,f24,93
Line 3,field2,field3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f22,f23,f24,96
Line 4,field2,field3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f22,f23,f24,19
Line 5,field2,field3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f22,f23,f24,77
Line 6,field2,field3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f22,f23,f24,30
Line 7,field2,field3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f22,f23,f24,30
Line 8,field2,field3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f22,f23,f24,1
Line 9,field2,field3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f22,f23,f24,96
如果在终端中运行此awk
脚本,它会将所有field22
条目更改为“Hello”
awk -F, '{$22="hello"}1' OFS=, file.csv
并告诉你:
Line 0,field2,field3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,hello,f23,f24,16
Line 1,field2,field3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,hello,f23,f24,44
Line 2,field2,field3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,hello,f23,f24,93
Line 3,field2,field3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,hello,f23,f24,96
Line 4,field2,field3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,hello,f23,f24,19
Line 5,field2,field3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,hello,f23,f24,77
Line 6,field2,field3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,hello,f23,f24,30
Line 7,field2,field3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,hello,f23,f24,30
Line 8,field2,field3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,hello,f23,f24,1
Line 9,field2,field3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,hello,f23,f24,96
如果您想在新文件中使用它,只需重定向输出,如下所示:
awk -F, '{$22="hello"}1' OFS=, file.csv > newfile.csv
您可以使用以下方式从Applescript中调用此方法:
do shell script "awk -F, '{$22=\"hello\"} 1' file.csv > newFile.csv"
如果您希望用户从对话框窗口中选择文件,那么您的文件名将位于Applescript变量中 - 在您的示例中,它名为theFile
。然后,您需要将此转换为shell和awk
理解的文件名,并引用它并将其传递给shell,如下所示:
do shell script "awk -F, '{$22=\"hello\"} 1' & quoted form of POSIX path of theFile & " > /tmp/a"
如果您只想在包含“77”的行上更改字段22,请执行以下操作:
awk -F, '/77/ {$22="hello"} 1' file.csv
Line 0,field2,field3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f22,f23,f24,16
Line 1,field2,field3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f22,f23,f24,44
Line 2,field2,field3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f22,f23,f24,93
Line 3,field2,field3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f22,f23,f24,96
Line 4,field2,field3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f22,f23,f24,19
Line 5 field2 field3 f4 f5 f6 f7 f8 f9 f10 f11 f12 f13 f14 f15 f16 f17 f18 f19 f20 f21 hello f23 f24 77
Line 6,field2,field3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f22,f23,f24,30
Line 7,field2,field3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f22,f23,f24,30
Line 8,field2,field3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f22,f23,f24,1
Line 9,field2,field3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f22,f23,f24,96
如果您只想在字段1与“第7行”匹配的行上更改field22,则可以执行以下操作:
awk -F, '$1 ~ /Line 7/ {$22="hello"} 1' file.csv
Line 0,field2,field3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f22,f23,f24,16
Line 1,field2,field3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f22,f23,f24,44
Line 2,field2,field3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f22,f23,f24,93
Line 3,field2,field3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f22,f23,f24,96
Line 4,field2,field3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f22,f23,f24,19
Line 5,field2,field3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f22,f23,f24,77
Line 6,field2,field3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f22,f23,f24,30
Line 7 field2 field3 f4 f5 f6 f7 f8 f9 f10 f11 f12 f13 f14 f15 f16 f17 f18 f19 f20 f21 hello f23 f24 30
Line 8,field2,field3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f22,f23,f24,1
Line 9,field2,field3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f22,f23,f24,96
注意:强>
-F,
只是告诉awk
输入字段用逗号分隔。 OFS=,
告诉awk
用逗号分隔输出字段。