LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-19-2012, 12:24 PM   #1
rameshpaul_cog
LQ Newbie
 
Registered: May 2012
Posts: 19

Rep: Reputation: 0
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...
 
Old 06-19-2012, 12:42 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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'?
 
Old 06-19-2012, 12:57 PM   #3
rameshpaul_cog
LQ Newbie
 
Registered: May 2012
Posts: 19

Original Poster
Rep: Reputation: 0
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...
 
Old 06-19-2012, 01:11 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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[*]}"
 
Old 06-19-2012, 01:17 PM   #5
rameshpaul_cog
LQ Newbie
 
Registered: May 2012
Posts: 19

Original Poster
Rep: Reputation: 0
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

Last edited by rameshpaul_cog; 06-19-2012 at 01:18 PM.
 
Old 06-19-2012, 01:19 PM   #6
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,979
Blog Entries: 1

Rep: Reputation: 209Reputation: 209Reputation: 209
Quote:
Originally Posted by rameshpaul_cog View Post
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
 
Old 06-19-2012, 01:23 PM   #7
rameshpaul_cog
LQ Newbie
 
Registered: May 2012
Posts: 19

Original Poster
Rep: Reputation: 0
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...

Last edited by rameshpaul_cog; 06-19-2012 at 01:30 PM.
 
Old 06-19-2012, 01:25 PM   #8
rameshpaul_cog
LQ Newbie
 
Registered: May 2012
Posts: 19

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by custangro View Post
It would help if you'll let us know what shell you are using

--C
Am using bash shell
 
Old 06-19-2012, 01:32 PM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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
 
Old 06-19-2012, 01:39 PM   #10
rameshpaul_cog
LQ Newbie
 
Registered: May 2012
Posts: 19

Original Poster
Rep: Reputation: 0
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 !!

Last edited by rameshpaul_cog; 06-20-2012 at 04:29 AM.
 
Old 06-19-2012, 06:30 PM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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.
 
Old 06-20-2012, 12:52 AM   #12
bsat
Member
 
Registered: Feb 2009
Posts: 347

Rep: Reputation: 72
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[*]}
 
Old 06-20-2012, 04:01 AM   #13
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
@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.
 
Old 06-20-2012, 04:25 AM   #14
rameshpaul_cog
LQ Newbie
 
Registered: May 2012
Posts: 19

Original Poster
Rep: Reputation: 0
Thanks Bsat.. ..

Thanks grail....

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

Last edited by rameshpaul_cog; 06-20-2012 at 04:30 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Trouble reading input inside bash while loop Ineed$ Programming 5 01-21-2012 06:27 AM
[SOLVED] Function goes into infinite loop when reading from a file vikas027 Programming 9 10-08-2011 12:54 AM
[SOLVED] Bash - While Loop reading from two lists simultaneously - nested while loop wolverene13 Programming 11 10-01-2011 05:00 PM
Unexpected result in while do done loop reading file steven.c.banks Linux - General 2 05-01-2008 12:32 PM
How to store text(strings) in a 2D character array reading from a text file(C++) bewidankit Programming 3 02-14-2008 07:08 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration