Awk-sed help: removing extension name.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk-sed help: removing extension name.
# 8  
Old 06-06-2013
Quote:
Originally Posted by Don Cragun
Not quite. With %%.* (instead of %.*), it will remove the first period and all characters that follow it; not the rightmost period to the end.
You are right. The second "%" was a typo on my part, thank you for pointing it out. I corrected it in the posting above.

bakunin
# 9  
Old 06-06-2013
Btw, Yoda, the awk did not work, it returend the same file, however the parameter expansion one is great.
(OS=hp-ux ksh )

Code:
echo "rp8-file.1.cr_cr.cr" | awk -F'.' '{NF-=1}1' OFS='.'
rp8-file.1.cr_cr.cr



Thanks, for , parameter expansion way it worked great:
Code:
Filename=rp8-file.1.cr_cr.cr
echo "${Filename%.cr}"

I would like to know how this works, I never come across this before the parameter expansion way . Thanks all for your time.
# 10  
Old 06-06-2013
I don't know why it didn't work for awk in HP-UX.

But I know for sure that it is buggy, for example it prints FILENAME in BEGIN rule which is not correct as per the standard!!
Code:
awk 'BEGIN { print FILENAME } { print $0 }' file
file
test

# 11  
Old 06-06-2013
Quote:
Originally Posted by rveri
I would like to know how this works, I never come across this before the parameter expansion way .
You can read up on "Parameter Expansion" in the man page of your shell. This, btw., is the reason why it is so fast: it is done completely inside the shell, whereas any external program (awk, sed, tr, ...) would have to be loaded and started first.

Here are some basics, for a head start:

You can address the full content of a variable like this:

Code:
${variable}

but you can also address arbitrary parts of a variables content by using "modifiers":

Code:
${variable#pattern}
${variable##pattern}
${variable%pattern}
${variable%%pattern}

What this does is: the content of "$variable" is reduced by the pattern if it matches. "%" will remove "pattern" from the right side, "#" will remove from the left side:

Code:
variable="start.middle.end"
echo ${variable#*.}
middle.end
echo ${variable%.*}
start.middle

The pattern is "*." in the first example, and it is applied at the left side (#) of the content, whereas the pattern is ".*" in the second example and is applied at the right side. "Pattern" means a file glob in this case, like you search for files "foo.*" to find all files starting with "foo." and any extension.

If you double the "%" or "#" symbols you switch from shortest-possible match to longest-possible match. In case of a single possible match it will make no difference, but in the above case, where "*." could match two possible strings:

Code:
variable="start.middle.end"
echo ${variable##*.}
end
echo ${variable%%.*}
start

You can even nest these expressions and use the outcome of one as the pattern for another:

Code:
variable="get first 2 characters of a string regardless of length"
echo ${variable%${variable#*??}}
ge

Here, "{variable#*??}" produces the third to last character and this is used as a pattern to "${variable%<pattern>}".

Notice, that without an explicit assignment the content of "$variable" is NOT CHANGED. The expansion is just addressing different portions of it:

Code:
variable="start.middle.end"
echo ${variable#*.}
middle.end
echo ${variable}
start.middle.end

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 12  
Old 06-06-2013
Quote:
Originally Posted by rveri
... ... ...
Code:
echo "rp8-file.1.cr_cr.cr" | awk -F. '{$NF="";print $0}'
rp8-file 1 cr_cr

Didn't work because:
  1. Although you set FS to "." (using -F), you didn't set OFS. So the periods in the output were replaced by the default <space> OFS when $0 was recalculated after changing the value of the last field.
  2. If you had set OFS="." as well as setting FS=".", changing the last field from "cr" to "" does not remove the field so the output would have been
    Code:
    rp8-file.1.cr_cr.

    instead of:
    Code:
    rp8-file.1.cr_cr

The following should work:
Code:
echo "rp8-file.1.cr_cr.cr" | awk 'BEGIN{FS=OFS="."}{NF--;$1=$1;print $0}'

but when parameter substitution does the job, why use awk?
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