Reading a file line by line from BASH

Words
22
Reading
1 min
Listen
Play
6y

image.png

To read a file line by line from BASH you can use one of the following options:

  1. While, read, operator <
  2. cat, while, read
  3. AWK

For example, if we have the following file (emails.txt):

[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]

Option 1

$ while read line; do echo "Sending email to $line"; done < emails.txt

Option 2

$ cat emails.txt |while read line; do  echo "Sending email to $line"; done

Option 3

I suggest AWK if the file has more than one column since AWK allows to specify columns separator, if we have the following file:

Sybil|[email protected]
Neville|[email protected]
Sean|[email protected]

then we can type:

awk -F'|' '{email=$1" <"$2">"} {print "Sending email to "email;}' emails.txt

Via LiBreByte


Reading a file line by line from BASH | Ecency