LatinUnit
LatinUnit

Reputation: 159

While loop, how to read from second line of text file

i've tried everything for the past 2 hours to get this working but my experience in shell and programming is limited.

I have a loop

while IFS="," read var1 var2 var3 var4 var5 ; do

statements here...

done < $file

Now the operation inside statement is to read from a text file that has 5 fields as you can see and then use them to create accounts in Linux using bash

useradd $var1 -p var2 -g var3 and so on

I want to make the script to start reading from the second line only, i cant get it working. something like while ifs=, read (from second line) var 1 var 2 var3 etc.

the reason for this is that the file is an exported database from excel to csv so the first line would contain titles like first name dob enrolled etc etc and is not needed

Your help is appreciated

Addition:

Before the users are being created, I have added an if statement and if users meet a condition then they will be created.

if [ "$var5" == "fullyenrolled" ]; then
continue with account creation...
echo "$var1 successfuly created"
else
echo "Sorry user $var1 is not fully enrolled"
fi

the output of the echo is something like

Full name is not fully enrolled
user1 successfuly created
user2 successfuly created

even if i add sed 1d | while IFS= etc etc. it seems that it is still reading the first line this is why im getting the first output like "full name is not fully enrolled"

Upvotes: 8

Views: 17974

Answers (2)

user492203
user492203

Reputation:

A solution using awk:

awk 'NR >= 2 { print }' < "$file"

Upvotes: 1

Elias Dorneles
Elias Dorneles

Reputation: 23796

Use sed to "delete" the first line from the input passed to the while loop

sed 1d $file | while ...
do
         your statements here
done

Upvotes: 12

Related Questions