Register
It is currently Mon Sep 01, 2014 5:31 pm

filling a sequence


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Wed Nov 04, 2009 3:32 am   

Joined: Thu Oct 09, 2008 3:26 am
Posts: 15
Location: Columbus, OH
/t for tab
file:

zip:12345
city:some city (might have spaces)
1234/tmain st
1235/tmain st
1242/tmain st
1245/tmain st
zip:2345
city:some city (might be same, might not)
1257/tmain st
1259/tmain st
1260/tmain st
1266/tmain st

I want to fill the sequence of streets with the above zip/city info and then reset to the new zip/city sequence as encountered so I wind up with lines like:

1234/tmain st/tcity/tzip

thx to all for help and understanding


Top
 Profile  
 PostPosted: Wed Nov 04, 2009 4:31 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
It is very possible that this isn't bulletproof :P 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:
sajko@hanna:~> 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
sajko@hanna:~> 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


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP