I would like to push some lines in files using bash here how my file look like
589097 1234567802 32 0 0 25 4 4935232014070914070958 0 2 0 0 0 0.00000000000341392324000000000341395276
589097 1234567802 32 0 0 25 4 4935232014070914070958 0 2 0 0 0 0.00000000000341392324000000000341395276
589097 12345678901001 32 0 0 10 4 4935232014070914070958 0 10 0 0 0 0.00000000000341392324000000000341395276
547233 12345678901001 34 0 0 10 4 4935232014070914070958 0 10 0 0 0 0.00000000000001074106000000000003392014
358474 12345678901001 32 0 0 5 4 4935232014070914070958 0 10 0 0 0 0.00000000000204811406000000000204817557
547233 44556601 34 0 0 2023 4935232014071314071358 0 3 0 0 0 0.00000000000001074106000000000003392014
547233 44556602 34 0 0 2023 4935232014071314071358 0 3 0 0 0 0.00000000000001074106000000000003392014
547233 44556603 34 0 0 2023 4935232014071314071358 0 3 0 0 0 0.00000000000001074106000000000003392014
547233 44556604 34 0 0 2023 4935232014071314071358 0 3 0 0 0 0.00000000000001074106000000000003392014
547233 44556605 34 0 0 2023 4935232014071314071358 0 3 0 0 0 0.00000000000001074106000000000003392014
547233 44556606 34 0 0 2023 4935232014071314071358 0 3 0 0 0 0.00000000000001074106000000000003392014
547233 44556607 34 0 0 2023 4935232014071314071358 0 3 0 0 0 0.00000000000001074106000000000003392014
547233 44556608 34 0 0 2023 4935232014071314071358 0 3 0 0 0 0.00000000000001074106000000000003392014
547233 44556609 34 0 0 2023 4935232014071314071358 0 3 0 0 0 0.00000000000001074106000000000003392014
547233 44556610 34 0 0 2023 4935232014071314071358 0 3 0 0 0 0.00000000000001074106000000000003392014
626967 1234567803 32 0 0 22 4 4935232014071214071258 0 6 0 0 0 0.00000000000374291378000000000374291403
123456 1234567804 99 0 0 20 4 4935202014071414071458 0 6 0 0 0 0.00000000000123456789000000000987654321
698218 12345678901002 44 0 0 8 4 4935202014071414071458 0 16 0 0 0 0.00000000000374291378000000000374291403
370958 12345678901002 32 0 0 10 4 4935202014071414071458 0 16 0 0 0 0.00000000000404240990000000000404244066
123456 12345678901002 10 0 0 2 4 4935202014071414071458 0 16 0 0 0 0.00000000000123456780000000000123456780
528034 1234567805 30 0 0 20 4 4935232014071514071558 0 4 0 0 0 0.00000000000378397276000000000378404939
686200 12345678901003 36 0 0 1 8 4935232014071514071558 0 2 0 0 0 0.00000000000365718954000000000365727049
368530 12345678901004 34 0 0 10 4 4935232014071614071658 0 13 0 0 0 0.00000000000274290046000000000274294645
368530 12345678901004 36 0 0 10 4 4935232014071614071658 0 13 0 0 0 0.00000000000274290046000000000274294647
854809 12345678901005 32 0 0 10 4 4935232014071614071658 0 13 0 0 0 0.00000000000202369548000000000202378103
854809 12345678901005 34 0 0 10 4 4935232014071614071658 0 13 0 0 0 0.00000000000202369548000000000202378105
368530 1234567806 38 0 0 22 4 4935232014071614071658 0 7 0 0 0 0.00000000000274290046000000000274294649
368530 1234567807 40 0 0 22 4 4935232014071614071658 0 7 0 0 0 0.00000000000274290046000000000274294651
854809 1234567808 36 0 0 22 4 4935232014071614071658 0 7 0 0 0 0.00000000000202369548000000000202378107
854809 1234567809 38 0 0 22 4 4935232014071614071658 0 7 0 0 0 0.0000000000020236954800000000020237810
I have some rules: The second column is My NumCarton. I would have to cut my file with this Number. Here my code:
#!/bin/bash
# function which extract either Carton Number
split()
{
echo "$1" |cut -f$2 -d/
}
# Delete previous file
rm -f ?
# Processing Data
fich=1
nb_lig=0
#for info in "${data[@]}"
cat inputter.txt| while read info
do
# If the Carton Number has change
carton=$(split "$info" 2)
if test "$carton" != "$same_carton"
then
# We have a new carton Number
same_carton="$carton"
# If we have previous lines in the buffer we wrote down in the file
if test "${#buffer[*]}" -gt 0
then
for lig in "${buffer[@]}"
do
echo "$lig"
done >>"$fich"
fi
# we retain lines from the buffer
nb_lig=$(expr $nb_lig + ${#buffer[*]})
# we initiate the buffer()
buffer=()
fi
# we add lines to the buffer table
buffer[${#buffer[*]}]="$(split "$info" 1) $carton"
# if the data is beyond 4 lines
if test $(expr ${#buffer[*]} + $nb_lig) -gt 4
then
# we have a new file
fich=$(expr $fich + 1)
nb_lig=0
fi
done
Assume that my file is in inputter.txt. My problem is in the split function. with this function, I want to get the NumCarton so I compare to the next one in the next line.
But some of my NumCarton are 14 digits long and the others not.
Maybe an example may be helpful. See line 1 and line 2 may be in the same file. we could add 2 more lines but this will cut a Pack. the NumCarton '12345678901001' has 3 items. So we create an other file.This new file should contain those 3 items + the line which contain '44556601'. To sum up one file could contain one or more Pack. But one file couldn't contain more than 4 lines
Here a small part of my file:
589097 1234567802 32 0 0 25 4 4935232014070914070958 0 2 0 0 0 0.00000000000341392324000000000341395276
589097 1234567802 32 0 0 25 4 4935232014070914070958 0 2 0 0 0 0.00000000000341392324000000000341395276
589097 12345678901001 32 0 0 10 4 4935232014070914070958 0 10 0 0 0 0.00000000000341392324000000000341395276
547233 12345678901001 34 0 0 10 4 4935232014070914070958 0 10 0 0 0 0.00000000000001074106000000000003392014
358474 12345678901001 32 0 0 5 4 4935232014070914070958 0 10 0 0 0 0.00000000000204811406000000000204817557
See according to what I say above, The 2 lines would go in the first file The 3lines + 1line would go in the second file.