LordBoBCUP
LordBoBCUP

Reputation: 103

Loop through results of sed

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

Upvotes: 5

Views: 3821

Answers (2)

codeforester
codeforester

Reputation: 42999

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:

Upvotes: 4

Maciek
Maciek

Reputation: 792

The output of your sed would be useful (because of dealing with endlines), but other than that, it seems that this question provides an answer that would go along these lines:

IFS=' ' read -r -a array <<< "$lines"
for element in "${array[@]}"
do
    echo "$element"
done

Upvotes: 0

Related Questions