Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

read a word from a file 2

Status
Not open for further replies.

Shelllearner

Programmer
Oct 18, 2006
2
CR
Hi, I am learning to use shell scripts for my job.
I need to read a file and get the third line that goes like this
Keywords = ,denie, fail, failed, ......

there I need to store in a variable each word after =
I have tryed several things but I cannot make it.

help please
 
Hi

[tt]bash[/tt]'s [tt]read[/tt] can put the fields in an array. So this will output the word "fail" from your example :
Code:
i=0; while IFS="," read -a v; do test $((++i)) -ne 3 && continue; echo ${v[2]}; done < /input/file

Feherke.
 
awk is a useful tool for extracting information out of files like that.

You could use a script like this to pull out everything after the = on the third line of the file.

[tt]awk -F= 'NR==3 { print $2 }' /input/file[/tt]

In other words, when the number of records (NR) is equal to 3, print the second field. -F= means that the fields are separated by "=".

You can set shell variables to a value using a syntax like this:

[tt]VARIABLENAME=`somecommand`[/tt]

So if you combine the two, you get:

[tt]VARIABLENAME=`awk -F= 'NR==3 { print $2 }' /input/file`[/tt]

Hope that helps.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top