1834454 Members
2945 Online
110067 Solutions
New Discussion

Getting File Content

 
SOLVED
Go to solution
Pando
Regular Advisor

Getting File Content

I have a file with the following contents..

...
...
DIFFLOTID,0001
...
...

I need to get the 0001 placed into a variable.

Maximum points to correct replies!
5 REPLIES 5
Sridhar Bhaskarla
Honored Contributor

Re: Getting File Content

Hi Fernando,

If there are only two fields are seperated by comma, then you can use 'while'

oIFS=$IFS
IFS=","

while read first next
do

done < your_file
IFS=$oIFS

In the above, 'next' is your variable. The above won't work if there is are multiple commands in each line.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Pando
Regular Advisor

Re: Getting File Content

Im trying out to use the command below:

VAR=$(grep "DIFFLOTID" $FILE |awk -F \" '{print $1}')

but i get the whole string DIFFLOTID,0001
Sridhar Bhaskarla
Honored Contributor
Solution

Re: Getting File Content

Hi,

Use this

VAR=$(awk -F "," '/DIFFLOTID/ {print $2}' your_file)

If there are more than one line containing DIFFLOTID, then VAR will have more than one value.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Pando
Regular Advisor

Re: Getting File Content

Thanks SRI
Pando
Regular Advisor

Re: Getting File Content

Hi SRI,

Thanks for your reply. It was a great help to a neophyte like me. I would like to ask now how can i know if the $var contains pure strings of numbers or alphanumerics?

Many thanks!