Reading a file line by line from BASH

in #bash4 years ago

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):

imperdiet.nec.leo@etmalesuadafames.net
molestie.in.tempus@nuncQuisque.edu
a.sollicitudin.orci@ut.edu
lorem.Donec.elementum@arcuNunc.ca
et.rutrum.eu@nisidictumaugue.ca
augue@Cumsociis.co.uk
adipiscing@convallisestvitae.com
Nunc.sed.orci@rhoncusDonecest.org
pede@tempuslorem.net
imperdiet@InloremDonec.ca

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|arcu@dolordolor.ca
Neville|euismod@semperNam.co.uk
Sean|In.lorem.Donec@tinciduntaliquamarcu.edu

then we can type:

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

Via LiBreByte


Sort:  

Congratulations @srdotlibre! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :

You got more than 10 replies. Your next target is to reach 50 replies.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Do not miss the last post from @hivebuzz:

Hive Power Up Day - The countdown is ticking
Hive Power Up Day - Let's grow together!

Yay!
Your post has been boosted with Ecency Points. Keep up the good work!
Dear reader, Install Android: https://android.ecency.com, iOS: https://ios.ecency.com mobile app or desktop app for Windows, Mac, Linux: https://desktop.ecency.com
Learn more: https://ecency.com
Join our discord: https://discord.me/ecency