Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

for loop syntax with grep in bash

I want to do text processing for lines in file.txt that ends with 0, I wrote an easy form of it to ask my question clearly.

$ cat file.txt
1 343 4352 0
324 4324 4324 324
432 432 2345 0

$ cat script.sh
for i in `grep " 0$" file.txt`
do
  echo $i; 
done

I want the output to be:

1 343 4352 0

432 432 2345 0

I want $i variable to be "1 343 4352 0" and after that to be "432 432 2345 0" but in this script $i variable is valued 1 then 343

like image 954
Fattaneh Talebi Avatar asked Nov 17 '25 08:11

Fattaneh Talebi


1 Answers

You can do this:

IFS=$'\n' arr=($(grep ' 0$' file.txt))
for  i in "${arr[@]}"
do
  echo "$i"
  #do other tasks here.
done
like image 62
Jahid Avatar answered Nov 19 '25 23:11

Jahid



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!