Discussion Locked

Unix script to select information.

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I need help to create file for reading and process information in a file:

File Content:

962113897 B 10/1/04 12/31/04 100%
962113897 O 10/1/04 12/31/04 100%
962115794 B 10/1/04 12/31/04 100%
962115794 O 10/1/04 12/31/04 100%

Fisrt Case:
If the second field contain a "B" I need to repeat the all the registry in the following order and adding a new field containing the following letters G,K,E,O,W,T,U,V on the second column and redirect the result to a file.

Example:

962113897|G|B|100%|10/1/04|12/31/04|
962113897|K|B|100%|10/1/04|12/31/04|
962113897|E|B|100%|10/1/04|12/31/04|
962113897|O|B|100%|10/1/04|12/31/04|
962113897|W|B|100%|10/1/04|12/31/04|
962113897|T|B|100%|10/1/04|12/31/04|
962113897|U|B|100%|10/1/04|12/31/04|
962113897|V|B|100%|10/1/04|12/31/04|

Second Case:

If the second field contain a "O" I need to repeat the all the registry in the following order and adding a new field containing the following letters B,A,D,P,H, on the second column and redirect the result to a file.

Example:

962113897|B|O|100%|10/1/04|12/31/04|
962113897|A|O|100%|10/1/04|12/31/04|
962113897|D|O|100%|10/1/04|12/31/04|
962113897|P|O|100%|10/1/04|12/31/04|
962113897|H|O|100%|10/1/04|12/31/04|

The total result must be like this:

962113897|G|B|100%|10/1/04|12/31/04|
962113897|K|B|100%|10/1/04|12/31/04|
962113897|E|B|100%|10/1/04|12/31/04|
962113897|O|B|100%|10/1/04|12/31/04|
962113897|W|B|100%|10/1/04|12/31/04|
962113897|T|B|100%|10/1/04|12/31/04|
962113897|U|B|100%|10/1/04|12/31/04|
962113897|V|B|100%|10/1/04|12/31/04|
962113897|B|O|100%|10/1/04|12/31/04|
962113897|A|O|100%|10/1/04|12/31/04|
962113897|D|O|100%|10/1/04|12/31/04|
962113897|P|O|100%|10/1/04|12/31/04|
962113897|H|O|100%|10/1/04|12/31/04|

Thank You for your help !

Best Regards !

3 replies

by gary_w

There's probably a trickier way to do it, but this example should be easy to follow. Create process.sh below and call lihe this:
process.sh . Output will go to .out. Note that there should be error handling added, etc.

Gary

=== process.sh ===
outfile=$1.out
> $outfile
cat $1 | while read line
do
set $line
if [ $2 = 'B' ]
then for i in G K E O W T U V
do
echo "$1|$i|$2|$5|$3|$4|" >> $outfile
done
else if [ $2 = 'O' ]
then for i in B A D P H
do
echo "$1|$i|$2|$5|$3|$4|" >> $outfile
done
fi
fi
done
echo "Output in $outfile"
==== End process.sh ====

This discussion is closed to new replies.