If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > shell script to concatenate lists together

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-05-07, 21:51
rfourn rfourn is offline
Registered User
 
Join Date: Jun 2007
Posts: 11
shell script to concatenate lists together

Below is a description of what im trying to achieve:

Write a shell script to concatenate lists together, and output the resulting list. Do not include any argument that is a sub-list of the entire list. (The script will clean the list of any redundant items.) You must preserve the original order of the list. Remember to account for the following situations:

# a : at the beginning of the list is the same as a . at the beginning of the "list" (:/bin is the same a .:/bin)

# a :: any where in the list is the same as :.: (/bin::/etc is the same as (/bin:.:/etc)

# a : at the end of the list is the same a :. ( /bin: is the same as /bin:. )

The input to the script will be colon or space separated lists and the output will be a single colon separated list with all redundant items removed.

Examples:

prompt> clean_list a a:b a:b:c :x: y:z
a:b:c:.:x:y:z

prompt>clean_list applerange:apple pear orange peach
applerangeeareach

The following code is where im at right now but i'm not quite getting the desired results. for example, when i try the first example above i get a result of a:b:c:x:y:z (not the a:b:c:.:x:y:z)

#!/bin/sh

for P in `echo $* | sed -e 's/^:/.:/' -e 's/::/:.:/' -e 's/:$/:./' -e 's/:/ /g'`

do
case $NP in

"") NP="$P"
;;

$P|$P:*|*:$P:*|*:$P) continue
;;

*) NP="$NP:$P"
;;

esac

done

echo $NP

Any suggestions are appreciated!!!!!
Reply With Quote
  #2 (permalink)  
Old 07-07-07, 04:11
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Why do want to do this - is this a real world problem or just a homework assignment?
Mike
Reply With Quote
  #3 (permalink)  
Old 07-09-07, 16:45
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
Replace all spaces by : in your sed command
Code:
for P in `echo $* | sed -e 's/ /:/g' -e 's/^:/.:/' -e 's/::/:.:/' -e 's/:$/:./' -e 's/:/ /g'`
You can group all the -e options :
Code:
for P in `echo $* | sed 's/ /!/g; s/^:/.:/; s/::/:.:/; s/:$/:./; s/:/ /g'`
__________________
Jean-Pierre.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On