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
# 8  
Old 01-14-2012
I cut/pasted the code in your post and it seems fine to me; I'm not getting an error. Can you past in your whole script, there is likely a typo somewhere in it.

An explanation:

The STOP and START shell variables allow you to define a line range from the input file. If not defined, the whole file is processed. If only START is defined, all lines before START are skipped. If STOP is defined, all lines after that line are skipped.

Code:
awk -F : -v last=${STOP:-0} -v first=${START:-0} '
    /^#/ { next; }   # if a line in the input starts with a hash, it is skipped

    # this section prints the first three fields and the whole line 
    # the $0 prints the whole line with the separators and is needed only to 
    # show that as $1 in the shell script echo
    NR >= first && (last == 0  || NR <= last) {
        print $1, $2, $3, $0
    }
' input-file

This User Gave Thanks to agama For This Post:
# 9  
Old 01-14-2012
Quote:
Originally Posted by agama
I cut/pasted the code in your post and it seems fine to me; I'm not getting an error. Can you past in your whole script, there is likely a typo somewhere in it.

An explanation:

The STOP and START shell variables allow you to define a line range from the input file. If not defined, the whole file is processed. If only START is defined, all lines before START are skipped. If STOP is defined, all lines after that line are skipped.

Code:
awk -F : -v last=${STOP:-0} -v first=${START:-0} '
    /^#/ { next; }   # if a line in the input starts with a hash, it is skipped
 
    # this section prints the first three fields and the whole line 
    # the $0 prints the whole line with the separators and is needed only to 
    # show that as $1 in the shell script echo
    NR >= first && (last == 0  || NR <= last) {
        print $1, $2, $3, $0
    }
' input-file

Thanks a mil for the explanation. It's gonna be pretty handy.
I use the same code that you've posted and get the EOF error. My frustrations not withstanding, i have gone ahead and written the results of the for loop into a tempfile and then proceeded to read the code from below(i have the working section commented out). I'd still like to understand why the below code throws up an error. I am only new to shell script and i'd like to understand whats going on so as not to repeat the errors. Thank you for your time
Code:
for i in $( cat param.txt | tail +$START | head -$STOP | grep -v "^#" | grep ':' )
                             do
                                   #echo $i
                                  # printf "%s\n" "$i" >> .param
                                   #printf "Press [enter] key to continue. . ."
                                   #read enterKey
                                   OLDIFS=$IFS
                                   IFS=":"
                                awk -F : -v last=${RESPONSESTOP:-0} -v first=${RESPONSE:-0} '
                                   /^#/ { next; }
                                   NR >= first && (last == 0  || NR <= last) {
                                    print $1, $2, $3, $0
                                    }
                                   ' param.txt | while read x y z i
                                   do
                                        echo "first field $x"
                                        echo "second field $y"
                                        echo "third field $z"
                                        echo "whole record (i) $i
                                   done
                                   #while read x y z
                                   #do
                                   #if [ "$z" == "GOD" ] ; then
                                   #       function1 $x $y
                                   #       function2 $y
                                   #       function3 $y
                                   #fi
                                   #if [ "$z" == "DEVIL" ] ; then
                                   #       function4
                                #   fi
                                #   done < .param
                                   IFS=$OLDIFS
                             done
                      #  echo "`cat .param`"
                        #rm -rf .param

# 10  
Old 01-14-2012
You had a missing quote on one of the echo statements.

I think this is all that you need. The cat.... statement isn't necessary. Awk reads the param.txt file and thus you don't need to cat it through head/tail to get it into the awk.

Code:
awk -F : -v last=${RESPONSESTOP:-0} -v first=${RESPONSE:-0} '
   /^#/ { next; } 

   NR >= first && (last == 0  || NR <= last) { 
        print $1, $2, $3, $0
    }
   ' param.txt | while read x y z i 
   do
        echo "first field $x"
        echo "second field $y"
        echo "third field $z"
        echo "whole record (i) $i"   # <missing close quote was here

        if [[ $z == "GOD" ]]
        then
                function1 $x $y
                function2 $y
                function3 $y
        fi
        if [[ $z == "DEVIL" ]]
        then
            function4
        fi
   done

# 11  
Old 01-14-2012
Or alternatively, use plain shell without pipes, e.g.
Code:
START=2; STOP=7
linenr=0
while IFS=: read x y z ; do
  linenr=$((linenr+1));
  [ $linenr -ge $START ] || continue
  [ $linenr -le $STOP  ] || break
  case $x in 
    \#*) continue
  esac
  echo " Do your stufff with $x $y and $z at $linenr" 
done < infile

Code:
 Do your stufff with RED value2 and Y at 2
 Do your stufff with BLUE value4 and Y at 4
 Do your stufff with RED value5 and Y at 5
 Do your stufff with RED value6 and Y at 6
 Do your stufff with BLUE value7 and Y at 7

This User Gave Thanks to Scrutinizer For This Post:
# 12  
Old 01-15-2012
Quote:
Originally Posted by agama
You had a missing quote on one of the echo statements.

I think this is all that you need. The cat.... statement isn't necessary. Awk reads the param.txt file and thus you don't need to cat it through head/tail to get it into the awk.

Code:
awk -F : -v last=${RESPONSESTOP:-0} -v first=${RESPONSE:-0} '
   /^#/ { next; } 
 
   NR >= first && (last == 0  || NR <= last) { 
        print $1, $2, $3, $0
    }
   ' param.txt | while read x y z i 
   do
        echo "first field $x"
        echo "second field $y"
        echo "third field $z"
        echo "whole record (i) $i"   # <missing close quote was here
 
        if [[ $z == "GOD" ]]
        then
                function1 $x $y
                function2 $y
                function3 $y
        fi
        if [[ $z == "DEVIL" ]]
        then
            function4
        fi
   done

Hi agama,

I have rectified the missing quote. when i run the script, i get the following error
Quote:
awk: syntax error near line 1
awk: bailing out near line 1
---------- Post updated at 07:40 AM ---------- Previous update was at 07:26 AM ----------

Quote:
Originally Posted by Scrutinizer
Or alternatively, use plain shell without pipes, e.g.
Code:
START=2; STOP=7
linenr=0
while IFS=: read x y z ; do
  linenr=$((linenr+1));
  [ $linenr -ge $START ] || continue
  [ $linenr -le $STOP  ] || break
  case $x in 
    \#*) continue
  esac
  echo " Do your stufff with $x $y and $z at $linenr" 
done < infile

Code:
 Do your stufff with RED value2 and Y at 2
 Do your stufff with BLUE value4 and Y at 4
 Do your stufff with RED value5 and Y at 5
 Do your stufff with RED value6 and Y at 6
 Do your stufff with BLUE value7 and Y at 7

In the above code, if i change the values of start and stop to 7 and 18 respectively and try to execute
Code:
echo " Do your stufff with $x $y and $z at $linenr"

i am not getting any output. I can only print the values between 1 and 6. If i try to print from 6 onwards, there is no output (I have over 50 files btw).

Last edited by goddevil; 01-15-2012 at 05:54 AM..
# 13  
Old 01-15-2012
Hi, I just tried it and it works here. Could you post a representative sample of say 25 lines of the file you tested with? What shell/OS are you using?
# 14  
Old 01-15-2012
Quote:
Originally Posted by Scrutinizer
Hi, I just tried it and it works here. Could you post a representative sample of say 25 lines of the file you tested with? What shell/OS are you using?
I use a bash shell in Solaris.

I am using a file with the format given below. If i choose any value from 1-5 as start and end at say 15, your logic works. But if i choose say 7-13 , then the
Quote:
echo " Do your stufff with $x $y and $z at $linenr"
- dows not happen.

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
BLUE:value12:Y
BLUE:value13:Y
RED:value14:Y
BLUE:value15:Y
RED:value16:y

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