I have a file like in following format


I tried with IFS but it is not working

while IFS=',' read -r f1 f2 f3 f4 f5 f6 f7 
  echo "$f1 $f2 $f3 $f4 $f5 $f6 $f7" 
done < "$input"

I want to get User, Pass, IP values in my shell script .

Please help.

  • 3
    Did you try anything? – taliezin Apr 30 '15 at 6:33
  • Ya i tried with IFS but it is not working Like this input="/path/to/your/input/file.cvs" while IFS=',' read -r f1 f2 f3 f4 f5 f6 f7 do echo "$f1 $f2 $f3 $f4 $f5 $f6 $f7" done < "$input" – user112534 Apr 30 '15 at 6:48
  • You can edit your question with this info and what you are trying to achieve, so people can see what you have tried and will be able to help you. – taliezin Apr 30 '15 at 6:51
  • 2
    Rather than just copying some other command that you saw somewhere, I suggest that you look at bash(1) and/or other bash (or Bourne shell) documentation (try searching this site), and see how IFS= is supposed to work.  Hint: IFS=',' is not a magic bullet that solves all problems. – G-Man Apr 30 '15 at 7:55

I'm not really sure if I get the point of your question.

At first: IFS is a variable, that contains a separator, like a tab, a space or something. By default it contains a space, tab and newline.

EDIT: from a for-loop loop to a while-loop, suggested by terdon

while read line
        echo $line|cut -d\: -f2
done < /path/to/file.csv

This generates this output:


You can do this with a while-loop as well, in personal I just like the for-loop here more.

What is the loop doing? At first, the loop reads the first line of the file.csv and saves it to the variable "line".

Inside the loop we echo the variable $line and cut it into peaces. We cut it with the delimiter (-d) ":", because in youre file.csv there is always a ":" between the identifier and the value. with the field-indicator (-f) we only want to show us the field 2 (-f2). The for-loop ends when the file.csv has no more lines.

For more informations see the man page of cut(1).

  • By default, $IFS contains a space and a tab and a newline – Peter.O Apr 30 '15 at 9:09
  • true. I'll edit it! – quenia Apr 30 '15 at 9:11
  • Welcome to U&L and thanks for your answer. Please (re-)read the help->tour especially the part about chit-chat. Greetings are inappropriate on this Q&A site and your name is already below the answer based on your profile – Anthon Apr 30 '15 at 9:26
  • You may like for loops more, but I'm afraid they're very simply wrong here. Your answer will break if there is any whitespace on the line. There is basically never good reason to use for i in $(cat file). You should always use while read i; do ... done < file instead. – terdon Apr 30 '15 at 10:36
  • I never really thought about the different behaviour of a for and a whileloop. for this it works at least - I but your comment to my answer. – quenia Apr 30 '15 at 11:12

It may be tempting to use eval, but as passwords can contain arbirary chars (which could/would be executed as code, it makes it risky; bordering on a plain-and-simple: "Don't do it!".
This works - using arrays.

Contents of test file

Pass:blala with    spaces: and colons: and $PATH

set -f                          # disable globbing
IFS=$'\n'                       # 'word' split only at newline char
value=($(sed 's/:/\n./' file))  # '.' to cater for empty value fields

echo user="${value[1]/./}" 
echo pass="${value[3]/./}" 
echo   ip="${value[5]/./}"


pass=blala with    spaces: and colons: and $PATH
  • @StéphaneChazelas - I've added set -f - thanks – Peter.O Apr 30 '15 at 9:44
  • Note that it assumes non-empty fields (in zsh, you can use IFS=$'\n\n' to work around that). – Stéphane Chazelas Apr 30 '15 at 9:59
  • @StéphaneChazelas - thanks again - fixed for empty 'value' fields. – Peter.O Apr 30 '15 at 21:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.