1

I have the following problem: Script

#!/usr/bin/ksh

for unitcaseno in `cat /pims/nigel/UNLOAD-DB/xaa`
do
        echo "String = $unitcaseno"
        unitno=`echo $unitcaseno | cut -d'|' -f1`
        caseno=`echo $unitcaseno | cut -d'|' -f2`
        echo "Unitno = $unitno:Caseno = $caseno"
break
done

The file is very large so I am breaking after the first record.

Sample file:
  349702|            1|
  349702|            1|
  349702|            1|
 1133247|            6|
 1133247|            6|
 12708  |            1|
 12708  |            1|
 299466 |            2|
 299466 |            2|
 299466 |            2|
 299466 |            2|
 501877 |            1|
 501877 |            1|
 55871  |            9|
 55871  |            9|
 64239  |            7|
 64239  |            7|

Output

String = 349702|
Unitno = 349702:Caseno =

My problem is that it is not printing out the second field in either output.

Archemar
  • 31,554
Nigel B
  • 11
  • 1
  • 1
    http://unix.stackexchange.com/questions/169716/why-is-using-a-shell-loop-to-process-text-considered-bad-practice – Sundeep Jul 21 '16 at 08:54

1 Answers1

4

The problem you're having is with the

for unitcaseno in `cat /pims/nigel/UNLOAD-DB/xaa`

structure. This will split the file into words, not lines. This is why you see String = 349702 in the output; you're only seeing the first word and not the whole line.

Instead consider a loop similar to

while read unitcaseno
do
  ....
done < /pims/nigel/UNLOAD-DB/xaa

Now you're processing a line at a time

String = 349702|            1|
Unitno = 349702:Caseno =  1

Note you have other problems; eg extra whitespace before the Caseno. But this should get you to the next step!