脚本删除关键字后的文本

时间:2011-05-14 18:16:26

标签: php javascript applescript

我有一个带有一堆URL的txt文件,如:

http://url1.com/folder1/folder2
http://url3.com/folder1/folder2
http://url2.com/folder1/folder2

我正在寻找一个会在“.com”之后删除所有内容的脚本。似乎会有一个简单的AppleScript,但我似乎无法找到我要找的东西。有什么想法吗?

3 个答案:

答案 0 :(得分:3)

sed -e“s,.com /.*,, com,g”< infile> OUTFILE

答案 1 :(得分:1)

建立在cbz回答你可以通过带有一些操作的AppleScript运行他的命令

-- choose afile you could also set this to a variable
set infile to choose file 

--lets find out where the file lives so we know where to save the output to
tell application "Finder" to set outpath to quoted form of POSIX path of (container of infile as text)

-- convert the path of file to something shell can understand
set infile to quoted form of POSIX path of infile

--add file name to outpth
set outfile to outpath & "parsed_text.txt"

--put it all together and go, note I rearrange the command so that it will generate the results to a new file
do shell script "sed -e 's,.com/.*$,.com,gw " & outfile & "' " & infile

答案 2 :(得分:0)

这是PHP中的一个。你也可以使用正则表达式,但我觉得使用parse_url可以让你继续使用它,而不必担心会弹出任何特殊情况。

<?php
$lines = file('yourfile.txt');
$sites = '';
foreach($lines as $url) {
    $parsed = parse_url($url);
    $sites .= $parsed['scheme']."://".$parsed['host']."/\n";
}
file_put_contents('yournewfile.txt', $sites);
?>