| Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free! |
concatenate strings
|
Original Message
|
Name: tspj88
Date: April 14, 2004 at 10:20:36 Pacific
Subject: concatenate strings OS: unix CPU/Ram: 512
|
Comment: How can I concatenate following strings into simpified patterns: (1) from dir1 dir1:dir2 dir1:dir2:dir3 :dir4: changed into dir1:dir2:dir3:dir4 (2) from /bin:/usr/bin /usr/bin:/bin /usr/etc:/etc: changed into /bin:/usr/bin:/usr/etc:/etc: (3) from dir1:dir3:dir1:dir2:dir3 dir2 changed into dir1:dir3:dir2 Any comment for help.
Report Offensive Message For Removal
|
|
Response Number 1
|
Name: Wolfbone
Date: April 19, 2004 at 21:39:48 Pacific
|
Reply: (edit)Bash one-liner to read your input string and echo the transformed version: read string ; declare -a a=(${string//:/ }) ; i=0 ; while [[ ${a[$i]} != "" ]] ; do [[ ${str/ ${a[$i]}/} = $str ]] && str=$str" ${a[i]}" ; ((i++)) ; done ; str=${str// /:} ; echo ${str#:} ; unset a i str string Works okay assuming you didn't want the trailing ':' in (2).
Report Offensive Follow Up For Removal
|

Post Locked
This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
Go to Unix Forum Home