Reading a string and passing passing arguments to a while loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading a string and passing passing arguments to a while loop
# 1  
Old 01-14-2012
Reading a string and passing passing arguments to a while loop

I have an for loop that reads the following file

Code:
cat param.cfg
val1:env1:opt1
val2:env2:opt2
val3:env3:opt3
val4:env4:opt4
.
.

The for loop extracts the each line of the file so that at any one point, the value of i is
Code:
val1:env1:opt1

etc...

I would like to extract each individual attribute and pass it to a while loop as follows:

Code:
for i in $( cat refire.cfg | tail +$START | head -$STOP | grep -v "^#" | grep ':' )
do
# read the value of i here and break it down as arguments x y z for this loop
while read x y z 
do
stuff
done
done

Quote:
The variables $START & $STOP determines the range of execution of the param.cfg file
I want to be able to repeat the while loop for each iteration of i
# 2  
Old 01-14-2012
If I got it right, you can break it down altering IFS:
Code:
OLDIFS=$IFS
IFS=":"

while read A B C; do
   do someting here with $A or $B or $C
done < infile

IFS=$OLDIFS

This User Gave Thanks to zaxxon For This Post:
# 3  
Old 01-14-2012
Quote:
Originally Posted by zaxxon
If I got it right, you can break it down altering IFS:
Code:
OLDIFS=$IFS
IFS=":"
 
while read A B C; do
   do someting here with $A or $B or $C
done < infile
 
IFS=$OLDIFS

Thank you Zaxxon.

I understand from your code what you're trying to read the i/p file in the while loop. I actually want to read the value of the variable i from the for loop.

When i try to make the while loop from $i , i am getting the following error :
Quote:
$i: ambiguous redirect
Any thoughts?

---------- Post updated at 03:49 PM ---------- Previous update was at 03:27 PM ----------

Is there any way that the value of the variable i be stored in a file or a variable that can be read by the while loop.

I have tried writing to a file and it does not work.

Code:
for i in $( cat rparam.cfg | tail +$RESPONSE | head -$RESPONSESTOP | grep -v "^#" | grep ':' )
 
echo $i
$i > .tempparam
while read x y z
do
<stuff>
done < tempparam
rm -f tempparam
done

# 4  
Old 01-14-2012
Maybe you post a snippet of that refire.cfg or now called rparam.cfg so we can provide some more direct code. The grep'ing might not be needed to supply a for loop when being used directly on that file.
# 5  
Old 01-14-2012
Quote:
Originally Posted by zaxxon
Maybe you post a snippet of that refire.cfg or now called rparam.cfg so we can provide some more direct code. The grep'ing might not be needed to supply a for loop when being used directly on that file.
Here you go. To be honest, i dont even need the while loop above as long as the arguments x, y and z can be extracted from the variable i
Code:
RED:value1:Y
RED:value2:Y
#RED:value3:Y
BLUE:value4:Y
RED:value5:Y
RED:value6:Y
BLUE:value7:Y
BLUE:value8:Y
RED:value9:Y
RED:value10:Y
RED:value11:y

# 6  
Old 01-14-2012
In your original example the variable i was assigned the whole record (e.g. val1:env1:opt1). In the code that zaxxon suggested, the components val1 env1 opt1 were broken out and assigned to variables A, B and C. There would be no need to reference i, as there wasn't anything assigned to i anyway. You should be able to use A, B, and C as is.

From your original post, you also do a lot of extra work to only read a subset of lines from the input file. That isn't handled by zaxxon's example. Moving along the same lines as was offered, the following code will generate just the lines between START and STOP (inclusive) and assign them to the variables f1, f2, and f3 (A, B, C).

Code:
awk -F : -v last=${STOP:-0} -v first=${START:-0} '
    /^#/ { next; }
    NR >= first && (last == 0  || NR <= last) {
        print $1, $2, $3, $0
    }

' input-file | while read f1 f2 f3 i
do
        echo "first field: $f1"
        echo "second field $f2"
        echo "third field $f3"
        echo "whole record (i) $i"
done

This User Gave Thanks to agama For This Post:
# 7  
Old 01-14-2012
Quote:
Originally Posted by agama
Code:
awk -F : -v last=${STOP:-0} -v first=${START:-0} '
    /^#/ { next; }
    NR >= first && (last == 0  || NR <= last) {
        print $1, $2, $3, $0
    }
 
' input-file | while read f1 f2 f3 i
do
        echo "first field: $f1"
        echo "second field $f2"
        echo "third field $f3"
        echo "whole record (i) $i"
done

Thanks Agama. Having tried the code above, i am getting an EOF error. Can you please explain what the awk is doing and if the format of the code is correct? I am getting the following error
Quote:
unexpected EOF while looking for matching `"'
---------- Post updated at 06:48 PM ---------- Previous update was at 05:36 PM ----------

Hi Agama,

I am unfamiliar with the syntax of the belwo code that you provided. I am getting an error from the code. Can you please let me know if the format of the code is correct? Thanks in advance.

Code:
awk -F : -v last=${STOP:-0} -v first=${START:-0} '
    /^#/ { next; }
    NR >= first && (last == 0  || NR <= last) {
        print $1, $2, $3, $0
    }
 
' input-file | while read f1 f2 f3 i
do

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to avoid "Too many arguments" error, when passing a long String literal as input to a command?

Hi, I am using awk here. Inside an awk script, I have a variable which contains a very long XML data in string format (500kb). I want to pass this data (as argument) to curl command using system function. But getting Too many arguments error due to length of string data(payloadBlock). I... (4 Replies)
Discussion started by: cool.aquarian
4 Replies

2. Shell Programming and Scripting

Using here document when passing arguments

I have a script test.sh which reads various inputs from a user. #!/bin/ksh read x read y read z echo x: $x y: $y z: $z # read few more things again read a read b echo a: $a b: $b When i run this script as test.sh << EOF 1 2 EOF (3 Replies)
Discussion started by: ariesb2b
3 Replies

3. Shell Programming and Scripting

Passing arguments that contain space

hi All, i am trying to pass arguments that contain space , value will be stored in variables to be used further in script , i went thru previous posting , still its not clear to how to implement for my case. passing 3 args test.sh it is 'fun to work in unix' inside shell ... (3 Replies)
Discussion started by: gvkk
3 Replies

4. Shell Programming and Scripting

Passing arguments to csh

I have noticed this thing using csh when passing arguments Suppose I call a csh script using ../Scripts/plot-model.csh -vmod="npt02-z30.vmod" -R="0/80/0/30" -c="0/4.5" -aspr="1:10" Somehow the " get removed when doing $argv ending up with -vmod=npt02-z30.vmod... (0 Replies)
Discussion started by: kristinu
0 Replies

5. UNIX for Dummies Questions & Answers

Passing arguments

I need to pass arguments to a shell script.My batch is calling some java program. ################# x=$1 y=$2 java -classpath program ################### if first parameter and second parameter is null then java -classpath program if first parameter is not null and second parameter is... (3 Replies)
Discussion started by: mnjx
3 Replies

6. Shell Programming and Scripting

Passing arguments to the subshell

I have a shell script which is invoked by passing an argument. The outer shell script calls another subshell and I want the argument passed down to flow down to the subshell. E.g Invoking a shell ======>> abc_refresh.ksh NM Below is the content of abc_refresh.ksh Value1=$1... (7 Replies)
Discussion started by: Mihirjani
7 Replies

7. Shell Programming and Scripting

passing arguments

Hi I have a script to which I pass multiple arguments, for example lets say the script name is "abc". I run the script like ./abc def /file <directory location> In the above "def" is the first argument and "/file" is the second argument. I expect <directory location> that is passed after... (4 Replies)
Discussion started by: zmfcat1
4 Replies

8. Shell Programming and Scripting

Passing Arguments-Help

Hi, I have a script which adds the user credentials to an ldap server. Im passing the variables as below.. /path/my_script $uname $pwd $environ ${deposit} If i enter some special characters like ';' in $pwd, script returns an error which is set to display if the user enters... (5 Replies)
Discussion started by: Tuxidow
5 Replies

9. Shell Programming and Scripting

passing in arguments into a file using a loop

Hi, so Im a bit new to shell scripting and want to do the following but not sure how. Basically I have a file named "output" which contains misc text but inside the file I want to set up variables like $1 or some symbol. Anyways, in another file called "list" I have a list of items that I want to... (11 Replies)
Discussion started by: eltinator
11 Replies

10. UNIX for Dummies Questions & Answers

passing arguments

I'm trying to pass a filename, or all the files in the current directory to the ls command with a script. Unsuccessful so far, here are a few of my attempts: #!/bin/ksh read fname #if (( $# > 0 )); then $fname | ls -l #fi this produces a long listing of all the files in my current... (4 Replies)
Discussion started by: jpprial
4 Replies
Login or Register to Ask a Question