Issue
I'm having trouble looping through the lines returned by sed that are stored in a variable.
Currently it looks a bit like this
lines=$(sed -rne '/'"$timestamplastupload"'/,/'"$timestampnow"'/ p' /var/log/test.log)
for line in "$lines"; do
echo "This line is: $line"
done
But so far this isn't working, the loop one runs once and while the contents of the $line variable appears to be the entire $lines variable and thus only printed once not looped through line by line prefixing with "This line is: "
What am I missing to be able to loop through each line of the sed output piece by piece. (Eventually ill be uploading the matching ones line by line to an API) so I need each line separately so I can process them as required.
As requested, some output from sed which has been tided up for security
Wed Feb 28 22:33:11 2018 us=452112 xxx/1.2.3.4:55487 [xxx] Inactivity timeout (--ping-restart), restarting
Wed Feb 28 22:33:11 2018 us=452112 xxx/1.2.3.4:55487 [xxx] Inactivity timeout (--ping-restart), restarting
Wed Feb 28 22:33:11 2018 us=452112 xxx/1.2.3.4:55487 [xxx] Inactivity timeout (--ping-restart), restarting
Wed Feb 28 22:33:11 2018 us=452180 xxx/1.2.3.4:55487 SIGUSR1[soft,ping-restart] received, client-instance restarting
Wed Feb 28 22:33:11 2018 us=452180 xxx/1.2.3.4:55487 SIGUSR1[soft,ping-restart] received, client-instance restarting
Wed Feb 28 22:33:11 2018 us=452180 xxx/1.2.3.4:55487 SIGUSR1[soft,ping-restart] received, client-instance restarting
Wed Feb 28 22:33:11 2018 us=452180 xxx/1.2.3.4:55487 SIGUSR1[soft,ping-restart] received, client-instance restarting
Solution
Don't use for
for reading. Use process substitution instead:
while read -r line; do
# your logic
done < <(sed -rne '/'"$timestamplastupload"'/,/'"$timestampnow"'/ p' /var/log/test.log)
See:
Answered By - codeforester Answer Checked By - Senaida (WPSolving Volunteer)