I am writing a shell script to do some conversion:
#!/bin/bash
ls -1 *.cwk | while read FILE_OLD
do
FILE_NEW=$FILE_OLD".txt"
cat $FILE_OLD | [... do some recoding ...] > $FILE_NEW
done
This converts myFile.cwk into myFile.cwk.txt. How could I calculate FILE_NEW without the extension in order to get myFile.txt?
Michael Hufschmidt <Michael_Hufschm...@omnis.net> wrote:
> Hello @ all,
> I am writing a shell script to do some conversion:
> #!/bin/bash
> ls -1 *.cwk | while read FILE_OLD
> do
> FILE_NEW=$FILE_OLD".txt"
> cat $FILE_OLD | [... do some recoding ...] > $FILE_NEW
> done
> This converts myFile.cwk into myFile.cwk.txt. How could I calculate > FILE_NEW without the extension in order to get myFile.txt?
With the ${var%pattern} (strip trailing pattern) parameter expansion:
FILE_NEW=${FILE_OLD%.cwk}.txt
Consider using a for loop instead of piping ls' output to a while loop:
Michael Hufschmidt <Michael_Hufschm...@omnis.net> writes:
> Hello @ all,
> I am writing a shell script to do some conversion:
> #!/bin/bash
> ls -1 *.cwk | while read FILE_OLD
> do
> FILE_NEW=$FILE_OLD".txt"
> cat $FILE_OLD | [... do some recoding ...] > $FILE_NEW
> done
> This converts myFile.cwk into myFile.cwk.txt. How could I calculate
> FILE_NEW without the extension in order to get myFile.txt?
${FILE_OLD%.cwk}.txt
In addition, I'd
(a) quote all variables so as to be safe against file names with spaces,
(b) use file globing rather than ls | while read, and
(c) remove the use of cat:
for f in *.cwk; do recode-program <"$f" >"${f%.cwk}.txt"; done
If the "recode-program" is actually a pipeline, it's nice to make use of
some little-used syntax:
for f in *.cwk; do <"$f" head | tr A-Z a-z >"${f%.cwk}.txt"; done
> Michael Hufschmidt<Michael_Hufschm...@omnis.net> wrote:
>> Hello @ all,
>> I am writing a shell script to do some conversion:
>> #!/bin/bash
>> ls -1 *.cwk | while read FILE_OLD
>> do
>> FILE_NEW=$FILE_OLD".txt"
>> cat $FILE_OLD | [... do some recoding ...]> $FILE_NEW
>> done
>> This converts myFile.cwk into myFile.cwk.txt. How could I calculate
>> FILE_NEW without the extension in order to get myFile.txt?
> With the ${var%pattern} (strip trailing pattern) parameter expansion:
> FILE_NEW=${FILE_OLD%.cwk}.txt
> Consider using a for loop instead of piping ls' output to a while loop:
> for FILE_OLD in *.cwk
> do
> ...
> done
Hello Dave,
thank you, this works perfectly. I do not use a loop because some of the filenames may contain spaces.
> Am 08.02.2012 12:11 schrieb Dave Gibson:
>> Michael Hufschmidt<Michael_Hufschm...@omnis.net> wrote:
>>> Hello @ all,
>>> I am writing a shell script to do some conversion:
>>> #!/bin/bash
>>> ls -1 *.cwk | while read FILE_OLD
>>> do
>>> FILE_NEW=$FILE_OLD".txt"
>>> cat $FILE_OLD | [... do some recoding ...]> $FILE_NEW
>>> done
>>> This converts myFile.cwk into myFile.cwk.txt. How could I calculate
>>> FILE_NEW without the extension in order to get myFile.txt?
>> With the ${var%pattern} (strip trailing pattern) parameter expansion:
>> FILE_NEW=${FILE_OLD%.cwk}.txt
>> Consider using a for loop instead of piping ls' output to a while
>> loop:
>> for FILE_OLD in *.cwk
>> do
>> ...
>> done
> Hello Dave,
> thank you, this works perfectly. I do not use a loop because some of
> the filenames may contain spaces.
That shouldn't be a problem if you quote the variable.
-- = Aragorn =
(registered GNU/Linux user #223157)
Michael Hufschmidt <Michael_Hufschm...@omnis.net> writes:
> Am 08.02.2012 12:11 schrieb Dave Gibson:
>> Michael Hufschmidt<Michael_Hufschm...@omnis.net> wrote:
>>> Hello @ all,
>>> I am writing a shell script to do some conversion:
>>> #!/bin/bash
>>> ls -1 *.cwk | while read FILE_OLD
>>> do
>>> FILE_NEW=$FILE_OLD".txt"
>>> cat $FILE_OLD | [... do some recoding ...]> $FILE_NEW
>>> done
>>> This converts myFile.cwk into myFile.cwk.txt. How could I calculate
>>> FILE_NEW without the extension in order to get myFile.txt?
>> With the ${var%pattern} (strip trailing pattern) parameter expansion:
>> FILE_NEW=${FILE_OLD%.cwk}.txt
>> Consider using a for loop instead of piping ls' output to a while loop:
>> for FILE_OLD in *.cwk
>> do
>> ...
>> done
> Hello Dave,
> thank you, this works perfectly. I do not use a loop because some of
> the filenames may contain spaces.
Your code does not work if you have spaces in a file name and the use of
a 'for' loop is not cause any problems. Using a 'for' loop with file
globing works just as well as 'ls | while read' when there are spaces,
but your use of unquoted variable expansions ($FILE_OLD rather than
"$FILE_OLD" for example) causes problems no matter how you get the names
to start with.
Michael Hufschmidt <Michael_Hufschm...@omnis.net> wrote:
> Am 08.02.2012 12:11 schrieb Dave Gibson:
>> Consider using a for loop instead of piping ls' output to a while loop:
>> for FILE_OLD in *.cwk
> thank you, this works perfectly. I do not use a loop because some of the
> filenames may contain spaces.
Globbing (wildcard expansion) is performed after word splitting so the
'for' command will receive each of the names in the generated list as
a distinct item.
Try:
for f in *.cwk ; do printf ' -->%s<--\n' "$f" ; done
In article <9pf234Fph...@mid.individual.net>,
Michael Hufschmidt <Michael_Hufschm...@omnis.net> wrote:
>Hello @ all,
>I am writing a shell script to do some conversion:
> #!/bin/bash
> ls -1 *.cwk | while read FILE_OLD
> do
> FILE_NEW=$FILE_OLD".txt"
> cat $FILE_OLD | [... do some recoding ...] > $FILE_NEW
> done
>This converts myFile.cwk into myFile.cwk.txt. How could I calculate >FILE_NEW without the extension in order to get myFile.txt?
>Thanks in advance for any hints - Michael
The "classical" method uses basename(1).
Modern shells have string-manipulation constructs that let you do it without
the need to call an external utility.
> In article <9pf234Fph...@mid.individual.net>,
> Michael Hufschmidt <Michael_Hufschm...@omnis.net> wrote:
>>Hello @ all,
>>I am writing a shell script to do some conversion:
>> #!/bin/bash
>> ls -1 *.cwk | while read FILE_OLD
>> do
>> FILE_NEW=$FILE_OLD".txt"
>> cat $FILE_OLD | [... do some recoding ...] > $FILE_NEW
>> done
>>This converts myFile.cwk into myFile.cwk.txt. How could I calculate >>FILE_NEW without the extension in order to get myFile.txt?
>>Thanks in advance for any hints - Michael
> The "classical" method uses basename(1).
> Modern shells have string-manipulation constructs that let you do it without
> the need to call an external utility.
That is part of the standard Unix shell.
-- Chris F.A. Johnson, author <http://shell.cfajohnson.com/>
===================================================================
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
Robert Bonomi wrote:
> Michael Hufschmidt <Michael_Hufschm...@omnis.net> wrote:
>> I am writing a shell script to do some conversion:
>> #!/bin/bash
>> ls -1 *.cwk | while read FILE_OLD
>> do
>> FILE_NEW=$FILE_OLD".txt"
>> cat $FILE_OLD | [... do some recoding ...] > $FILE_NEW
>> done
>> This converts myFile.cwk into myFile.cwk.txt. How could I calculate
>> FILE_NEW without the extension in order to get myFile.txt?
> The "classical" method uses basename(1).
basename(1) does not remove filename suffixes ("extensions"). It removes the leading directory components of a file path.
-- PointedEars
Please do not Cc: me. / Bitte keine Kopien per E-Mail.
Janis Papanagnou wrote:
> Am 10.02.2012 08:33, schrieb Ralph Spitzner:
>> Robert Bonomi wrote:
>> [...]
>>> The "classical" method uses basename(1).
>> [...]
>> Am I missing somethinge here ?
> Yes; reading the man page:
> "[...] any leading directory components removed. [...]"
Which is not what the OP is looking for.
-- PointedEars
Please do not Cc: me. / Bitte keine Kopien per E-Mail.
>Robert Bonomi wrote:
>> Michael Hufschmidt <Michael_Hufschm...@omnis.net> wrote:
>>> I am writing a shell script to do some conversion:
>>> #!/bin/bash
>>> ls -1 *.cwk | while read FILE_OLD
>>> do
>>> FILE_NEW=$FILE_OLD".txt"
>>> cat $FILE_OLD | [... do some recoding ...] > $FILE_NEW
>>> done
>>> This converts myFile.cwk into myFile.cwk.txt. How could I calculate
>>> FILE_NEW without the extension in order to get myFile.txt?
>> The "classical" method uses basename(1).
>basename(1) does not remove filename suffixes ("extensions"). It removes >the leading directory components of a file path.
Stephane Chazelas wrote:
> Thomas 'PointedEars' Lahn:
>> basename(1) does not remove filename suffixes ("extensions"). It removes
>> the leading directory components of a file path.
> [...]
Thanks, that also works in GNU. However, this does not help with unknown
or multiple "extensions", and requires another tool, so I think I will usually stick to the equally POSIX-compliant shell parameter expansions ${x%.*} and ${x%%.*} (which also work in GNU bash).
> > Thomas 'PointedEars' Lahn:
> >> basename(1) does not remove filename suffixes ("extensions"). It removes
> >> the leading directory components of a file path.
> > [...]
> Thanks, that also works in GNU. However, this does not help with unknown
> or multiple "extensions", and requires another tool, so I think I will > usually stick to the equally POSIX-compliant shell parameter expansions > ${x%.*} and ${x%%.*} (which also work in GNU bash).
[...]
careful with that though:
$ x=/etc/init.d/foo
$ echo ${x%.*}
/etc/init
zsh/csh:
$ echo $x:r
/etc/init.d/foo
You may also need to consider /path/to/foo.x/ and foo-2.1.tar.gz
Stephane Chazelas wrote:
> 2012-02-10 13:22:01 +0100, Thomas 'PointedEars' Lahn:
>> Stephane Chazelas wrote:
>> > Thomas 'PointedEars' Lahn:
>> >> basename(1) does not remove filename suffixes ("extensions"). It
>> >> removes the leading directory components of a file path.
>> > [...]
>> Thanks, that also works in GNU. However, this does not help with unknown
>> or multiple "extensions", and requires another tool, so I think I will
>> usually stick to the equally POSIX-compliant shell parameter expansions
>> ${x%.*} and ${x%%.*} (which also work in GNU bash).
> [...]
Thomas 'PointedEars' Lahn <PointedE...@web.de> writes:
> Janis Papanagnou wrote:
>> Am 10.02.2012 08:33, schrieb Ralph Spitzner:
>>> Robert Bonomi wrote:
>>> [...]
>>>> The "classical" method uses basename(1).
>>> [...]
>>> Am I missing somethinge here ?
>> Yes; reading the man page:
>> "[...] any leading directory components removed. [...]"
> Which is not what the OP is looking for.
They want to strip the suffix. Of course, basename can do that too, but
if the OP were ever to generalise the script to work with paths other
than ., the full behaviour of basename would get in the way.
Ben Bacarisse <ben.use...@bsb.me.uk> writes:
> Thomas 'PointedEars' Lahn <PointedE...@web.de> writes:
>> Janis Papanagnou wrote:
>>> Am 10.02.2012 08:33, schrieb Ralph Spitzner:
>>>> Robert Bonomi wrote:
>>>> [...]
>>>>> The "classical" method uses basename(1).
>>>> [...]
>>>> Am I missing somethinge here ?
>>> Yes; reading the man page:
>>> "[...] any leading directory components removed. [...]"
>> Which is not what the OP is looking for.
> They want to strip the suffix...
<snip irrelevant clarification>
Oh dear. I somehow missed the word "not". Please ignore.
i2012-02-10 13:52:21 +0100, Thomas 'PointedEars' Lahn:
[...]
> > zsh/csh:
> > $ echo $x:r
> > /etc/init.d/foo
> So zsh and csh are not POSIX-compliant?
[...]
No, and they never claimed to be. zsh has a "sh" emulation mode
(also enabled when called as "sh") than aims at improving POSIX
compliance, where you need ${x:r} instead (but of course you
wouldn't if you were writing a POSIX script).
> > Michael Hufschmidt <Michael_Hufschm...@omnis.net> wrote:
> >> I am writing a shell script to do some conversion:
> >> #!/bin/bash
> >> ls -1 *.cwk | while read FILE_OLD
> >> do
> >> FILE_NEW=$FILE_OLD".txt"
> >> cat $FILE_OLD | [... do some recoding ...] > $FILE_NEW
> >> done
> >> This converts myFile.cwk into myFile.cwk.txt. How could I calculate
> >> FILE_NEW without the extension in order to get myFile.txt?
> > The "classical" method uses basename(1).
> basename(1) does not remove filename suffixes ("extensions"). It removes > the leading directory components of a file path.
It optionally removes a suffix, but you have to supply the suffix explicitly as the second argument.
-- Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Barry Margolin wrote:
> Thomas 'PointedEars' Lahn <PointedE...@web.de> wrote:
>> basename(1) does not remove filename suffixes ("extensions"). It removes
>> the leading directory components of a file path.
> It optionally removes a suffix, but you have to supply the suffix
> explicitly as the second argument.
11-hours newsfeed?
-- PointedEars
Please do not Cc: me. / Bitte keine Kopien per E-Mail.