LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   read from file (https://www.linuxquestions.org/questions/linux-newbie-8/read-from-file-150319/)

newbielinux 02-25-2004 08:41 AM

read from file
 
Hi all

I am wondering how can I read from file and save the values from
file in some variable using script????

-Need help to do that.....

frieza 02-25-2004 08:43 AM

hmm
VARIABLE = `cat file` ?

newbielinux 02-25-2004 08:52 AM

Quote:

Originally posted by frieza
hmm
VARIABLE = `cat file` ?


Thank you for your reply...
I think I was not clear in my question....I can see my file saying

cat filename

but I am looking to have a script that opens file and read contents of file
while saving certain values in variable....

frieza 02-25-2004 08:58 AM

hmm, such as?

i'd look into grep and if

newbielinux 02-25-2004 09:04 AM

Quote:

Originally posted by frieza
hmm, such as?

i'd look into grep and if

--------->>>>>>>>>>>>>>>>>>>>>>>>>
I will try to put my questions in more clear words...........

I am not sure how can I read a file using script and store certain values
from that file to varibles in script..

I am new to this so pls assist if you can.

frieza 02-25-2004 09:13 AM

ok, the values being? words? numbers?

homey 02-25-2004 09:19 AM

Here is an example using read to get the information. It uses a space as a field separator by default.
In this case, the names.txt has information which looks like this...

fred 500
sue 600
tim 500
joe 500

while read USER GROUP reads user then group from each line.

NEW_USERS="/home/names.txt"
cat ${NEW_USERS} | \
while read USER GROUP
do
adduser ${USER} -g ${GROUP}

newbielinux 02-25-2004 09:44 AM

Quote:

Originally posted by homey
Here is an example using read to get the information. It uses a space as a field separator by default.
In this case, the names.txt has information which looks like this...

fred 500
sue 600
tim 500
joe 500

while read USER GROUP reads user then group from each line.

NEW_USERS="/home/names.txt"
cat ${NEW_USERS} | \
while read USER GROUP
do
adduser ${USER} -g ${GROUP}


Thank you very much for your reply.......
although it seems to me it works but I have certain issues related to it....
my file from where I am reading looks like this...
(configuration.test)/////////////////
release test_0.1

patch patch1
//////////////////////////

and my script which is reading this file is:
///////////////////////////////////////////////////////////////////
read_file="/usr/configuration.test"
cat ${read_file}|\
while read USER GROUP
do
adduser ${USER} -g ${GROUP}
done
//////////////////////////////////////////////////

and my output is:
///////////////////////////////////////////////
adduser: unknown group test_0.1
adduser: option requires an argument -- g
usage: adduser [-u uid [-o]] [-g group] [-G group,...]
[-d home] [-s shell] [-c comment] [-m [-k template]]
[-f inactive] [-e expire ] [-p passwd] [-M] [-n] [-r] name
adduser -D [-g group] [-b base] [-s shell]
[-f inactive] [-e expire ]
adduser: unknown group patch1
adduser: option requires an argument -- g
usage: adduser [-u uid [-o]] [-g group] [-G group,...]
[-d home] [-s shell] [-c comment] [-m [-k template]]
[-f inactive] [-e expire ] [-p passwd] [-M] [-n] [-r] name
adduser -D [-g group] [-b base] [-s shell]
[-f inactive] [-e expire ]
adduser: option requires an argument -- g
usage: adduser [-u uid [-o]] [-g group] [-G group,...]
[-d home] [-s shell] [-c comment] [-m [-k template]]
[-f inactive] [-e expire ] [-p passwd] [-M] [-n] [-r] name
adduser -D [-g group] [-b base] [-s shell]
[-f inactive] [-e expire ]
/////////////////////////////////////

I am not sure why I am getting all that options too...........??????????

homey 02-25-2004 10:11 AM

Quote:

adduser: unknown group test_0.1
It starts reading at the top of the file so you can't have anything other than the list you want to read from. In this case, users and groups.
Also, you need to be root to add users.

newbielinux 02-25-2004 10:24 AM

Quote:

Originally posted by homey
It starts reading at the top of the file so you can't have anything other than the list you want to read from. In this case, users and groups.
Also, you need to be root to add users.

--------------------->>>>>>>>>>

Thank you...actually my requirement is bit different...after
reading the file I want to capture certain values leaving certain
lines....ok I should try it with example

File to be read:
////////////////////////////////
release test_0.1

patch
patch1
///////////////////////////////(EOF)
Now I can read all these lines but I need to capture certain
parameters like test_0.1 from first row and patch1 line...

in other words I want to store test_0.1 and patch1 in some variable
...not sure how to achieve it....Thank you for all of your help.....

newbielinux 02-25-2004 11:11 AM

Quote:

Originally posted by newbielinux
--------------------->>>>>>>>>>

Thank you...actually my requirement is bit different...after
reading the file I want to capture certain values leaving certain
lines....ok I should try it with example

File to be read:
////////////////////////////////
release test_0.1

patch
patch1
///////////////////////////////(EOF)
Now I can read all these lines but I need to capture certain
parameters like test_0.1 from first row and patch1 line...

in other words I want to store test_0.1 and patch1 in some variable
...not sure how to achieve it....Thank you for all of your help.....



------------------>>>>>>>>>>>>>>>>>>
I am using this code to read file configuration.test and then trying to use
$release value in sed command.....but if I echo $release outside done loop
I am getting null.....how can I use that value in sed statement..
pls help????????????


read_file="/usr/configuration.test"
cat ${read_file}|\
while read release patch
do
#adduser ${release} -g ${patch}
echo $release
echo $patch
done

#sed "s/test_0.1/`echo $release`/" oldspec.test >spec.test

homey 02-25-2004 01:08 PM

Ok, forget the adduser part as that is not related to what you are doing.
Maybe this is more what you have in mind....

Code:

#!/bin/bash

cat /usr/configuration.test | grep '[0-9]$'| \
sed -e 's/^\(.*\) \(.*\)/\2/' >file.txt

read_file="file.txt"
cat ${read_file}|\
while read word
do
echo " $word"

done



All times are GMT -5. The time now is 01:01 AM.