Answer the question
In order to leave comments, you need to log in
How to process this array in Bash loop?
Hello!
There is a file with a list like:
[email protected]_line
[email protected]_line
[email protected]_line
The idea is as follows: in the while read line loop, check that the variable is equal to the first part of the line (the one before "@"), then perform the action with the second part of the line (the one what's after "@")
That is:
while read line
do
if $1 == "$line" # <<<< вот здесь должна быть проверка части строки, но непонятно как реализовать
then
echo "Second part of the line" #<<<< а вот здесь вывод второй части строки, но также непонятно как реализовать
Answer the question
In order to leave comments, you need to log in
It can be solved through ${VAR#*@} and ${VAR%@*}, or ${VAR##*@} and ${VAR%%@*}
the first @ character that comes across, or the last one.
#!/bin/bash
AAA="[email protected]_line\[email protected]_line\[email protected]_line"
echo -e "${AAA}" | while read line; do
if [ "$1" == "${line%@*}" ]; then
echo "Second part of the line: ${line#*@}"
fi
done
You can use sed
[[email protected] ~]$ text="[email protected]\[email protected]\[email protected]\[email protected]"
[[email protected] ~]$ v="word2"
[[email protected] ~]$
[[email protected] ~]$ echo -e "$text"
[email protected]
[email protected]
[email protected]
[email protected]
[[email protected] ~]$ echo -e "$text" | sed -n '/^'$v'@/ s/^.*@//p'
line2
line4
[[email protected] ~]$
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question