Bhavesh Ghodasara Bhavesh Ghodasara - 20 days ago 5
Bash Question

read files from unix in batch of certain length

I have one file named as p.txt.
It contains following values:

201601
201602
201603
201604
201605
201606
201607
201608
201609
201610


I want to read this records in batch of 3. i.e.
one variable will have following three values

201601
201602
201603


in first iteration. in second iteration it will have next three lines

201604
201605
201606


If number is not fully divided by 3 then iteration will be divider +1.

how is it possible in unix?

What I have tried so far:

PERD=`cat P.txt`

for perd in `cat PERD_ID.txt`;
do
bteq << EOF
.logon ${conn_string};

/* Database*/
DATABASE $ET;

/* Update NULL LOCL_SEGMNT3_IDNs*/
INSERT INTO T
SELECT *
FROM A
WHERE PERIOD IN ($PERD);

.if errorcode != 0 then .exit 5.1;

.LOGOFF
.EXIT
EOF

done


Current code reads every line and executes insert in DB. I want to have these query for 3 period for better performance.

/* Update NULL LOCL_SEGMNT3_IDNs*/
INSERT INTO T
SELECT *
FROM A
WHERE PERIOD IN (201601,201602,201603);

Answer

Try to start with this simple code. Modify it at your will:

cat p.txt | while read a; 
do
   read b;
   read c;
   echo $a $b $c;
done

The variables a, b, c have the 3 values.

Comments