LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Reading a multiple file inside loop and store! (https://www.linuxquestions.org/questions/linux-newbie-8/reading-a-multiple-file-inside-loop-and-store-4175412298/)

rameshpaul_cog 06-19-2012 12:24 PM

Reading a multiple file inside loop and store!
 
Hi ,

This might be a repeated question but i searched alot to figure out a way.. but unfortunately i couldn't find...
i have a file file.list
cat file.list
text1:text2:text3
text4:text5:text6
....
am writing a script to manipulate the strings and store it in a vairable outside the loop

Code:

IFS=':'
while read  fa sa ta
do
a=$(echo "$fa" )
b=$(echo "$sa" )
c=$(echo "$ta" )
echo $a
echo $b
echo $c
done < file.list
echo $c

In this case it returns the value of the last line only
but, i want the files to be read line by line and use the inputs outside the loop...

Am sorry if this is already existing query or someone might have answered.. but i couldn't figure out ....

Any help would be really appreciated !!

Thanks...

grail 06-19-2012 12:42 PM

Firstly, the echoes are redundant as you can simply assign one variable to another:
Code:

a=$fa
You may need to explain further what your requirement is?

Currently the value of variable 'c' will of course only hold the last value assigned to it, which in your example would be 'text6'.

Would you explain further what you think "should" be in 'c'?

rameshpaul_cog 06-19-2012 12:57 PM

yeah...correct ... it will display text6...

Precisely,

If the script reads the first line it will have 3 variable that is in this case a,b,c and i want those variables to be used outside the loop ....
and subsequently after reading each line it has to perform some tasks with the variable outside the loop....

please guide to use the variables stored and make use of that outside the loop anywhere in the script..

Hope this serves the question !

am not constraint with while we can use any...

grail 06-19-2012 01:11 PM

If I understand correctly I would use arrays to access your data:
Code:

while IFS=':' read  fa sa ta
do
    a+=( "$fa" )
    b+=( "$sa" )
    c+=( "$ta" )
done < file.list

echo "${c[*]}"


rameshpaul_cog 06-19-2012 01:17 PM

Its giving error... :(
./small.sh: line 39: syntax error near unexpected token `"$fa"'
./small.sh: line 39: ` a+=( "$fa" )'

am using bash shell and aix 5.3

custangro 06-19-2012 01:19 PM

Quote:

Originally Posted by rameshpaul_cog (Post 4707090)
Its giving error... :(
./small.sh: line 39: syntax error near unexpected token `"$fa"'
./small.sh: line 39: ` a+=( "$fa" )'

am using bash shell and aix 5.3

It would help if you'll let us know what shell you are using

--C

rameshpaul_cog 06-19-2012 01:23 PM

lemme explain my whole requirement.. hope that fills the gap

i have to execute a sql in unix based upon the input values of the file.list
once if i read the first line.. it has to sent the inputs to other file
@$a:$b/$c in this format ...i have to redirect the output to other file say output.txt

like wise it has to manipulate for all the lines of the file.list

If you need more explanation... i will give .. but... am not getting any method to perform this...

rameshpaul_cog 06-19-2012 01:25 PM

Quote:

Originally Posted by custangro (Post 4707091)
It would help if you'll let us know what shell you are using

--C

Am using bash shell

grail 06-19-2012 01:32 PM

I would guess it may be an older version of bash (needs to be 3.1 or newer).

Why do you need to store in anything after the loop if all you need to do is simply make a new file?
Code:

while IFS=':' read  fa sa ta
do
    echo "@$fa:$sa/$ta" >> output.txt
done < file.list


rameshpaul_cog 06-19-2012 01:39 PM

thanks grail..that was a good one....:)

but...small prob in that....

i have to echo it and ask for the user input if the details provided are correct...later on only i can redirect it...

and also sorry for confusion...is there any other way that i can use it some where outside the loop instead of redirecting it ?...

that would be really helpful i think.. because....instead of creating a file i can use outside the loop right ?...and perform the same which i want to do in the created file...

let me give my entire script

Code:

IFS=":"
while read a b c
do
echo "-----------------"
echo  "$a"
  echo "$b"
  echo "$c"

done < file.list

while [[ $option != yes && $option != no ]]
do
        echo Are the information looks good ? Please answer [yes or no];
        read option;

done

if [ "$option" = "yes" ]; then

echo " installing the commands  "

@$b/$c  [this is the part i want the variables to be used]

elif [ "$option" = "no" ]; then

        echo "am not going to install "

fi


Sorry for bothering !!

chrism01 06-19-2012 06:30 PM

The point is that if you use
Code:

while read
.
.
.
done<file

syntax, then it effectively creates a subshell to run the loop in and the vars will not be available outside the loop.
If you have multiple lines on input to process, then out the 'done<file' at the bottom of the script.

bsat 06-20-2012 12:52 AM

Does this help?
Code:


#!/bin/bash
i=0
while IFS=":" read fa sa ta
do

x[i]=$fa
((i++))

x[i]=$sa
((i++))

x[i]=$sa
((i++))


done < input1
echo ${x[*]}


grail 06-20-2012 04:01 AM

@chrism01 - The example you provided is the one that does not create a sub shell, however the following would:
Code:

cat file | while ...
@OP - Yes you can still use your read to get information from the user within the loop and as demonstrated earlier, simply store the data in arrays is you wish to use
outside the loop.

rameshpaul_cog 06-20-2012 04:25 AM

Thanks Bsat.. ..

Thanks grail....

it works exactly... i can call the variable based upon the i value......thanks lot:)


All times are GMT -5. The time now is 07:52 PM.