Btw, Yoda, the awk did not work, it returend the same file, however the parameter expansion one is great.
(OS=hp-ux ksh )
Thanks, for , parameter expansion way it worked great:
I would like to know how this works, I never come across this before the parameter expansion way . Thanks all for your time.
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:
but you can also address arbitrary parts of a variables content by using "modifiers":
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:
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:
You can even nest these expressions and use the outcome of one as the pattern for another:
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:
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.
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
instead of:
The following should work:
but when parameter substitution does the job, why use awk?
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)
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)
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)
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)
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)
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)
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)
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)