Query regarding string Manipulation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Query regarding string Manipulation
# 1  
Old 08-19-2008
Query regarding string Manipulation

Dear Experts,
I have to write a shell script in which i get one string of numbers (e.g 1234567890) from user & i have to chage it to (2143658709). Please suggest how can i achieve the same.

Rule is:
In case the number of digits is even then translation should be:

123456 -> 214365

In case it is odd then translation should be:

12345 -> 2143F5 (One F before last character).

Thanks,
# 2  
Old 08-19-2008
Is this a homework question? Assuming that it's not, what is the real world problem of your question? What are you working on?

Regards
# 3  
Old 08-19-2008
It looks like it's just transposing two characters at a time of the number string, if it's even. If it's odd, just adding a "F" to the end of the number and then doing the transposing. Here's a solution in perl, with a fair amount of comments. Smilie

Code:
#!/usr/bin/perl -w
use strict;

# transpose two characters at a time of a given number, with a twist

#- if the character count is even, just do the simple transposing
# 1234567890 = 2143658709

#- if the character count is odd, do the transposing, and add an "F" before the last number 
# 123456789 = 21436587F9

my $number = $ARGV[0];
my $newnumber;
my $i = 1;
my $c = 0; #counter to keep track of character placement for substr
my $l1 = length($number); #length of string, used to determine odd or even

if ($l1%2 == 1) 
{
    # $number is odd
    # Add "F" to end of number - the easiest way to insert that F in the proper place
    $number .= "F";

    # now that we have added a character, get the new length of the number string
    my $l1 = length($number);
    
    # $times is how many times we need to cycle through grabbing substrings.  we're transposing 2 numbers at a time, so divide the number by 2 (or multiply .5)
    my $times = ($l1 * .5);

    for my $i ( 1 .. $times) 
    { 
        # grab 2 letters at a time, starting with 0 -- $c was defined above as 0
        my $substring = substr($number,$c,2);

        # transpose the letters using perl's builtin reverse()
        my $transposed = reverse($substring);

        # build the new number by appending the newly transposed letters to $newnumber
        $newnumber .= $transposed;

        # increase the offset counter by 2 so on the next interation we grab the next two letters.
        $c = ($c+2);
    }
} else 
{
    # $number is even
    my $l1 = length($number);
    my $times = ($l1 * .5);
    for my $i ( 1 .. $times) 
    { 
        my $substring = substr($number,$c,2);
        my $transposed = reverse($substring);
        $newnumber .= $transposed;
        $c = ($c+2);
    }
}

print "$newnumber\n";

# 4  
Old 08-19-2008
Hi hiptoss
Thanks a lot for the reply. Script is working perfectly fine. Smilie


Hi Franklin52
This definitely is not a homework question. In Telecom domain we typically store the data in BCD format but for retrieving we have to do some manipulations ...


Regards,
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. OS X (Apple)

String manipulation

i have a string that am looking to extract all characters following 3 consecutiv numbers. Example my string is J1705PEAN038TDMN, i need to get TDMN My string can have multiple 3 consecutive numbers, i need what follows last occurance (9 Replies)
Discussion started by: gigagigosu
9 Replies

2. Shell Programming and Scripting

String manipulation.

If a have a variable with a first and last name. and say the variable looks like this... FIRST LAST how could process the variable to look like First .L bash 3.2 (osx) (3 Replies)
Discussion started by: briandanielz
3 Replies

3. Shell Programming and Scripting

String Manipulation

I'm making a little game in Perl, and I am trying to remove the first instance of a character in an arbitrary string. For example, if the string is "cupcakes"and the user enters another string that contains letters from "cupcake" e.g: "sake"the original string will now look like this (below)... (3 Replies)
Discussion started by: whyte_rhyno
3 Replies

4. Shell Programming and Scripting

String manipulation

Hi , I am getting a string like aaa,bbb,sdsdad,sdfsdf,sdfsdfdsf,rtyrtyr,45654654,ddfdfdfgdfg,dfgdfgdg........... Now what I need is to format it. So after each nth comma I need one newline. So the above will look like when n=3 aaa,bbb,sdsdad, sdfsdf,sdfsdfdsf,rtyrtyr,... (4 Replies)
Discussion started by: Anupam_Halder
4 Replies

5. Shell Programming and Scripting

Deleting part of a string : string manipulation

i have something like this... echo "teCertificateId" | awk -F'Id' '{ print $1 }' | awk -F'te' '{ print $2 }' Certifica the awk should remove 'te' only if it is present at the start of the string.. anywhere else it should ignore it. expected output is Certificate (7 Replies)
Discussion started by: vivek d r
7 Replies

6. Homework & Coursework Questions

String Manipulation

Write a shell program to display the position of the right - most character in a given input string. Example : Input : RAHUL Output : L is in the 5th position also tell me how to count length of string and how to find the position of specific character in left most side. Homework... (0 Replies)
Discussion started by: shashwat2691
0 Replies

7. Shell Programming and Scripting

string manipulation

if I have two string variable, how do I add one to anther. like a= "a" b="b" c=$a+$b but that doesn't work. Is there anyway to solve it.http://www.qtl.co.il/img/copy.pnghttp://www.google.com/favicon.icohttp://www.babylon.com/favicon.icohttp://www.morfix.com/favicon.ico (2 Replies)
Discussion started by: programAngel
2 Replies

8. UNIX for Dummies Questions & Answers

String manipulation

I am doing some training for a job I have just got and there is an exercise I am stuck with. I am not posting to ask a question about logic, just a trivial help with string manipulation. I would appreciate if somebody could at least give me a hint on how to do it. Basically, the intelligent part... (8 Replies)
Discussion started by: Dantastik
8 Replies

9. Shell Programming and Scripting

String Manipulation Help

Hey Guys, Right i know how to alter a word to begin with a capital letter, i know how to remove unwanted characters and replace them with the relevant character however i don't now if there is a way to do them all in one line. Code: echo -n ${string:0:1} | tr a-z A-Z #convert first letter... (4 Replies)
Discussion started by: shadow0001
4 Replies

10. UNIX for Advanced & Expert Users

Urgent Awk manipulation query help

Hi UNIX gurus :) , I have the following requirement and not being able to find a solution :( . i have got a file demo.lst movies 10 12 25 songs 11 8 15 DVD 15 12 10 Total 58 60 75 NOTE:-Total value not the sum of the columns but some predefined value. My... (5 Replies)
Discussion started by: rahul26
5 Replies
Login or Register to Ask a Question