w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Looping multiple lines of text with variables in bash

One solution is:

grep -r -i "word|word|word|word|word" /home >>/var/testlog
while IFS= read -r line
do
    [[ $line =~ (/home)/([^/]+)/([^:]*):(.*) ]] || echo Failed on
line=$line
    echo "User is: ${BASH_REMATCH[2]}, The line flag with the word is:
${BASH_REMATCH[4]}, and the file path for the text is:
${BASH_REMATCH[1]/${BASH_REMATCH[2]}}/${BASH_REMATCH[3]}."
done <testlog

If the use of sed is allowed, then the while loop is unnecessary:

grep -r -i "word|word|word|word|word" /home >>/var/testlog
sed -r 's|(/home/(w+)/[^:]*):(.*)|User is: 2, The line flag with the word
is: 3, and the file path for the text is: 1.|' testlog




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.