T
T
Thomas Storm2015-10-09 16:27:26
bash
Thomas Storm, 2015-10-09 16:27:26

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" #<<<< а вот здесь вывод второй части строки, но также непонятно как реализовать

This can be done through AWK or SED, but are there any regular expressions for this (grep it as a last resort)?
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2015-10-09
@v_sadist

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

A
abcd0x00, 2015-10-10
@abcd0x00

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 question

Ask a Question

731 491 924 answers to any question