It is very possible that this isn't bulletproof

But I think it's quite solid
Code:
#!/bin/bash
# take the input file as argument 1
file=$1
# Set the seperator symbol to a new line
IFS="
"
# Some incrementors, $a is set to -1 to compensate for the first occurance of "zip"
a=-1
b=0
# loop throu the text file
for i in $(cat $file); do
# If we find "zip" then we create another element in our pseudo-multidimensional array
echo $i | grep "zip" &> /dev/null
if [ $? -eq 0 ]; then
((a++))
b=0
fi
# Add that line to the array
eval "array$a[$b]=\"$i\""
((b++))
done
# Start looping throu the multidimensional array
b=0
while [ $b -le $a ]; do
name="array$b"
# Get the length of the current "zip code array element"
len=$(eval "echo \${#$name[@]}")
# clear previous values, in case city is missing.
unset zip city
for i in $(seq 1 $len); do
((i--))
# Retrieve the value
value=$(eval "echo \${$name[$i]}")
# Check if the value is something to print, or if its informational
case $value in
# This will react to any combination of cases for the word "zip"
[Zz][Ii][Pp]*) zip=$(echo $value | cut -d':' -f2) ;;
# This will react to any combination of cases for the word "city"
[Cc][Ii][Tt][Yy]*) city=$(echo $value | cut -d':' -f2) ;;
# if none of the above is found it's time to echo the output
*) echo -e "$value\t$city\t$zip" ;;
esac
done
((b++))
done
Usage info follows,
Code:
Usage: ./script.sh file-containing-lines.txt
This script requires you to format the output as you described in your file, no more no less.
Code:
[email protected]:~> cat test.txt
zip:12345
city:some city
1234 main street
1235 main street
1242 main street
1245 main street
zip:2345
city:some city2
1257 main street
1259 main street
1260 main street
1266 main street
[email protected]:~> sh test.sh test.txt
1234 main street some city 12345
1235 main street some city 12345
1242 main street some city 12345
1245 main street some city 12345
1257 main street some city2 2345
1259 main street some city2 2345
1260 main street some city2 2345
1266 main street some city2 2345
ps. I'm somewhat very bored at work today ;P ds.
Best regards
Fredrik Eriksson