-2

I have a file like in following format

User:blala Pass:blala IP:***.***.**.** 

I tried with IFS but it is not working

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" 

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

Please help.

4
  • 3
    Did you try anything?
    – taliezin
    CommentedApr 30, 2015 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"CommentedApr 30, 2015 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
    CommentedApr 30, 2015 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.CommentedApr 30, 2015 at 7:55

2 Answers 2

2

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

User:blala Pass:blala with spaces: and colons: and $PATH IP:***.***.**.** 

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]/./}" 

Output:

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

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 do echo $line|cut -d\: -f2 done < /path/to/file.csv 

This generates this output:

blala blala ***.***.**.** 

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).

5
  • By default, $IFS contains a space and a tab and a newline
    – Peter.O
    CommentedApr 30, 2015 at 9:09
  • true. I'll edit it!
    – quenia
    CommentedApr 30, 2015 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
    CommentedApr 30, 2015 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
    CommentedApr 30, 2015 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
    CommentedApr 30, 2015 at 11:12

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.