Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read line by line from a file and execute inner contents

Tags:

linux

shell

unix

sh

How do I read contents from a file line by line with spaces and execute a portion of that line

For example I have the following in my file

Hello world $(echo 9923,3443,434,344 | cut -d"," -f4)
Hello world $(echo 9923,3443,434,344 | cut -d"," -f2)
Hello world $(echo 9923,3443,434,344 | cut -d"," -f1)

My Expected result would be

Hello world 344
Hello world 3443
Hello world 9223

What I get is by echoing in a while loop

Hello world $(echo 9923,3443,434,344 | cut -d"," -f4)
Hello world $(echo 9923,3443,434,344 | cut -d"," -f2)
Hello world $(echo 9923,3443,434,344 | cut -d"," -f1)

My code will be something like

while read LINE
do
     echo $LINE
done < FILE

I have tried several things like using backticks,double quotes,eval nothing seems to work.

like image 966
Yuvi Avatar asked Dec 12 '25 01:12

Yuvi


1 Answers

Try this:

while read line; do
    eval "echo $line";
done < FILE
like image 124
Alexey Shmalko Avatar answered Dec 13 '25 22:12

Alexey Shmalko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!