Помните, что вызов awk
или sed
вызывает новую программу, когда Bash уже запущен. Вместо этого вы можете отфильтровать ваш ввод непосредственно в Bash:
$ while read url; do echo ${url%.*}; done <<EOL
129.168.100.1.111
1291.168.200.10.111
EOL
129.168.100.1
1291.168.200.10
То есть
while read url
do
echo ${url%.*}
done < urls.txt
Ссылки
Расширение параметра (см. man bash
) {url%.*}
использует переменную $url
и удаляет (%
) все, что соответствует шаблону .*
. Обратите внимание, что сопоставление с образцом отличается от регулярных выражений. В частности, сопоставление с образцом имеет более простой синтаксис. То есть .
соответствует буквенному символу .
, а *
соответствует любой строке. Сопоставление с образцом чаще всего известно для шаблонов имен файлов, как в *.txt
и т. Д.
${parameter%word}
${parameter%%word}
Remove matching suffix pattern. The word is expanded to produce
a pattern just as in pathname expansion. If the pattern matches
a trailing portion of the expanded value of parameter, then the
result of the expansion is the expanded value of parameter with
the shortest matching pattern (the ``%'' case) or the longest
matching pattern (the ``%%'' case) deleted. If parameter is @
or *, the pattern removal operation is applied to each posi-
tional parameter in turn, and the expansion is the resultant
list. If parameter is an array variable subscripted with @ or
*, the pattern removal operation is applied to each member of
the array in turn, and the expansion is the resultant list.