It is currently Thu, 20 Jan 2022 09:36:53 GMT



 
Author Message
 Returning only 1 string problem
Hi,

I have made a shell script where I pass a filename that extension fmb,
mmb or  pll and it returns the same name but with extension fmx, mmx
or plx.

The problem I have is that I want to do it in 1 line, something like
this:

 outfile=$(echo $1 | sed 's/\(.*\.\)fmb/\1fmx/')$(echo $1 | sed
's/\(.*\.\)mmb/\1mmx/')$(echo $1 | sed 's/\(.*\.\)pll/\1plx/')

Ex. script.sh file1.fmb
But this returns file1.fmxfile1.fmbfile1.fmb and I want only
file1.fmx.

Ex. script.sh file1.mmb
It returns file1.fmbfile1.mmxfile1.fmb and I want only file1.mmx

Thanks in advance,

John Lewis.



 Sat, 09 Sep 2006 19:57:44 GMT   
 Returning only 1 string problem

What about
outfile=$(echo $1 | sed -e "s/.fmb/.fmx/g" -e "s/.pll/.plx/g" -e "s/.mmb/.mmx/g")

Seemed to work for me.

--

+=======+==+=======>
LANkrypt0 :: aa#2118
"GzA SOb FE6 OQQ:
 pFp zAM 6Fn MS6 QUb 6po J6."
- KAQ QAO 08z Om6 SZ6 Oo6, wO0 Q6p
postcount=$(($postcount+1))
+=========================+======>



 Sat, 09 Sep 2006 21:12:20 GMT   
 Returning only 1 string problem

As an add to this, through playing around with regex's the following
_should_ work for any 3 letter extensions universally:

sed -e "s/\\.[a-z][a-z]/&x/g" -e "s/.$//g"

[lankrypt0@localhost]$ echo file1.fll | sed -e "s/\\.[a-z][a-z]/&x/g" -e "s/.$//g"
file1.flx
[lankrypt0@localhost]$ echo file1.mmb | sed -e "s/\\.[a-z][a-z]/&x/g" -e "s/.$//g"
file1.mmx
[lankrypt0@localhost]$ echo file1.pll | sed -e "s/\\.[a-z][a-z]/&x/g" -e "s/.$//g"
file1.plx

--

+=======+==+=======>
LANkrypt0 :: aa#2118
"GzA SOb FE6 OQQ:
 pFp zAM 6Fn MS6 QUb 6po J6."
- KAQ QAO 08z Om6 SZ6 Oo6, wO0 Q6p
postcount=$(($postcount+1))
+=========================+======>



 Sat, 09 Sep 2006 21:37:43 GMT   
 Returning only 1 string problem

This will work in any shell:

        outfile=`basename "$1" .`x

In ksh and, I believe, in some others (but not sh) it's just:

        outfile="${1%?}x"

Regards,

        Ed



 Sat, 09 Sep 2006 23:37:51 GMT   
 Returning only 1 string problem

    Not a good idea. Forcing multi-line scripts into a single line has
    novelty value, but not much else.

    And, of course, posting very long lines to a newsgroup is not a
    good idea, even if it doesn't wrap at an inopportune place.

    Even if you do want the final script in a single line, develop it
    in a more readable form, then massage it.

    In this case, however, it is a very simple one-liner. Since you
    use the $() form of command substitution, your shell can (almost
    certainly) also do string chopping in variable expansion.

    Your problem involves nothing more than changing the last letter,
    so the script is just:

echo ${1%?}x

--
    Chris F.A. Johnson                  http://cfaj.freeshell.org/shell
    ===================================================================
    My code (if any) in this post is copyright 2004, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License



 Sun, 10 Sep 2006 00:20:23 GMT   
 Returning only 1 string problem

outfile="'sed -e 's/.$/x/' <<[EOF]
$1
[EOF]'"

or,

outfile="`expr \"x$1\" : 'x\(.*\).'`x"



 Sun, 10 Sep 2006 01:38:15 GMT   
 Returning only 1 string problem

this is not one line but i think it would work

case "$file_name" in
        *.fmb)
              remove_suffix=".fmx"
              new_suffix="value here"
            ;;
        *.mmb)
              remove_suffix=".mmb"
              new_suffix="value here"
            ;;

        ....
        ....
        *)

            ;;
    esac

    file=$(basename $file_name $remove_suffix)
    newfile=$file$new_suffix

Hope this helps



 Sun, 10 Sep 2006 01:44:04 GMT   
 Returning only 1 string problem

Hint:
    If there is no solution, change the problem.  To change the last
    character to 'x',
        echo $1 | sed 's/.$/x/'
        echo ${1%?}x

--
William Park, Open Geometry Consulting, <opengeome...@yahoo.ca>
Linux solution for data processing and document management.



 Sun, 10 Sep 2006 03:42:47 GMT   
 Returning only 1 string problem
Hi,

The best  is of course the shortest one: outfile="${1%?}x"

Thanks for all your replies!



 Tue, 12 Sep 2006 00:21:32 GMT   
 
   [ 9 post ] 

Similar Threads

1. return number string from a char string

2. IBCS2 problem- getipdomainname returns null string

3. Returned string length of getenv(3C) in Solaris?

4. Need app that returns 2 or more equal strings in a file for bash script

5. Need help returning a string from a function

6. Return character in vi search string

7. does getenv return a null terminated string ?

8. Q: proper way to return strings from lex?

9. AIX returns bad current-time-string


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software