LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 09-06-2009, 01:27 AM   #1
clstanton
LQ Newbie
 
Registered: May 2008
Posts: 27

Rep: Reputation: 0
Removing characters from file name


Good evening. I need to remove letters from filenames up to and including the "-":

example:

ne-swickdnv.txt
south-windsexto.txt
westcomp-hawk22.txt

there are a variable number of characters before the "-". The output should be:

swickdnv.txt
windsexto.txt
hawk22.txt

I have tried to use sed

for file in *.txt;
do
sed 's/*-//g' $file >tempfile
mv tempfile $file
done

of course this did not work or I would not be here. I have tried several variations with no luck. I am sure it is because I do not understand the correct use of the wildcard in the sed command but I cannot find anything on this. All examples I have found remove a set number of charaters or deal with spaces.

Thanks for the time.

Chris
 
Old 09-06-2009, 01:49 AM   #2
pielas
Member
 
Registered: Jan 2009
Location: Poland
Distribution: Arch Linux
Posts: 50

Rep: Reputation: 17
use cut:

for file in *.txt;
do
mv "$file" "`echo $file|cut -d "-" -f 2`";
done

EDIT:
If you have more than one "-" in file names try 2- instead of 2

Last edited by pielas; 09-06-2009 at 01:57 AM.
 
Old 09-06-2009, 02:01 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
An alternative is shell parameter substitution:
Code:
#!/bin/bash
for file in *.txt
do
  mv "$file" "${file##*-}"
done
 
Old 09-06-2009, 02:15 AM   #4
clstanton
LQ Newbie
 
Registered: May 2008
Posts: 27

Original Poster
Rep: Reputation: 0
Thank you both!
I tried both options and they both worked. I promise I will study these options of the mv command. I did not know they existed.

Chris
 
  


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
removing file with special characters rsashok Linux - General 4 02-09-2010 04:08 PM
removing characters from variable? hateseven Programming 6 02-24-2009 09:34 PM
Removing special characters from filenames PlymWS Linux - Software 1 08-09-2007 05:11 AM
Python - Removing first x characters Mithrilhall Programming 3 01-16-2007 09:52 AM
Removing Characters when outputting to a script thedarkdestroyer Linux - Newbie 3 03-21-2006 03:13 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:10 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