Awk-sed help: removing extension name.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk-sed help: removing extension name.
# 1  
Old 06-06-2013
Awk-sed help: removing extension name.

Experts:

Filename:
Code:
 rp8-file.1.cr_cr.cr

I want to remove the extension .cr
I want to get the output as
Code:
rp8-file.1.cr_cr


Tried like this but not getting correct output:


Code:
echo "rp8-file.1.cr_cr.cr" | awk -F. '{$NF="";print $0}'
rp8-file 1 cr_cr

# 2  
Old 06-06-2013
Code:
echo "rp8-file.1.cr_cr.cr" | sed 's/\.[^.]*$//'

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 06-06-2013
Team Thanks, I got it with this awk code with few try:

Code:
echo "p8-file.1.cr_cr.cr" | awk -F[.][c][r]$ '{print $1}'
p8-file.1.cr_cr

Please advise if this is correct way of using the
Code:
-F

field option in awk.
# 4  
Old 06-06-2013
Using awk:
Code:
echo "rp8-file.1.cr_cr.cr" | awk -F'.' '{NF-=1}1' OFS='.'

Using Parameter Substitution in shell:
Code:
filename="rp8-file.1.cr_cr.cr"
echo ${filename%.*}

# 5  
Old 06-06-2013
Quote:
Originally Posted by rveri
Experts:

Filename:
Code:
 rp8-file.1.cr_cr.cr

I want to remove the extension .cr
I want to get the output as
Code:
rp8-file.1.cr_cr


Tried like this but not getting correct output:


[CODE]echo "rp8-file.1.cr_cr.cr" | awk -F. '{$NF="";print $0}'
rp8-file 1 cr_cr
Yoda's suggestion is fine if you want to remove any filename extension. If you just want to remove .cr and leave other extensions as is, use:
Code:
Filename=rp8-file.1.cr_cr.cr
echo "${Filename%.cr}"

# 6  
Old 06-06-2013
You do not need awk for this and if you intend to fire up awk for this single purpose, like in your example, i suggest you do not do it: it will slow down your script considerably.

Use "parameter expansion" instead. Assuming you use either KornShell or bash this is how to remove an extension of ".cr" from a filename:

Code:
filename="something.cr"
echo ${filename%.cr}            # just output the new name, not changing anything
newname="${filename%.cr}"       # assign the new name to a variable

I hope this helps.

bakunin

/PS: seeing Don Craguns suggestion, here are some variants:

Code:
filename="something.cr"
echo ${filename%.??}            # removes any 2-character extension
echo ${filename%.???}           # like above, removes any 3-character extension
echo ${filename%.*}             # removes any extension, that is: from the rightmost "." to the end

I hope this helps.

bakunin

Last edited by bakunin; 06-06-2013 at 05:53 PM..
# 7  
Old 06-06-2013
Quote:
Originally Posted by bakunin
You do not need
... ... ...

Code:
echo ${filename%%.*}           # removes any extension, that is: from the rightmost "." to the end

I hope this helps.

bakunin
Not quite. With %%.* (instead of %.*), it will remove the first period and all characters that follow it; not the rightmost period to the end. For example:
Code:
Filename=rp8-file.1.cr_cr.cr
echo "${Filename%.*}"

produces:
Code:
rp8-file.1.cr_cr

but:
Code:
echo "${Filename%%.*}"

produces:
Code:
rp8-file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

BASH - Removing the very last character(s) extension of a filename

Hello. I would like to know how to do this in bash script : A_WORD="ABCD_EFGH.0.100.40.123" NEW_WORD=remove_last_ext("A_WORD") NEW_WORD --> ABCD_EFGH.0.100.40 A_WORD="ABCD_EFGH.0.50.3" NEW_WORD=remove_last_ext("A_WORD") NEW_WORD --> ABCD_EFGH.0.50 A_WORD="ABCD_EFGH.3.100.50." ... (2 Replies)
Discussion started by: jcdole
2 Replies

2. Shell Programming and Scripting

removing a word in a multiple file starting at the dot extension

hi I would like to ask if someone knows a command or a script on how to rename a multiple file in the directory starting at the end of the filename or at the .extension( i would like to remove the last 11 character before the extension) for example Below is the result of my command ls inside... (5 Replies)
Discussion started by: jao_madn
5 Replies

3. UNIX for Dummies Questions & Answers

sed or awk - removing part of line?

hi all, I am having trouble finding the right string for this - I dont know whether to use awk or sed.. If I have a file with alot of names and phone numbers like this McGowan,Sean 978-934-4000 Kilcoyne,Kathleen 603-555-1212 Club603,The 617-505-1332 Boyle,William 301-444-1221 And... (11 Replies)
Discussion started by: alis
11 Replies

4. Shell Programming and Scripting

removing the filename extension

Is there an easy way to strip off a filename's extension? For example, here's a filename: blahblahblah.thisisok.thisisnotok I want to get rid of .thisisnotok from the filename, so that what's left is blahblahblah.thisisok Thanks. I have a directory full of filenames that need to be... (5 Replies)
Discussion started by: daflore
5 Replies

5. Shell Programming and Scripting

Replacing or removing a long list of pattern by using awk or sed

Input: >abc|123456|def|EXIT| >abc|203456|def|EXIT2| >abc|234056|def|EXIT3| >abc|340056|def|EXIT4| >abc|456000|def|EXIT5| . . . Output: def|EXIT| def|EXIT2| def|EXIT3| def|EXIT4| def|EXIT5| . . My try code: (9 Replies)
Discussion started by: patrick87
9 Replies

6. UNIX for Dummies Questions & Answers

Removing prefix from multiple files and renaming file extension

Hello i have the files in this format pdb1i0t.ent pdb1lv7.ent pdb1pp6.ent pdb1tj2.ent pdb1xg2.ent pdb2b4b.ent pdb2ewe.ent Now i have to remove the prefix pdb from all the files and also i need to change the extension of .ent to .txt The new file should look like this ... (3 Replies)
Discussion started by: empyrean
3 Replies

7. Shell Programming and Scripting

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 (7 Replies)
Discussion started by: r_praveenk
7 Replies

8. UNIX for Dummies Questions & Answers

removing the extension from all filenames in a folder

Hi there, I'm pretty new to UNIX and have tried trawling through this forum to find an answer to what I want to try to do, which I'm sure is very simple but I don't know how to do it. What I have a a folder that contains multiple files that I have copied from Windows and I want to remove the... (5 Replies)
Discussion started by: johnmcclintock
5 Replies

9. Solaris

removing particular lines ending with a .cnt extension in a text file

I have a text file with rows of information (it is basically a ls command information(o/p from ls command)) I need to remove the lines ending with a .cnt extension and keep the lines ending with .zip extension, how to accomplish this. I also only need the date,size and name of the file from every... (2 Replies)
Discussion started by: ramky79
2 Replies

10. Shell Programming and Scripting

Removing the extension and FTP the files

Hi All I am having around 100 files like: a.xml.done a.xml b.xml.done b.xml .... Now I need to remove .done extension from the above and FTP the remaining files. Totally there are 100 files. How to accomplish this (1 Reply)
Discussion started by: pradkumar
1 Replies
Login or Register to Ask a Question