0

I have a big file called file1 on my ESX host. I run some awk command on the file and extract useful blocks of data into a variable which works fine on the busybox shell.

Now I want to read from that variable (info) line by line, change something and write to a different file.

info=$(awk '/pat1/{flag=1}/pat2/{flag=0}flag' file1)  -> works fine

while IFS= read -r line; do

printf '%s\n' "$line" 

done <<< "$info"

but the while loop doesn't work. Get error as "unexpected redirection". I also tried `done << "$info". Then I get error as

syntax error: unexpected end of file (expecting "}")

So basically how to read from a variable line by line?

Thanks Would appreciate your response

1 Answers1

0

The here-string redirection <<< isn't a standard feature, and Busybox's sh doesn't support it. The error message is a bit odd, but perhaps it parses it as << followed by <. Also the here-doc syntax, started by << is a different thing completely.

You'll have to do something like

echo "$info" | while IFS= read -r line; do 
    printf '%s\n' "$line" 
done

Or pass the data through a temporary file:

tmpfile=$(mktemp)
awk '/pat1/{flag=1}/pat2/{flag=0}flag' file1 > "$tmpfile"
while IFS= read -r line; do 
    printf '%s\n' "$line" 
done < "$tmpfile"
rm "$tmpfile"

But see Why is my variable local in one 'while read' loop, but not in another seemingly similar loop? if you use the first one.

ilkkachu
  • 138,973