скрипт для удаления текста после ключевого слова - PullRequest
1 голос
/ 14 мая 2011

У меня есть текстовый файл с кучей URL-адресов, таких как:

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

Я ищу скрипт, который удалит все после ".com". Кажется, для этого будет простой яблочный сценарий, но я не могу найти то, что ищу. Есть идеи?

Ответы [ 3 ]

3 голосов
/ 14 мая 2011

sed -e "s, .com /.*$,. Com, g" outfile

1 голос
/ 16 мая 2011

основываясь на ответе 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
0 голосов
/ 14 мая 2011

Вот один в 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);
?>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...