LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 12-09-2009, 08:34 PM   #1
leighya
LQ Newbie
 
Registered: Dec 2009
Posts: 5

Rep: Reputation: 0
Unhappy sed command to replace file extension


I understand how to use a variable in a sed command, but I can't get the output into a variable.


FILE=readme.txt
now i want to remove the extension of filename
so file woud be:
FILE=readme

my script:
NFILE=`echo $FILE | sed 's/.txt//'`
mv ../out/$FILE ../out/$NFILE
FILE=$NFILE


now when i run my script. i get this unknown character extensions in my new file(NFILE). Can someone help me with this.

Thanks
-newbie
 
Old 12-09-2009, 08:40 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
you don't need sed to remove file extension
Code:
$ filename="a.txt"
$ echo ${filename%.*}
a
 
Old 12-09-2009, 08:51 PM   #3
leighya
LQ Newbie
 
Registered: Dec 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Unhappy

Quote:
Originally Posted by ghostdog74 View Post
you don't need sed to remove file extension
Code:
$ filename="a.txt"
$ echo ${filename%.*}
a

how do i do this

echo Renaming $FILE
NFILE= echo ${filename%.*}
mv ../out/$FILE ../out/$NFILE
FILE=$NFILE

I'm really sorry for this simple question.
 
Old 12-09-2009, 08:53 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
NFILE=${filename%.*}
 
Old 12-09-2009, 09:16 PM   #5
leighya
LQ Newbie
 
Registered: Dec 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ghostdog74 View Post
Code:
NFILE=${filename%.*}
Thanks!this works.but i seem to get a little rectangle character in my new file.
becomes: readme[]

and if i put a new file extension to this file,this character still doesn't disappear.
e.g: readme.enc[]
 
Old 12-09-2009, 09:20 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Code:
fullname="a.txt"
fname=$(echo $fullname|cut -d'.' -f1)
newname=${fname}.dat
 
Old 12-09-2009, 10:22 PM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by leighya View Post
Thanks!this works.but i seem to get a little rectangle character in my new file.
becomes: readme[]

and if i put a new file extension to this file,this character still doesn't disappear.
e.g: readme.enc[]
this should not happen. what is your actual file name? if it is just readme.txt, you would only get "readme" and nothing else.
 
Old 12-09-2009, 11:27 PM   #8
centos82
Member
 
Registered: Sep 2008
Distribution: CentOS
Posts: 75

Rep: Reputation: 16
I am going to agree with ghostdog. There is some hidden character in your filename.

Try navigating to the directory your file is in.

Run "ls -l > myls"
Then "cat -vet myls"

This will show you any hidden characters in your file. See if there is something between readme and your ".". I'll bet there is.

Also, one thing to note about you sed command. It should work but the . in sed is a wildcard so if your file was named readmeAtxt your sed command would change that to readme even though it was not a .txt file because the wildcard matches to the "A". The proper sed is sed 's/\.txt$//' The "\" says ignore the . as a wildcard and treat it as a literal . The $ which could be considered optional says the .txt should occur at the end of the string. If you are dealing with files that have different extensions though, the script is mutch better written with the awk or cut command.

echo $STRING | cut -d. -f1
or
echo $STRING | awk -F. '{print $1}'

Last edited by centos82; 12-09-2009 at 11:29 PM.
 
Old 12-10-2009, 01:21 AM   #9
leighya
LQ Newbie
 
Registered: Dec 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by centos82 View Post
I am going to agree with ghostdog. There is some hidden character in your filename.

Try navigating to the directory your file is in.

Run "ls -l > myls"
Then "cat -vet myls"

This will show you any hidden characters in your file. See if there is something between readme and your ".". I'll bet there is.

Also, one thing to note about you sed command. It should work but the . in sed is a wildcard so if your file was named readmeAtxt your sed command would change that to readme even though it was not a .txt file because the wildcard matches to the "A". The proper sed is sed 's/\.txt$//' The "\" says ignore the . as a wildcard and treat it as a literal . The $ which could be considered optional says the .txt should occur at the end of the string. If you are dealing with files that have different extensions though, the script is mutch better written with the awk or cut command.

echo $STRING | cut -d. -f1
or
echo $STRING | awk -F. '{print $1}'



Thanks for the explanation. I used this script to get rid of the file extension

$(echo $filename|cut -d'.' -f1)


But still the unknown character is there.
Now when i tried to look at the current directory of the file with the unknown character(rectangle thingy)..it turned out to be a \r char. like this: readme\r
 
Old 12-10-2009, 02:11 AM   #10
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by leighya View Post
Now when i tried to look at the current directory of the file with the unknown character(rectangle thingy)..it turned out to be a \r char. like this: readme\r
If you have only one such file beginning with readme you can fix it manually with
Code:
mv readme* readme
Here's how I reproduced the problem and fixed it
Code:
c:~/d/tmp$ x=$'readme\r'
c:~/d/tmp$ touch "$x"
c:~/d/tmp$ ls readme*
readme?
c:~/d/tmp$ mv readme* readme
c:~/d/tmp$ ls readme*
readme
 
Old 12-10-2009, 02:26 AM   #11
leighya
LQ Newbie
 
Registered: Dec 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by catkin View Post
If you have only one such file beginning with readme you can fix it manually with
Code:
mv readme* readme
Here's how I reproduced the problem and fixed it
Code:
c:~/d/tmp$ x=$'readme\r'
c:~/d/tmp$ touch "$x"
c:~/d/tmp$ ls readme*
readme?
c:~/d/tmp$ mv readme* readme
c:~/d/tmp$ ls readme*
readme

i have multiple files to read and rename.
i used $FILE for the filename
 
Old 12-10-2009, 05:07 PM   #12
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
You can add a loop to my code
Code:
for fullname in $(cat filelist.txt)
do
    fname=$(echo $fullname|cut -d'.' -f1)
    newname=${fname}.dat
done
for filenames that have bad char(s) at the end, you'll need string manipulation fns http://tldp.org/LDP/abs/html/string-manipulation.html to remove them.
 
Old 12-10-2009, 06:49 PM   #13
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by chrism01 View Post
You can add a loop to my code
Code:
for fullname in $(cat filelist.txt)
do
    fname=$(echo $fullname|cut -d'.' -f1)
    newname=${fname}.dat
done
for filenames that have bad char(s) at the end, you'll need string manipulation fns http://tldp.org/LDP/abs/html/string-manipulation.html to remove them.
breaks on files with spaces... using cat + for loop like that is bad. Either have to change IFS or use a while read loop. Also, with shell, no need to use external command. Its faster that way
Code:
while read -r fullname
do
    IFS="."
    set -- $fullname
    fname=$1
    echo $fname
    # to get rid of bad chars
    echo ${fname//[^[:print:]]/}
done <"file"
 
Old 12-10-2009, 08:43 PM   #14
ArfaSmif
Member
 
Registered: Oct 2008
Location: Brisbane Australia
Distribution: Fedora, Centos, Manjaro
Posts: 317

Rep: Reputation: 70
Another simple way to do this is to use the command "basename"
For example:-

$ basename filename.txt .txt

will give you :-

filename
 
Old 12-10-2009, 08:51 PM   #15
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by ArfaSmif View Post
Another simple way to do this is to use the command "basename"
For example:-

$ basename filename.txt .txt

will give you :-

filename
basename works on one file and AFAIK does not support wildcard (if it does, correct me). To work on multiple files , effectively a loop is still required. In that case, its the same as calling external command for each file.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem using sed to replace string in file umk Debian 12 02-01-2012 08:39 AM
sed command to replace slash with backslash stelmed Slackware 13 08-19-2010 05:01 AM
Sed to read the file and replace and insert the pattern saurabhchokshi Programming 2 06-12-2009 01:15 PM
sed command to replace date format 2007/01/22 with 070122? johnpaulodonnell Linux - Newbie 3 01-23-2007 07:29 AM
sed - find and replace command bullshit Programming 9 01-05-2006 03:25 AM

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

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