Storing the values in text file using while loop in shell script

Hi Frdz

while read line
do
name=`echo $line | cut -d' ' -f 1 `
password=`echo $line | cut -d`-` -f 2`

name > logfile.txt
password > logfile.txt

done < list.txt

When it is run, am getting last values in list.txt file only,it is not storing lall the list entry values. How can i get all the values in logfile.txt by iterating list.txt file.

Thnx

how can it sort???
it simple print the first field in name and second field of password one after one in new file..
what exactly you want go through the man page of sort command

-vidya

There are a lot of syntax errrors:
And you need to use echo to print the contents of a variable..
You need to use redirection in append mode or else your previous data will be overwritten.
You need to use proper syntax of cut command.

echo "" > logfile.txt
while read line
do

name=`echo $line | cut -d " " -f1 `
password=`echo $line | cut -d "-" -f2`
echo $name >> logfile.txt
echo $password >> logfile.txt
done < list.txt

-Devaraj Takhellambam

i believe that this is a typo:

name > logfile.txt
password > logfile.txt

you meant:

echo $name > logfile.txt
echo $password > logfile.txt

what is wrong here is that the ">" always overwrite the output file. use ">>" instead so it will append. make sure that the output file is blank before the start of the loop like:

cat /dev/null > logfile.txt

Thanks Guys for Quick Reply

When you post code, please wrap it in

 tags.


while read line
do
  name=${line%% *}  ## there's no need for cut
  temp=${line#*-}
  password=${temp%%-*}

  printf "%s\n" "$name" "$password"

done < list.txt  > logfile.txt