1827474 Members
1754 Online
109965 Solutions
New Discussion

posix shell script array

 
Wiboon Tanakitpaisal_1
Occasional Advisor

posix shell script array

Anybody could be help ?

if I have text file as below

---------------textfile.txt-----------------
messages line 1
messages line 2
messages line 3
messages line 4
messages line 5
messages line 6
messages line 7
messages line 8
messages line 9
messages line 10
------------------------------------------------

I want to cut every 5 lines and send to filter
and then go to next 5 lines and send to filter again until end of file with out using temp file
(because I want to tuning performance , my old script using temp file and sed command to cut line 1 to 5
from temp file and send to filter)

I got input from program just like command as below

cat textfile.txt|tee

I want to use output from this command just like

cat textfile.txt|tee
program ....
...
...

How can I write shell script to handle this process
I can not use "while read -r xx" command because this command get input from text file but I want to insert this
code to my program.

Can I use array ?

Many thanks.

6 REPLIES 6
Jean-Louis Phelix
Honored Contributor

Re: posix shell script array

hi,

I don't really understand what you need. Could you please be more explicit on your input and output and how you want to use it or perhaps even post your old script ?

Regards.
It works for me (© Bill McNAMARA ...)
Jarle Bjorgeengen
Trusted Contributor

Re: posix shell script array

If you want performance for text -prosessing, filtering and re-formatting, use perl-scripts.

Rgds Jarle
Wiboon Tanakitpaisal_1
Occasional Advisor

Re: posix shell script array

My old script as below , this script use $file as temp file,if I can do this job without temp file it will better.

--------

.
.
.

/usr/bin/tee | sed 's/^C//' | pr -t -l${_length} > $file

reset_printer

cat /usr/lib/asx/thai/pre15cpi.fnt
cat /usr/lib/asx/thai/thaib410n.fnt
echo "^[(1X\c"
esc1="^[\&\l5C"
esc2="^[\&\l48D"

FileLen=`cat $file|wc -l`
nLoop=`expr $FileLen / $_length`
LastLen=`expr $FileLen % $_length`
LoopCount=1
ARG="-pr=/usr/lib/asx/thai/prn/dumb -i=/usr/lib/asx/thai/code/sic-tis -o=/usr/lib/asx/thai/code/tis-sic -rm400 -cb2 -lpg44"
while [ ${LoopCount} -le ${nLoop} ]
do
cat $PCL_FILE # Print Pre-Printed Form

case ${_job_name} in
CSA*) echo "^[&a3R^[&a1C\c"
echo "\033&a5L\c" # set left margin
;;
esac

StartLine=`expr \( $LoopCount - 1 \) \* $_length + 1`
EndLine=`expr $LoopCount \* $_length`

sed -n "$StartLine,$EndLine p" $file |
/usr/lbin/thfil $ARG|sed 'G
s/\n/'$esc1'/
n
n
G
s/\n/'$esc2'/'
LoopCount=`expr $LoopCount + 1`
done

if [ $LastLen -gt 0 ]
then
cat $PCL_FILE # Print Pre-Printed Form

case ${_job_name} in
CSA*) echo "^[&a3R^[&a1C\c"
echo "\033&a5L\c" # set left margin
;;
esac

StartLine=`expr $nLoop \* $_length + 1`
EndLine=`expr $FileLen`
sed -n "$StartLine,$EndLine p" $file |
/usr/lbin/thfil $ARG|sed 'G
s/\n/'$esc1'/
n
n
G
s/\n/'$esc2'/'
fi

font_reset

rm -f $file
Robin Wakefield
Honored Contributor

Re: posix shell script array

Hi,

If I read this correctly, and without using perl, you maybe want something like:

while :;do
i=0
while [ $i -ne 5 ]; do
read line || break
a[$i]=$line
let i=i+1
done
echo ${a[*]}
sleep 1
test "$line" || exit
unset a
i=0
done

then you'd run it by typing:

cat textfile.txt | scriptname

the script sleeps for one sec. after reading 5 lines, that's where you could put your filter.

rgds, Robin
Jean-Louis Phelix
Honored Contributor

Re: posix shell script array

Hi,

I'm not sure that it would really be an enhancement but you can also use read with -p option.

just add "|&" at the end of first sed and use read -p to get input later in the script :

/usr/bin/tee | sed 's/^C//' | pr -t -l${_length} |&
...

while read -p xxx

A simplier example of this usage would be :

#!/usr/bin/sh
ll |&
while read -p file
do
echo "***$file***"
done

You will see that each line of ll input will be read as needed.

Regards.
It works for me (© Bill McNAMARA ...)
Wiboon Tanakitpaisal_1
Occasional Advisor

Re: posix shell script array

I got new problem I can not use read command because read command read line from std input (console) but this script call from another program and no tty for this process, I think awk may be work . Anybody can write this script with array in awk ? Could you please show me the example of script.