LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-16-2006, 06:02 AM   #1
Rindert
LQ Newbie
 
Registered: Jun 2005
Location: Roosendaal
Distribution: Kubuntu 6.10, amd64
Posts: 13

Rep: Reputation: 0
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
 
Old 08-16-2006, 06:19 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
can i suggest using basename instead?
Code:
# basename /root/test.txt .txt
test
 
Old 08-16-2006, 06:49 AM   #3
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
How about this?
Code:
ada@barnabas:~> ( filename=otherfile002.jpg; echo ${filename/%???.jpg})
otherfile
 
Old 08-16-2006, 06:54 AM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
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...
 
Old 08-16-2006, 06:57 AM   #5
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
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.
 
Old 08-16-2006, 07:11 AM   #6
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,499
Blog Entries: 2

Rep: Reputation: 68
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
 
Old 08-16-2006, 07:15 AM   #7
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,499
Blog Entries: 2

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

Code:
$ echo otherfile001.jpg | rev | cut -c 8- | rev
otherfile
 
Old 08-16-2006, 07:58 AM   #8
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
best?? pah, not even close...
 
Old 08-16-2006, 08:16 AM   #9
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,499
Blog Entries: 2

Rep: Reputation: 68
Ok, not "The Best", but how about the "easy" part ?

Cheers,
 
Old 08-16-2006, 08:58 AM   #10
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
easy? piping through 3 commands when 1 will do? keep trying
 
Old 08-16-2006, 09:05 AM   #11
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,499
Blog Entries: 2

Rep: Reputation: 68
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,
 
Old 08-16-2006, 11:49 AM   #12
Rindert
LQ Newbie
 
Registered: Jun 2005
Location: Roosendaal
Distribution: Kubuntu 6.10, amd64
Posts: 13

Original Poster
Rep: Reputation: 0
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
 
Old 08-16-2006, 01:18 PM   #13
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
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
 
Old 08-16-2006, 01:19 PM   #14
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
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}
 
Old 08-16-2006, 07:48 PM   #15
Rindert
LQ Newbie
 
Registered: Jun 2005
Location: Roosendaal
Distribution: Kubuntu 6.10, amd64
Posts: 13

Original Poster
Rep: Reputation: 0
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
MPlayer cutting off the start of each file nutthick Linux - Software 3 04-26-2006 12:40 PM
Html Form File Cottsay General 0 11-05-2005 01:30 PM
Sed Help Needed..Cutting out peice of file farmerjoe Programming 2 02-25-2005 06:05 PM
cutting down contents of text file kam_kenneth Linux - Newbie 2 05-22-2004 01:16 AM
I want Linux source code for FAT file system in user readable form not in binary form ramya272 Linux - Newbie 5 02-05-2004 07:54 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration