LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   cutting the last N characters form a file name (https://www.linuxquestions.org/questions/linux-general-1/cutting-the-last-n-characters-form-a-file-name-474351/)

Rindert 08-16-2006 06:02 AM

cutting the last N characters form a file name
 
Hello,

I'm writing a bash script and I would like to do the following:
I have a directory with the following:
file001.jpg
file002.jpg
file003.jpg
Now i want a bash script that can give 'file' back to me.
The command: 'cut -c 1-4' doest work for me, cause i also want the same to work on
otherfile001.jpg
otherfiie002.jpg
otherfile003.jpg

So what i'm looking for is a command that can strip the last 7 characters for me.

Anyone know how this can be done?

Thanks,

Rindert

acid_kewpie 08-16-2006 06:19 AM

can i suggest using basename instead?
Code:

# basename /root/test.txt .txt
test


spirit receiver 08-16-2006 06:49 AM

How about this?
Code:

ada@barnabas:~> ( filename=otherfile002.jpg; echo ${filename/%???.jpg})
otherfile


acid_kewpie 08-16-2006 06:54 AM

Quote:

Originally Posted by spirit receiver
How about this?
Code:

ada@barnabas:~> ( filename=otherfile002.jpg; echo ${filename/%???.jpg})
otherfile


i'd still recommend basename over this approach, you could actually still use a cut, but use the . as a field seperator, and keep the first field only
Code:

# echo test.txt | cut -d. -f1
test

all depends exactly what your scenario is...

spirit receiver 08-16-2006 06:57 AM

But Rindert's trying to remove the last 7 characters which consist of varying digits and the extension .jpg. That's why basename won't work.

marozsas 08-16-2006 07:11 AM

The best (and easy) approach is to remove 7 characters (xxx.jpg) from the BEGINNING of the string. The command 'rev' is handy in this case.

Code:

$ echo otherfile001.jpg | rev | sed -e 's/.......//' | rev
otherfile
$ echo file001.jpg | rev | sed -e 's/.......//' | rev
file


marozsas 08-16-2006 07:15 AM

Of course, if you like cut, ou can use 'cut -c 8-', instead sed.

Code:

$ echo otherfile001.jpg | rev | cut -c 8- | rev
otherfile


acid_kewpie 08-16-2006 07:58 AM

best?? pah, not even close... :)

marozsas 08-16-2006 08:16 AM

Ok, not "The Best", but how about the "easy" part ? :)

Cheers,

acid_kewpie 08-16-2006 08:58 AM

easy? piping through 3 commands when 1 will do? keep trying :)

marozsas 08-16-2006 09:05 AM

Hey acid_kewpie !

All right, I will never more use the words "best" and "easy" in a post, specially if you are subscribed to it ;)

cheers,

Rindert 08-16-2006 11:49 AM

Hello,

I've tried the
( filename=otherfile002.jpg; echo ${filename/%???.jpg})
command, it does work on the shell, but how can i use it in my bash script? I have to use it for mutpiple dirs so i think best way i to use with 'ls' right? But how do I get the result from 'ls' in the filename variable?
I'm kinda new to shell scripting. This is my first own script I'm writing.

Because I'm a newbie to shell scripts. Let say I want the easiest way of doing it, it doesn't have to be best way, as long as it is a good way.

Thanks, Rindert

spirit receiver 08-16-2006 01:18 PM

You could try something like
Code:

#! /bin/bash
cd /some/directory
for file in *.jpg
do
  echo "Found a filename that starts with ${file/%???.jpg}."
done


acid_kewpie 08-16-2006 01:19 PM

well it depends what your circumstances are, most of these ways will work, but do you have different length extensions? different test in extensions? if you do want to use that example there, then you'd simply replace "filename" in the echo part with the variable name you currently have the string assigned to. if you don't actaully want to echo it, but store it, then you'd do, for example:
Code:

original=test.txt
result=${original/%???.jpg}


Rindert 08-16-2006 07:48 PM

I'v tried some things, and for me this works good:
cat_profile= ls -1 *001.jpg | rev | cut -c 8- | rev
the result is get is: otherfile.
just what i wanted :)
thanks everyone for helping me :)


All times are GMT -5. The time now is 12:46 PM.