LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-02-2012, 01:38 PM   #1
brock_pace
LQ Newbie
 
Registered: Jun 2012
Posts: 23

Rep: Reputation: Disabled
How to delete a character.


Im trying to change a filename so that it doesn't have any hyphen/dashes in it. Basically, I have a file like a-1, I just want a1. any suggestions.
 
Old 07-02-2012, 01:49 PM   #2
earthnet
Member
 
Registered: Jul 2012
Distribution: OpenSUSE
Posts: 36

Rep: Reputation: Disabled
Have you tried anything to rename the file?
 
Old 07-02-2012, 01:50 PM   #3
brock_pace
LQ Newbie
 
Registered: Jun 2012
Posts: 23

Original Poster
Rep: Reputation: Disabled
Well thats not actually the file name, just an example. But with the real file name ive changed it to make it all lowercase using tr.
 
Old 07-02-2012, 01:59 PM   #4
earthnet
Member
 
Registered: Jul 2012
Distribution: OpenSUSE
Posts: 36

Rep: Reputation: Disabled
So, did you figure it out? Do you still need help?
 
Old 07-02-2012, 02:01 PM   #5
brock_pace
LQ Newbie
 
Registered: Jun 2012
Posts: 23

Original Poster
Rep: Reputation: Disabled
yeah, i dont know how to take out a hyphen from a file name
 
Old 07-02-2012, 02:02 PM   #6
earthnet
Member
 
Registered: Jul 2012
Distribution: OpenSUSE
Posts: 36

Rep: Reputation: Disabled
In your example it would be
Code:
mv a-1 a1
 
Old 07-02-2012, 02:06 PM   #7
brock_pace
LQ Newbie
 
Registered: Jun 2012
Posts: 23

Original Poster
Rep: Reputation: Disabled
hm, yeah that works great on my file, but for curiosities sake, how would you get rid of the hyphens in a string of randomly named files.
 
Old 07-02-2012, 02:15 PM   #8
earthnet
Member
 
Registered: Jul 2012
Distribution: OpenSUSE
Posts: 36

Rep: Reputation: Disabled
To remove the hyphens out of all the filenames in a directory you'd create a bash script:
Code:
#!/bin/bash
for i in ls
do
  mv $i `echo $i | sed -e 's/-//g'`
done
 
Old 07-02-2012, 02:17 PM   #9
brock_pace
LQ Newbie
 
Registered: Jun 2012
Posts: 23

Original Poster
Rep: Reputation: Disabled
im new, and to me that seems complex, could you explain it for me? that would be helpful.
 
Old 07-02-2012, 02:27 PM   #10
earthnet
Member
 
Registered: Jul 2012
Distribution: OpenSUSE
Posts: 36

Rep: Reputation: Disabled
Basically you put those commands into a text file in the directory in question. Change the file to be executable.
Code:
chmod 755 <file>
Then run it.
Code:
./<file>
Here's a breakdown of what is happening:
Code:
#!/bin/bash
This tells the system that it's a bash script.
Code:
for i in `ls`
(it should be `ls` not just ls) Take the output of the "ls" command and store it in the variable $i, repeat what is between "do" and "done" for every file in the list.
Code:
mv $i `echo $i | sed -e 's/-//g'`
mv $i will output "mv <filename>", the "sed" bit outputs the filename without hyphens. So it runs "mv <filename> <filename_without_hyphens>".
 
Old 07-02-2012, 02:35 PM   #11
brock_pace
LQ Newbie
 
Registered: Jun 2012
Posts: 23

Original Poster
Rep: Reputation: Disabled
thanks, that worked. ok, since you are awesome, now lets suppose in these filenames there are numbers in order, 1,2,3,4,...etc. how could i then reformat them so that they are each four digits. 0001, 0002, 0003, 0004, etc.
 
Old 07-02-2012, 02:40 PM   #12
earthnet
Member
 
Registered: Jul 2012
Distribution: OpenSUSE
Posts: 36

Rep: Reputation: Disabled
Here's another thread talking about just that.
 
Old 07-02-2012, 03:12 PM   #13
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,130
Blog Entries: 2

Rep: Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825
Quote:
Originally Posted by earthnet View Post
Code:
for i in `ls`
Using the output of ls in scripts is considered bad practice, since it can cause problems with filenames that have a space or a newline in them. I would change that to
Code:
for i in *
 
1 members found this post helpful.
Old 07-02-2012, 03:35 PM   #14
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Quote:
Originally Posted by brock_pace View Post
thanks, that worked. ok, since you are awesome, now lets suppose in these filenames there are numbers in order, 1,2,3,4,...etc. how could i then reformat them so that they are each four digits. 0001, 0002, 0003, 0004, etc.
brock;

If you look back over all the help you've been getting here, this should be pretty easy----e.g. have you read the SED reference we discussed earlier?

In SED, you can replace a pattern explicitly, or you can detect it and then replace it using a back reference. Here's an example of the latter: (eg imagine I'm acting on a string "xyz", and I want it to be "oooxyz")
Code:
sed 's/\(xyz\)/ooo\1/'
now, instead of the literal "xyz", suppose I want to do the same thing for any 3 characters:
Code:
sed 's/\(...\)/ooo\1/'
Now---in your question---do you want to change all files beginning is a single number? What exactly is the rule to be applied?
 
1 members found this post helpful.
Old 07-09-2012, 03:16 PM   #15
brock_pace
LQ Newbie
 
Registered: Jun 2012
Posts: 23

Original Poster
Rep: Reputation: Disabled
I would like for all files to have 4 digit numbers. For instance 1 would be 0001. 257 would be 0257. Im doing this for 240 files.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Script to delete aligned single-character columns with no field separator? kmkocot Programming 15 04-22-2010 11:45 AM
how to delete nth character in a text file? xiawinter Linux - Software 3 05-13-2008 11:50 AM
Can VI Do This?? Delete before a character in text - Maybe advanced. tbeehler Linux - Software 9 05-07-2007 06:02 PM
vi editor - how to delete a character in each row of a column? flipwhy Linux - Newbie 6 01-22-2007 03:12 PM
vi command to find and delete all lines beginning with a character geomatt Linux - Software 8 12-20-2004 04:30 AM


All times are GMT -5. The time now is 03:47 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration