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 |
you don't need sed to remove file extension
Code:
$ filename="a.txt" |
Quote:
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. |
Code:
NFILE=${filename%.*} |
Quote:
becomes: readme[] and if i put a new file extension to this file,this character still doesn't disappear. e.g: readme.enc[] |
Code:
fullname="a.txt" |
Quote:
|
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}' |
Quote:
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 |
Quote:
Code:
mv readme* readme Code:
c:~/d/tmp$ x=$'readme\r' |
Quote:
i have multiple files to read and rename. i used $FILE for the filename |
You can add a loop to my code
Code:
for fullname in $(cat filelist.txt) |
Quote:
Code:
while read -r fullname |
Another simple way to do this is to use the command "basename"
For example:- $ basename filename.txt .txt will give you :- filename |
Quote:
|
All times are GMT -5. The time now is 07:48 PM. |