Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read lines from File in Bash and parse words into variables for mailx parameters

Tags:

bash

sed

mailx

I have a bash script which reads lines from a text file with 4 columns(no headers). The number of lines can be a maximum of 4 lines or less. The words in each line are separated by SPACE character.

[email protected]   [email protected];[email protected]   Sub1   MailBody1
[email protected]   [email protected];[email protected]   Sub2   MailBody2
[email protected]   [email protected];[email protected]   Sub3   MailBody3
[email protected]   [email protected];[email protected]   Sub4   MailBody4

Currently, I am parsing the file and after getting each line, I am storing each word in every line into a variable and calling mailx four times. Wondering if is there is an elegant awk/sed solution to the below mentioned logic.

  • find total number of lines
  • while read $line, store each line in a variable
  • parse each line as i=( $line1 ), j=( $line2 ) etc
  • get values from each line as ${i[0]}, ${i[1]}, ${i[2]} and ${i[3]} etc
  • call mailx -s ${i[2]} -t ${i[1]} -r ${i[0]} < ${i[3]}
  • parse next line and call mailx
  • do this until no more lines or max 4 lines have been reached

Do awk or sed provide an elegant solution to the above iterating/looping logic?

like image 481
Shon Avatar asked Jul 20 '11 03:07

Shon


People also ask

Which is used to read lines from a file and split them into variables?

readlines() is used to read all the lines at a single go and then return them as each line a string element in a list. This function can be used for small files, as it reads the whole file content to the memory, then split it into separate lines.

How read file line by line in shell script and store each line in a variable?

We use the read command with -r argument to read the contents without escaping the backslash character. We read the content of each line and store that in the variable line and inside the while loop we echo with a formatted -e argument to use special characters like \n and print the contents of the line variable.


2 Answers

Give this a shot:

head -n 4 mail.txt | while read from to subject body; do
    mailx -s "$subject" -t "$to" -r "$from" <<< "$body"
done
  • head -n 4 reads up to four lines from your text file.
  • read can read multiple variables from one line, so we can use named variables for readability.
  • <<< is probably what you want for the redirection, rather than <. Probably.
like image 76
John Kugelman Avatar answered Sep 22 '22 23:09

John Kugelman


The above while loop works well as a simple alternative to sed and awk if you have a lot of control over how to display the lines of text in a file. the read command can use a specified delimiter as well, using the -d flag.

Another simple example:

I had used mysql to grab a list of users and hosts, putting it into a file /tmp/userlist with text as shown:

user1 host1
user2 host2
user3 host3

I passed these variables into a mysql command to get grant info for these users and hosts and append to /tmp/grantlist:

cat /tmp/userlist | while read user hostname;
do
  echo -e "\n\nGrabbing user $user for host $hostname..."
  mysql -u root -h "localhost" -e "SHOW GRANTS FOR '$user'@$hostname" >> /tmp/grantlist
done
like image 40
moo Avatar answered Sep 22 '22 23:09

moo