Creating variable by for loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Creating variable by for loop
# 1  
Old 01-28-2012
Creating variable by for loop

i have a file 'detail' which contains
Code:
 cat detail
111111
222222
333333
444444

but detail may be 4 line file.6 line file or 8 line file like
Code:
cat detail
111111
222222
333333
444444
555555
666666
777777
888888

so i want a declare a loop which assign the value of first line in one variable and second to second variable and so on as per the file whatever it is 4 , 6 or 8.
please help.

thanx

Last edited by Franklin52; 01-31-2012 at 03:14 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 01-28-2012
What exactly do you want to with this file?
You want to print those lines or give them as input?
# 3  
Old 01-28-2012
i want to use those variable as an input to further part...............
# 4  
Old 01-28-2012
you can use while loop if you want to input all to some script...
Code:
while read var
do
  thing-to-be-done
done < file-name

Or if you just want to print them :

Use :
Code:
sed -n <line to be printed>p file-name


Last edited by Franklin52; 01-28-2012 at 10:22 AM.. Reason: Please use code tags for code and data samples, thank you
# 5  
Old 01-28-2012
thanx for ur reply but i hv tried this................
Code:
export line1=`sed -n '1p' details.txt`
export line2=`sed -n '2p' details.txt`
export line3=`sed -n '3p' details.txt`
export line4=`sed -n '4p' details.txt`

but i want to do by loop to assign the variable......can u please help me out how to write
exact code for this?

Last edited by Franklin52; 01-31-2012 at 03:15 AM.. Reason: Please use code tags for code and data samples, thank you
# 6  
Old 01-28-2012
Well, what's your system, what's your shell?

In BASH you can do this:

Code:
C=0
while read LINE
do
        read "VAR${C}" <<<$"LINE"
        ((C++))
done <file

which will set VAR0, VAR1, ...

---------- Post updated at 01:11 PM ---------- Previous update was at 11:55 AM ----------

Actually, this will work in any shell:

Code:
C=0

while read LINE${C}
do
        C=`expr $C + 1`
done < file

# 7  
Old 01-29-2012
i used first code which giving an error
Code:
 `<' is not expected.

in this line
Code:
read "VAR${C}" <<<$"LINE"


Last edited by Franklin52; 01-31-2012 at 03:15 AM.. Reason: Please use code tags for code and data samples, thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Creating new file inside a for loop

Hi, I have list of files present in a folder. I want to search for a particular keyword sequentially and create a file which will be later used by some other program. Input files: $ ls a.dsx b.dsx c.dsx Dataline_.txt Dataline.txt loop.sh $ cat *.dsx help tyiis global for i in... (4 Replies)
Discussion started by: mac4rfree
4 Replies

2. Shell Programming and Scripting

Creating a loop for multiplying columns

I have 2 files, that look like this: ID SNP1 SNP2 SNP3 SNP4 A1 1 2 0 2 A2 2 0 1 1 A3 0 2 NA 1 A4 1 1 0 2 and this: SNP score SNP1 0.5 SNP2 0.7 SNP3 0.8 SNP4 0.2 Basically, all of the SNP-values are 0,1, 2 or NA, and they each have a score, listed in the second file. The total... (5 Replies)
Discussion started by: kayakj
5 Replies

3. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies

4. Shell Programming and Scripting

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now... (5 Replies)
Discussion started by: mrm5102
5 Replies

5. Shell Programming and Scripting

[SHELL: /bin/sh] For loop using variable variable names

Simple enough problem I think, I just can't seem to get it right. The below doesn't work as intended, it's just a function defined in a much larger script: CheckValues() { for field in \ Group_ID \ Group_Title \ Rule_ID \ Rule_Severity \ ... (2 Replies)
Discussion started by: Vryali
2 Replies

6. UNIX for Dummies Questions & Answers

Creating a loop to go through grep output

I'm doing this script in my Unix class and I've come to a roadblock. The purpose of this script is to search users directories for files that contain bad words i.e kill murder bomb etc., and then be able to ignore legitimate files with each use. I got the searching and ignoring part down but now... (1 Reply)
Discussion started by: jrod44
1 Replies

7. UNIX for Dummies Questions & Answers

creating shell loop homework help

Hello, i am new to unix and i had done very little assignment in unix. I need help on a homework assignment where i am require to create my own file call myshell. In the file it must have the commands that loop. Instead of displaying a regular command path like my computer name instead it is... (1 Reply)
Discussion started by: megaearth77
1 Replies

8. Shell Programming and Scripting

Creating a loop in csh

I have the following code and want to use a loop to output the results to the fparams file. if ($optparams == 1) then # Set the tdarwin parameters set txt01 = "Call to raytrac.csh" set txt02 = "" set txt03 = "./Scripts/raytrac.csh $*" set txt04 = "" set txt05 =... (0 Replies)
Discussion started by: kristinu
0 Replies

9. Shell Programming and Scripting

redirecting output and creating condition for while loop.

I have 2 questions. 1) Is there a means of directing output to a file (">") while making it still output to the console? I have a script that calls another lengthy script. 2) I can direct the output of the lengthy script and grep it for the words "good" or "bad" to know if I need the run... (2 Replies)
Discussion started by: mrwatkin
2 Replies

10. Shell Programming and Scripting

Creating loop for a script -Perl

Hi Guyz I designed a script that can compare 2 columns(values) of single file and gives the closest numbers to the first column by comparing the numbers in first column with second and it works in a single file. Now I'm trying to design a new script with 2 objectives for 2 files (not a single... (4 Replies)
Discussion started by: repinementer
4 Replies
Login or Register to Ask a Question