KSH - need to write a script to abbreviate a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting KSH - need to write a script to abbreviate a string
# 1  
Old 06-30-2011
KSH - need to write a script to abbreviate a string


Dear Members,

I have to write a UNIX script (KSH) to create a 6 letter abbreviation from a given string. The string will have alphabets and underscores only.

e.g. abc_pst_xyz is our string and I have to create an abbreviation which will look like 'abpsxy' or 'abcpyz' etc.

Now comes the tricky part. I have to make sure that out of the given set of strings no two strings have the same abbreviation.

Basically, I need to search a particular directory and see if such an abbreviation already exists. If it does then, I have to recreate a new set of 6 letter abbreviation from the string.

Please help me with this script. Also, help me understand the logic to generate or select random characters from a string.

Thank you very much in advance.
# 2  
Old 06-30-2011
The following script generate all abbrevations for a string :
Code:
$ cat gen_abbrev.ksh
#!/usr/bin/ksh

echo "$1" | \
nawk -v Abbrev_Len=${2:-6} '

function SplitString(str       ,i ,slen) {
    slen = length(str);
    for (i=1; i<=slen; i++)
        string_array[i] = substr(str, i, 1);
    string_array[0] = slen;
    return slen
}

function buildAbbrev(len, abbrev   ,i ,lstr, char) {
    lstr = string_array[0];
#print "buildAbbrev len=" len, "abbrev=[" abbrev "]", "lstr=" lstr;
    if (len > 0 ) {
       for (i=1; i<=lstr; i++) {
           char = string_array[i];
           if (char) {
               string_array[i] = ""
               buildAbbrev(len-1, abbrev char);
               string_array[i] = char;
           }
       }
    } else {
        print abbrev;
    }
    return
}

function buildAllAbbrevs(str, labbrev    ,astr) {
    string_array[0] = 0;
    if (SplitString(str) > labbrev) {
        buildAbbrev(labbrev, "");
    } else print "ERR";
}

{
    buildAllAbbrevs($0, Abbrev_Len);
    exit;
}
'

Example:
Code:
$ ./gen_abbrev.ksh abcd 3
abc
abd
acb
acd
adb
adc
bac
bad
bca
bcd
bda
bdc
cab
cad
cba
cbd
cda
cdb
dab
dac
dba
dbc
dca
dcb
$

You can use it like that :
Code:
gen_abbrev.ksh ${string} 6 |
while read abbreviation
do
     if valid_abbreviation 
     then
         use_abbrevation
         break
     fi
done

Jean-Pierre.
This User Gave Thanks to aigles For This Post:
# 3  
Old 06-30-2011
Error while execution


Thanks for the script aigles.

However, when I am trying to execute the script with the example you have provided, I am getting the following error:

Code:
gen_abbrev.ksh abcd 3

gen_abbrev.ksh[4]: nawk:  not found

Please help me resolve the error.


---------- Post updated at 03:33 PM ---------- Previous update was at 03:31 PM ----------

Thanks for the script aigles.

However, when I am trying to execute the script with the example you have provided, I am getting the following error:


Code:
gen_abbrev.ksh abcd 3

gen_abbrev.ksh[4]: nawk:  not found

Please help me resolve the error.
# 4  
Old 06-30-2011
replace nawk by awk

Jean-Pierre.
# 5  
Old 06-30-2011
Additional Info Required


Thanks. Replacing nawk by awk worked perfectly.
Now my other question is:

In the example you have stated, we get all the possible set of combinations of 3 letter string from 'abcd'.
But if I replace 'abcd' with 'abc_d' then I get set of string which have '_' underscore as well. However, I want that whatever be the parent string (including '_"), the generated abbreviation should not have an underscore.

How do I eliminate the occurrence of underscore.

Also, I understood the functions that you have created but what does the following line do:

Code:
awk -v Abbrev_Len=${2:-6}

Can you please provide me a link where I can get information about awk and how to use it in UNIX scripts?

Thank you very much!
# 6  
Old 06-30-2011
Quote:
Originally Posted by Yoodit
awk -v Abbrev_Len=${2:-6}
Sets the variable Abbrev_Len inside awk to the value of $2 in the shell. If $2 is absent, assume 6.
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 06-30-2011
Replace the SplitString function :
Code:
function SplitString(str       ,i ,j ,char ,slen ) {
    slen = length(str);
    for (i=1; i<=slen; i++)
        char = substr(str, i, 1);
        if (char ~ /[0-9a-zA-A]/)
            string_array[j++] = char;
    string_array[0] = j;
    return slen
}

Jean-Pierre.
This User Gave Thanks to aigles For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Script (ksh) - SQLPlus query filter using a string variable

Using ksh, I am using SQLPlus to execute a query with a filter using a string variable. REPO_DB=DEV1 FOLDER_NM='U_nmalencia' FOLDER_CHECK=$(sqlplus -s /nolog <<EOF CONNECT user/pswd_select@${REPO_DB} set echo off heading off feedback off select subj_name from subject where... (5 Replies)
Discussion started by: nkm0brm
5 Replies

2. Shell Programming and Scripting

Need to search a particular String form a file a write to another file using perl script

I have file which contains a huge amount of data. I need to search the pattern Message id. When that pattern is matched I need to get abcdeff0-1g6g-91g3-1z2z-2mm605m90000 to another file. Kindly provide your input. File is like below Jan 11 04:05:10 linux100 |NOTICE... (2 Replies)
Discussion started by: Raysf
2 Replies

3. Shell Programming and Scripting

grep exact string from files and write to filename when string present in file

I am attempting to grep an exact string from a series of files within a directory and append that output to the filename when it is present in the file. I've been after this all day with no luck. Thanks for your help in advance :wall:. (4 Replies)
Discussion started by: JC_1
4 Replies

4. Shell Programming and Scripting

String manipulation using ksh script

Hi, I need to convert string "(joe.smith" into "joe_smith" i.e. I need to remove the leading opening brace '(' and replace the dot '.' with an under score '_' can anyone suggest a one liner ksh script or unix command for this please (3 Replies)
Discussion started by: sdj
3 Replies

5. Shell Programming and Scripting

Grep a string and write a value to next line of found string

Hi, I have two variables x and y. i need to find a particular string in a file, a workflow name and then insert the values of x and y into the next lines of the workflow name. basically it is like as below wf_xxxxxx $$a= $$b= $$c= figo $$d=bentley i need to grep the 'wf_xxxx' and then... (6 Replies)
Discussion started by: angel12345
6 Replies

6. Shell Programming and Scripting

How do I write a ksh script that will check if all ftp files are received?

I am trying to code a ksh script that will check to see if all 26 incoming ftp files have been received before proceeding to the next function, which is to rename each file. Here is the pseudo-code of what I am trying to do: <<STEP_1>> IF all ALS files have been transmitted then... (2 Replies)
Discussion started by: doug145
2 Replies

7. Shell Programming and Scripting

String format in KSH script

Hello, I am attempting to format the output of my unix ksh script. Currently looks like ... FTP TO HR : Down FTP FROM HR 4011a : Up FTP FROM HR 4019a : Down FTP FROM HR : Down Would like the status to be aligned as follows: ... (4 Replies)
Discussion started by: dvella
4 Replies

8. Shell Programming and Scripting

Can ls in ksh write to output?

I have a ksh script written by someone... now i am trying to modify it to my requirement.... Its executed like this Read.ksh load ${CUR_MAINT_DATE} if then filnam="Load_CLM" else filnam="${1}" fi if then CUR_MAINT_DATE="${2}" fi if then ls... (1 Reply)
Discussion started by: bhagya2340
1 Replies

9. AIX

Problem in ksh script ( String comparison )

hi , i am trying to compre two strings if ] or if ] when the length of var1 is small (around 300-400 char ) it works fine but when it is large (around 900-1000 chars) it fails is there any limitations for this type of comparison ??? (1 Reply)
Discussion started by: amarnath
1 Replies

10. Shell Programming and Scripting

Problem in ksh script ( String comparison )

hi , i am trying to compre two strings if ] or if ] when the length of var1 is small (around 300-400 char ) it works fine but when it is large (around 900-1000 chars) it fails is there any limitations for this type of comparison ??? (3 Replies)
Discussion started by: amarnath
3 Replies
Login or Register to Ask a Question