使用Applescript和sed在文本文件中进行替换

使用Applescript和sed在文本文件中进行替换,sed,applescript,Sed,Applescript,这个问题是这个问题的续集 假设我有一个普通的txt文件/Users/myname/Desktop/urlist.txt: title 1 http://a.b/c title 2 http://d.e/f ... 我想(1)将所有的URL(.)转换为HTML代码,(2)添加 到每个空行,以便上述内容将成为: title 1 <a href="http://a.b/c">http://a.b/c</a> &nbsp;<br /> title 2

这个问题是这个问题的续集

假设我有一个普通的txt文件/Users/myname/Desktop/urlist.txt:

title 1
http://a.b/c

title 2
http://d.e/f

...
我想(1)将所有的URL(.)转换为HTML代码,(2)添加


到每个空行,以便上述内容将成为:

title 1
<a href="http://a.b/c">http://a.b/c</a>
&nbsp;<br />
title 2
<a href="http://d.e/f">http://d.e/f</a>
&nbsp;<br />
...
标题1

标题2
...
我来看看下面的Applescript:

set inFile to "/Users/myname/Desktop/URLlist.txt"
set middleFile to "/Users/myname/Desktop/URLlist2.txt"
set outFile to "/Users/myname/Desktop/URLlist3.txt"

do shell script "sed 's/\\(http[^ ]*\\)/<a href=\"\\1\">\\1<\\/a>/g' " & quoted form of inFile & " >" & quoted form of middleFile
do shell script "sed 's/^$/\\&nbsp;<br \\/>/g' " & quoted form of middleFile & " >" & quoted form of outFile
将内嵌设置为“/Users/myname/Desktop/urlist.txt”
将middleFile设置为“/Users/myname/Desktop/URLlist2.txt”
将outFile设置为“/Users/myname/Desktop/URLlist3.txt”
执行shell脚本“sed's/\\(http[^]*\\)/\\1/g'”&引用的填充格式和“>”&引用的中间文件格式
执行shell脚本“sed's/^$/\\/g'”&中间文件的引用形式&“>”&输出文件的引用形式
它是可行的,但它是多余的(而且很愚蠢?)。有人能把它说得更简洁些吗?是否可以只涉及一个文本文件而不是三个文本文件(即/Users/myname/Desktop/urlist.txt中的原始内容被最终结果覆盖)

非常感谢。请尝试:

set inFile to "/Users/myname/Desktop/URLlist.txt"

set myData to (do shell script "sed '
/\\(http[^ ]*\\)/ a\\
&nbsp;<br />
' " & quoted form of inFile & " | sed 's/\\(http[^ ]*\\)/<a href=\"\\1\">\\1<\\/a>/g' ")

do shell script "echo " & quoted form of myData & " > " & quoted form of inFile
将内嵌设置为“/Users/myname/Desktop/urlist.txt”
将myData设置为(执行shell脚本“sed”
/\\(http[^]*\\)/a\\

“"ed-form-of-infle&”| sed's/\\(http[^]*\\)/\\1/g') shell脚本“echo”和myData的引号形式&“>”和infle的引号形式吗
这将允许您稍后在脚本中使用myData变量。如果这不是较大脚本的一部分,而您只是在修改文件,请按照jackjr300的建议使用-i选项。此外,该脚本查找原始模式并将新行附加到其中,而不是简单地查找空行

编辑:

将内嵌设置为“/Users/myname/Desktop/urlist.txt”
将myData设置为(执行shell脚本“sed's/\\(http[^]*\\)/\\1/g;s/^$/\\/g'”&引用的填充格式)
shell脚本“echo”和myData的引号形式&“>”和infle的引号形式吗
使用
-i''
选项就地编辑文件

set inFile to "/Users/myname/Desktop/URLlist.txt"

do shell script "sed -i '' 's:^$:\\&nbsp;<br />:; s:\\(http[^ ]*\\):<a href=\"\\1\">\\1</a>:g' " & quoted form of inFile

我不能很好地理解sed命令(这会让我的大脑受伤),所以这里有一个applescript方法来完成这个任务。希望能有帮助

set f to (path to desktop as text) & "URLlist.txt"

set emptyLine to "&nbsp;<br />"
set htmlLine1 to "<a href=\""
set htmlLine2 to "\">"
set htmlLine3 to "</a>"

-- read the file into a list
set fileList to paragraphs of (read file f)

-- modify the file as required into a new list
set newList to {}
repeat with i from 1 to count of fileList
    set thisItem to item i of fileList
    if thisItem is "" then
        set end of newList to emptyLine
    else if thisItem starts with "http" then
        set end of newList to htmlLine1 & thisItem & htmlLine2 & thisItem & htmlLine3
    else
        set end of newList to thisItem
    end if
end repeat

-- make the new list into a string
set text item delimiters to return
set newFile to newList as text
set text item delimiters to ""

-- write the new string back to the file overwriting its contents
set openFile to open for access file f with write permission
write newFile to openFile starting at 0 as text
close access openFile

+1对于模型样本问题,带有样本输入、所需输出和样本代码(gasp)。继续发帖,祝你好运!非常感谢。Applescript可以工作,除了(1)一些汉字会变得乱七八糟,(2)双击新的URLIST.txt无法在Finder中打开它(但可以通过将其图标拖到Safari/Firefox来打开它。文本编码的问题可以纠正吗?如果有问题,您可以将文件读/写为«class utf8»。我添加了一个“编辑部分”感谢你花了这么多时间。由于adayzdone的脚本没有文本编码的问题,我很好奇Applescript和sed之间处理文本编码的区别。功能性和简洁性。谢谢。唯一的问题是,新的URLlist.txt无法在Fi中打开通过双击它(但可以通过将图标拖到Safari/Firefox来打开它)。如何解决这个问题?要通过双击打开文档,请使用“.html”扩展名而不是“.txt”保存文档。
html
文档并不像您想的那么简单,您必须指定DOCTYPE、名称空间(xmlns,xml:lang,lang)和Meta(http等价、内容、字符集)在浏览器中正确显示。请查看我的更新答案。谢谢。您的代码在逻辑上与我要求的不同。事实上,我要处理的真实文本文件比前面提到的示例输入(例如,涉及一些引用的句子)更复杂。(顺便说一句,与其他两个答案不同,您的代码没有文本编码的问题。我不知道为什么…)我以为您希望
跟踪每个URL。对于每一个空白行,请查看我的编辑版本。我很抱歉没有对其进行足够的澄清…对于您的编辑版本,我想您的意思是:第1行
将infle设置为“/Users/myname/Desktop/urlist.txt”
第2行
将myData设置为(do shell script“sed's/\\(http[^]*\\)/\\1/g;s/^$/\\/g'”"ed form of inflee)
第3行
将shell脚本“echo”"ed form of myData和“>”引用的inflee
添加到第二行
将myData设置为(
开头和第一个之前的“\\\”&。非常感谢!是的…复制得太快了。我将编辑的版本更新为正确的脚本。非常感谢您的及时回复!
set inFile to "/Users/myname/Desktop/URLlist.txt"

do shell script "sed -i '' 's:^$:\\&nbsp;<br />:; s:\\(http[^ ]*\\):<a href=\"\\1\">\\1</a>:g' " & quoted form of inFile
set inFile to "/Users/myname/Desktop/URLlist.html" -- text file with a ".html" extension
set nL to linefeed
set prepandHTML to "<!DOCTYPE html>\\" & nL & "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en-US\" lang=\"en-US\">\\" & nL & tab & "<head><meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />\\" & nL & "</head>\\" & nL

do shell script "sed -i '' 's:^$:\\&nbsp;<br />:; s:\\(http[^ ]*\\):<a href=\"\\1\">\\1</a>:g; 1s~^~" & prepandHTML & "~' " & quoted form of inFile
do shell script "echo '</html>' " & quoted form of inFile -- write last HTML tag
set f to (path to desktop as text) & "URLlist.txt"

set emptyLine to "&nbsp;<br />"
set htmlLine1 to "<a href=\""
set htmlLine2 to "\">"
set htmlLine3 to "</a>"

-- read the file into a list
set fileList to paragraphs of (read file f)

-- modify the file as required into a new list
set newList to {}
repeat with i from 1 to count of fileList
    set thisItem to item i of fileList
    if thisItem is "" then
        set end of newList to emptyLine
    else if thisItem starts with "http" then
        set end of newList to htmlLine1 & thisItem & htmlLine2 & thisItem & htmlLine3
    else
        set end of newList to thisItem
    end if
end repeat

-- make the new list into a string
set text item delimiters to return
set newFile to newList as text
set text item delimiters to ""

-- write the new string back to the file overwriting its contents
set openFile to open for access file f with write permission
write newFile to openFile starting at 0 as text
close access openFile
on writeTo_UTF8(targetFile, theText, appendText)
    try
        set targetFile to targetFile as text
        set openFile to open for access file targetFile with write permission
        if appendText is false then
            set eof of openFile to 0
            write «data rdatEFBBBF» to openFile starting at eof -- UTF-8 BOM
        else
            tell application "Finder" to set fileExists to exists file targetFile
            if fileExists is false then
                set eof of openFile to 0
                write «data rdatEFBBBF» to openFile starting at eof -- UTF-8 BOM
            end if
        end if
        write theText as «class utf8» to openFile starting at eof
        close access openFile
        return true
    on error theError
        try
            close access file targetFile
        end try
        return theError
    end try
end writeTo_UTF8

on readFrom_UTF8(targetFile)
    try
        set targetFile to targetFile as text
        targetFile as alias -- if file doesn't exist then you get an error
        set openFile to open for access file targetFile
        set theText to read openFile as «class utf8»
        close access openFile
        return theText
    on error
        try
            close access file targetFile
        end try
        return false
    end try
end readFrom_UTF8