Принятый ответ обеспечивает подход, который я использовал для удаления URL и т. Д. Из моих файлов.Однако он оставил «пустые» строки.Вот решение.
sed -i -e 's/http[s]\?:\/\/\S*//g ; s/www\.\S*//g ; s/ftp:\S*//g' input_file
perl -i -pe 's/^'`echo "\012"`'${2,}//g' input_file
Флаги GNU sed, используемые выражения:
-i Edit in-place
-e [-e script] --expression=script : basically, add the commands in script
(expression) to the set of commands to be run while processing the input
^ Match start of line
$ Match end of line
? Match one or more of preceding regular expression
{2,} Match 2 or more of preceding regular expression
\S* Any non-space character; alternative to: [^[:space:]]*
Однако
sed -i -e 's/http[s]\?:\/\/\S*//g ; s/www\.\S*//g ; s/ftp:\S*//g'
оставляет непечатаемый символ (ы),предположительно \n
(переводы строк).Стандартные sed
подходы на основе удаления "пустых" строк, вкладок и пробелов, например,
sed -i 's/^[ \t]*//; s/[ \t]*$//'
не работают, здесь: если вы не используете "метку ветки" для обработки новых строк, вы не можетезамените их, используя sed (который читает входные данные по одной строке за раз).
Решение состоит в том, чтобы использовать следующее выражение perl:
perl -i -pe 's/^'`echo "\012"`'${2,}//g'
, которое использует подстановку оболочки,
для замены восьмеричного значения
(т. Е. Перевод строки, \n
), что происходит 2 или более раз,
(иначе мы бы развернули все строки) с чем-то другим;здесь:
то есть, ничего.
[Вторая ссылка ниже предоставляет замечательную таблицу этих значений!]
Используются флаги perl:
-p Places a printing loop around your command,
so that it acts on each line of standard input
-i Edit in-place
-e Allows you to provide the program as an argument,
rather than in a file
Ссылки:
Пример:
$ cat url_test_input.txt
Some text ...
/3790913/sed-dlya-udaleniya-url-iz-faila
https://www.google.ca/search?dcr=0&ei=QCsyWtbYF43YjwPpzKyQAQ&q=python+remove++citations&oq=python+remove++citations&gs_l=psy-ab.3...1806.1806.0.2004.1.1.0.0.0.0.61.61.1.1.0....0...1c.1.64.psy-ab..0.0.0....0.-cxpNc6youY
http://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.TfidfVectorizer.html
https://bbengfort.github.io/tutorials/2016/05/19/text-classification-nltk-sckit-learn.html
http://datasynce.org/2017/05/sentiment-analysis-on-python-through-textblob/
https://www.google.ca/?q=halifax&gws_rd=cr&dcr=0&ei=j7UyWuGKM47SjwOq-ojgCw
http://www.google.ca/?q=halifax&gws_rd=cr&dcr=0&ei=j7UyWuGKM47SjwOq-ojgCw
www.google.ca/?q=halifax&gws_rd=cr&dcr=0&ei=j7UyWuGKM47SjwOq-ojgCw
ftp://ftp.ncbi.nlm.nih.gov/
ftp://ftp.ncbi.nlm.nih.gov/1000genomes/ftp/alignment_indices/20100804.alignment.index
Some more text.
$ sed -e 's/http[s]\?:\/\/\S*//g ; s/www\.\S*//g ; s/ftp:\S*//g' url_test_input.txt > a
$ cat a
Some text ...
Some more text.
$ perl -i -pe 's/^'`echo "\012"`'${2,}//g' a
Some text ...
Some more text.
$