0
(0)

There are several ways to read a file in loop, here are few of them with examples

consider my input file is as below

FileName: file1

File Content:

A B C
D E F
G H I
J K L

Method 1: using cat command

#!/bin/bash

cat file1 |while read <param1><param2><param3>
do
<your code goes here>
done

Method 2: using while loop command

#!/bin/bash

while read line
do
param1=echo $line | awk '{print $1}'
param2=echo $line | awk '{print $2}'
param3=echo $line | awk '{print $3}'

<your code goes here>

done < $file1

Method 3: using for loop command

This works well when your file contents do not have any white spaces within themselves. they should be either separated by commas,colons,semicolons etc.

consider my input file is as below

FileName: file2

File content:

A:B:C
D:E:F
G:H:I
J:K:L

your loop structure would look like as below.

#!/bin/bash

for i in $(cat file2)
do
first=$(echo $i | cut -d ":" -f 1)
second=$(echo $i | cut -d ":" -f 2)
third=$(echo $i | cut -d ":" -f 3)

<your code goes here>

done


How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.