LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   sed command to replace file extension (https://www.linuxquestions.org/questions/linux-newbie-8/sed-command-to-replace-file-extension-774606/)

leighya 12-09-2009 08:34 PM

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

ghostdog74 12-09-2009 08:40 PM

you don't need sed to remove file extension
Code:

$ filename="a.txt"
$ echo ${filename%.*}
a


leighya 12-09-2009 08:51 PM

Quote:

Originally Posted by ghostdog74 (Post 3786082)
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.

ghostdog74 12-09-2009 08:53 PM

Code:

NFILE=${filename%.*}

leighya 12-09-2009 09:16 PM

Quote:

Originally Posted by ghostdog74 (Post 3786088)
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[]

chrism01 12-09-2009 09:20 PM

Code:

fullname="a.txt"
fname=$(echo $fullname|cut -d'.' -f1)
newname=${fname}.dat


ghostdog74 12-09-2009 10:22 PM

Quote:

Originally Posted by leighya (Post 3786103)
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.

centos82 12-09-2009 11:27 PM

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}'

leighya 12-10-2009 01:21 AM

Quote:

Originally Posted by centos82 (Post 3786187)
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

catkin 12-10-2009 02:11 AM

Quote:

Originally Posted by leighya (Post 3786279)
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


leighya 12-10-2009 02:26 AM

Quote:

Originally Posted by catkin (Post 3786321)
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

chrism01 12-10-2009 05:07 PM

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.

ghostdog74 12-10-2009 06:49 PM

Quote:

Originally Posted by chrism01 (Post 3787119)
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"


ArfaSmif 12-10-2009 08:43 PM

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

$ basename filename.txt .txt

will give you :-

filename

ghostdog74 12-10-2009 08:51 PM

Quote:

Originally Posted by ArfaSmif (Post 3787248)
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.


All times are GMT -5. The time now is 07:48 PM.