Я пытаюсь удалить последние знаки кроме алфавитов:
support.help1.com,,
support.help1.com.
support.help1.com9
support.help1.com*
support.help1.com@@
support.help1.com##
support.help1.com%%
support.help1.com^
support.help1.com
support.help1.com,
support.help1.com-
Я хочу свой вывод как это:
support.help1.com
support.help1.com
support.help1.com
support.help1.com
support.help1.com
support.help1.com
support.help1.com
support.help1.com
support.help1.com
support.help1.com
support.help1.com
sed может помочь также:
command | sed 's/[^a-Z]*$//g'
# create the example output
$ echo "support.help1.com,,
support.help1.com.
support.help1.com9
support.help1.com*
support.help1.com@@
support.help1.com##
support.help1.com%%
support.help1.com^
support.help1.com
support.help1.com,
support.help1.com-" > trailexample.txt
# now edit this stream
# something like $ command_output | sed
$ cat trailexample.txt | sed 's/[^a-Z]*$//g'
support.help1.com
support.help1.com
support.help1.com
support.help1.com
support.help1.com
support.help1.com
support.help1.com
support.help1.com
support.help1.com
support.help1.com
support.help1.com
# explanation
# sed (replace) 's/this/by-this/g' :: sed 's/[^a-Z]*$//g'
# s : substitution command, we want to substitute strings
# The 'this' [^a-Z]*$ : regexp pattern
# ^ mean not
# a-Z means all aLphBetiCaL chars
# []* any number of what is in brackets
# $ means end of line
# So the 'this' is 'any number of consecutive non-alphabetical chars before end of line'
# And the 'by-this' is empty, nothing, nada, void :: //
# g : global substitution command, means do the replacement for all occurrences
Если можно использовать regex, просто загрузить каждую команду и использование, что regex ниже (получил его от здесь ):
^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$
Этот regex принимают URL с http
/ https
. Просто используйте его, чтобы подтвердить, допустим ли Ваш URL, ifnot, просто загрузите строку путем удаления последнего знака. Можно использовать этот обходное решение для этого:
string="string.help1.com&&"
foo=string
while [ !regex(foo) ]; do
foo=${foo%?}
done
print foo
NB: regex(foo)
является просто функцией, которая получила строку, возвратитесь True
, если regex хорошо, False
в случаях других
NB2: мой синтаксис, вероятно, не правилен, но он должен только дать Вам подсказку
Можно использовать остроту жемчуга для этого:
perl -pne 's/[^a-zA-Z]*$/\n/g' input.txt
Это читает содержание input.txt
linewise и заменяет все неалфавитные символы ([^a-zA-Z]*$
) в конце строки с новой строкой (\n
)
Это - классический поиск regex, и замените https://regex101.com/r/gRiUTc/2
Через оболочку, которую Вы могли использовать
<input sed -r 's/(\W+|[0-9]+)$//g'