How to get filename extension
Hi
I wrote a small and easy bash script, which just renames a file name to another. Here it is: #!/bin/bash echo "Original filename (without extension)" read n echo "New filename" read m f=`pwd` a=`ls $f | grep $n` b=`echo $a | sed ????????????` mv $f/$a $f/$m.$b The only problem is with sed, as I want to get the filenames extension. Sed is kind of sophisticated, could someone help me to get the extension from the filename (I don't need the filename, but its extension)? Thanks in advance Szymon |
Code:
#!/bin/bash |
That would depend a lot on what you mean by extension. DOS/Windoze requires "etensions" such as .exe, .cmd, .bat, .jpg etc... but Linux/UNIX do not. You can add such "suffixes" but it is just another part of the file name. Also since the "." has no special meaning you could actually have multiples for example:
this.is.my.favorite.file The end of the file is ".file" but that is not an extension. Now you CAN get the end of the file but you need to be aware of the foregoing. If you KNEW that all you had to search through were files that only had one dot like standard DOS/Windoze files you could do: b=`echo $a | awk -F. '{print $2}'` Here you use awk rather than sed. The -F says to set the delimiter to dot (.) and the print statement says to print the second field which would be the one after the dot. If you did that on the example above however instead of getting "file" as the "extension" you'd get "is" as the extension. |
Thanks, jlightner, your method works! I have found somewhere in the Internet another approach to that:
Code:
b=`echo $a | sed -e 's/.*\.//'` Exactly, I need to treat the filename as "name.extension", although it is not Linux style at all (old windows habit). I just wanted to save the user (me) the need for typing the whole filename, with the "extension" (I always avoid to have multiple dots in the filename). In fact, if one would like to rename the file test.tar to some.tar, he could just type "te" in the first program invocation, and the script should find the corresponding file (thanks to grep, really a great thing) - on the condition, that in the current directory there aren't any other files with the phrase "te" in the filename. Sed seems to be really complicated, could you tell me, what exactly is going on in the line above? Thanks for your answers Szymon |
s/.*\.//'
s = substitute first / = locate (search for) . = regular expression (regexp) special character meaning "concatenate" (join together what came before and after it). * = match all characters. \ = "escape" the character that follows. Some characters have special meaning (. for example means "concatenate") in regexp as noted above so putting "\." means look literally for the character "." rather than trying to use it to concatenate. So .*\. means look for any pattern up to and including the literal character "." (dot) second / = replace with - This would be followed by what ever pattern you wanted to put in place of what you searched for. third / = finish the replace with - since there was nothing after the second / before the third / you're essentially saying "replace all characters up to and including the dot with "nothing" which is a cute way of deleting them. The above sytnax (s/pattern/pattern/) is the most common usage of sed. One thing not done there (because it wasn't necessary) is to add a g after the 3rd slash. That would say "global". If you were using sed to parse the text IN a file rather than just a list and that text had multiple occurrences of "pattern" on a line then it would only change the first one without the g. |
Quote:
|
Jlightner, thank you very much for the explanation of the sed-command, now it is sort of easier for me to understand, what was written. Writing more complicated programs is not for me, I think, but in opposition to Windows, Linux has the advantage of shipping bash and other environments within the core system, so one can write easy "applets" and useful programs (for example, with gdialog, very good thing).
Thanks again. |
Quote:
Quote:
Quote:
Quote:
Quote:
|
All times are GMT -5. The time now is 09:56 AM. |