extract file extension using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extract file extension using sed
# 1  
Old 03-06-2008
extract file extension using sed

Hi,

how can i extract file extension using sed? for e.g., if a file name is abc.txt then how can i get "txt" (after .)

Thanks
praveen
# 2  
Old 03-06-2008
Code:
echo  abc.txt  | sed 's/.*\.//'
txt

# 3  
Old 03-06-2008
And awk one:
Code:
echo /home/abc.txt | awk -F"." '{print $NF}'

# 4  
Old 03-06-2008
if using bash
Code:
# a=/home/abc.txt.pdf
# echo ${a##*.}
pdf

# 5  
Old 03-06-2008
With zsh (also csh and tcsh):

Code:
% a=/home/abc.txt.pdf
% print $a:e
pdf

# 6  
Old 09-04-2008
Hi, i have some task in which i have a parameter file say SQLREJ.dat and in the script i need to extract the file name only without extension i.e. SQLREJ. and i'm passing it while running the script as third parameter that is it is $3. till now i am extracting it's name by just typing $3 in the script. I sthere any other way so that i can only get file name without any extension? can anybody help me with this ASAP.
Thanks,
Manmeet

Last edited by manmeet; 09-04-2008 at 07:51 PM..
# 7  
Old 09-05-2008
Manmeet, you should always start a new thread if you are asking a question about a new issue. It makes it clearer to everybody that this is a new request for help.

To answer your question, here are two ways of doing what you want in either bash or ksh
Code:
$ str=SQLREJ.dat
$ echo $str
SQLREJ.dat
$ echo ${str%\.dat}
SQLREJ
$ echo ${str%\.*}
SQLREJ

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using sed to extract line of a file

Ok .. This is driving me nuts. I suppose I need another set of eyes. Example Data interface Vlan1 description xxxxxxxxxxxx ip address 192.168.1.1 255.255.255.252 no ip redirects no ip proxy-arp ip flow ingress ip pim sparse-mode load-interval 30 ! interface Vlan2 ... (5 Replies)
Discussion started by: popeye
5 Replies

2. Shell Programming and Scripting

Awk-sed help: removing extension name.

Experts: Filename: rp8-file.1.cr_cr.cr I want to remove the extension .cr I want to get the output as rp8-file.1.cr_cr Tried like this but not getting correct output: echo "rp8-file.1.cr_cr.cr" | awk -F. '{$NF="";print $0}' rp8-file 1 cr_cr (11 Replies)
Discussion started by: rveri
11 Replies

3. Shell Programming and Scripting

sed - extract text from xml file

hi, please help, i have an xml file, e.g: ... <tag> test text asdas="${abc}" xvxvbs:asdas${222}sdad asasa="${aa_bb_22}" </tag> ... i want to extract all "${...}", e.g: ${abc} ${222} ${aa_bb_22} thank you. (2 Replies)
Discussion started by: gioni
2 Replies

4. Shell Programming and Scripting

Use grep sed or awk to extract string from log file and put into CSV

I'd like to copy strings from a log file and put them into a CSV. The strings could be on different line numbers, depending on size of log. Example Log File: File = foo.bat Date = 11/11/11 User = Foo Bar Size = 1024 ... CSV should look like: "foo.bat","11/11/11","Foo Bar","1024" (7 Replies)
Discussion started by: chipperuga
7 Replies

5. Shell Programming and Scripting

Need help please with Grep/Sed command to extract text and numbers from a file

Hello All, I need to extract lines from a file that contains ALPHANUMERIC and the length of Alphanumeric is set to 16. I have pasted the sample of the lines from the text file that I have created. My problem is that sometimes 16 appears in other part of the line. I'm only interested to... (14 Replies)
Discussion started by: mnassiri
14 Replies

6. Shell Programming and Scripting

Extract a number from a line in a file and sed in another copied file

Dear all, I am trying to extract a number from a line in one file (task 1), duplicate another file (task 2) and replace all instances of the strings 300, in duplicated with the extracted number (task 3). Here is what I have tried so far: for ((k=1;k<4;k++)); do temp=`sed -n "${k}p"... (2 Replies)
Discussion started by: mnaqvi
2 Replies

7. Shell Programming and Scripting

Using SED/AWK to extract xml at end of file

Hello everyone, Firstly i do not require alot of help.. i am right at the end of finishing my scipt but cannot find a solution to the last part. What i need to do is, prompt the user for a file to work with, which i have done. promt the user for an output file - which is done. #!/bin/bash... (14 Replies)
Discussion started by: hugh86
14 Replies

8. Shell Programming and Scripting

sed or awk to extract data from Xml file

Hi, I want to get data from Xml file by using sed or awk command. I want to get the following result : mon titre 1;Createur1;Dossier1 mon titre 1;Createur1;Dossier1 and save it in cvs file (fichier.cvs). FROM this Xml file (test.xml): <playlist version="1"> <trackList> <track>... (1 Reply)
Discussion started by: yeclota
1 Replies

9. Shell Programming and Scripting

Extract The File Path using SED

hi, I have a file path like usr/scripts/pass/bin and usr/scripts/pass/line I want to extract first three characters using sed Like for path usr/scripts/pass/bin i want to extract usr/scripts/pass and for path usr/scripts/pass/line i want to extract usr/scripts/pass (10 Replies)
Discussion started by: Diggi
10 Replies

10. Shell Programming and Scripting

using sed to conditionally extract stanzas of a file based on a search string

Dear All, I have a file with the syntax below (composed of several <log ..... </log> stanzas) I need to search this file for a number e.g. 2348022225919, and if it is found in a stanza, copy the whole stanza/section (<log .... </log>) to another output file. The numbers to search for are... (0 Replies)
Discussion started by: aitayemi
0 Replies
Login or Register to Ask a Question