Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

newbie question: bash file extension substitution...

4 views
Skip to first unread message

Martin Jørgensen

unread,
Sep 16, 2006, 7:57:49 AM9/16/06
to
Hi,

I want to delete these files:

-rw-r--r-- 1 mac mac 2459 Sep 16 10:54 multicol.aux
-rw-r--r-- 1 mac mac 8941 Sep 16 10:54 multicol.log
-rw-r--r-- 1 mac mac 40354 Sep 16 10:54 multicol.idx
-rw-r--r-- 1 mac mac 14311 Sep 16 10:54 multicol.glo
-rw-r--r-- 1 mac mac 151368 Sep 16 10:54 multicol.dvi

I first tried:

rm multicol.${dvi|log|idx|glo}

Then I tried:

rm multicol.${dvi|log|idx|glo}$

I can't remember it... I think I have seen how to do this?


Best regards
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk

james...@yahoo.com

unread,
Sep 16, 2006, 8:22:27 AM9/16/06
to

Martin Jørgensen wrote:
> Hi,
>
> I want to delete these files:
>
> -rw-r--r-- 1 mac mac 2459 Sep 16 10:54 multicol.aux
> -rw-r--r-- 1 mac mac 8941 Sep 16 10:54 multicol.log
> -rw-r--r-- 1 mac mac 40354 Sep 16 10:54 multicol.idx
> -rw-r--r-- 1 mac mac 14311 Sep 16 10:54 multicol.glo
> -rw-r--r-- 1 mac mac 151368 Sep 16 10:54 multicol.dvi
>
> I first tried:
>
> rm multicol.${dvi|log|idx|glo}
>
> Then I tried:
>
> rm multicol.${dvi|log|idx|glo}$
>
> I can't remember it... I think I have seen how to do this?

rm multicol.{aux,dvi,glo,idx,log}

Martin Jørgensen

unread,
Sep 16, 2006, 9:33:53 AM9/16/06
to
james...@yahoo.com writes:

Ah, it was the comma that did the whole thing, thanks.

I have a new bash file substitution problem...

I have a Latex-directory and I want to replace all occurences of
"old_string" with "new_string" both inside the .tex-files and
"outside". So I guess I must do a:

1) find . -iname *.tex | xargs grep -i old_string | something (looks
*inside* .tex-files for old_string and replaces occurences inside)

2) find . -iname *.* | grep -i old_string | mv {} new_string (or
something, since old_string will also be part of some of the
filenames, like old_string.gif => new_string.gif)

I'm not really into awk and sed, but I'm sure somebody will post
something that uses those functions and then I'll see if I can learn
from that... There are probably many ways of doing it, but I can't find
one...

Martin Jørgensen

unread,
Sep 17, 2006, 8:43:05 AM9/17/06
to
hotmai...@hotmail.com (Martin Jørgensen) writes:

-snip-


> 1) find . -iname *.tex | xargs grep -i old_string | something (looks
> *inside* .tex-files for old_string and replaces occurences inside)
>
> 2) find . -iname *.* | grep -i old_string | mv {} new_string (or
> something, since old_string will also be part of some of the
> filenames, like old_string.gif => new_string.gif)

Is nobody willing to tell me how to replace old_string with new_string
into a directory like shown above? I could do part 2 manually...

Chris F.A. Johnson

unread,
Sep 17, 2006, 8:48:00 AM9/17/06
to
On 2006-09-16, Martin Jørgensen wrote:
>
> I have a new bash file substitution problem...
>
> I have a Latex-directory and I want to replace all occurences of
> "old_string" with "new_string" both inside the .tex-files and
> "outside". So I guess I must do a:
>
> 1) find . -iname *.tex | xargs grep -i old_string | something (looks

find . -iname '*.tex' | xargs grep -li old_string |
while IFS= read -r file
do
sed 's/old_string/new_string/' > tempfile && mv tempfile "$file"
done

> *inside* .tex-files for old_string and replaces occurences inside)

Are there subdirectories? If not, you don't need find:

for file in *.[[Tt][Ee][Xx]]
do
sed 's/old_string/new_string/' > tempfile && mv tempfile "$file"
done


> 2) find . -iname *.* | grep -i old_string | mv {} new_string (or
> something, since old_string will also be part of some of the
> filenames, like old_string.gif => new_string.gif)

for file in *old_string*
do
left=${file%old_string*}
right=${file#*old_string*}
mv -i "$file" "${left}new_string$right"
done

> I'm not really into awk and sed, but I'm sure somebody will post
> something that uses those functions and then I'll see if I can learn
> from that... There are probably many ways of doing it, but I can't find
> one...


--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence

Jon LaBadie

unread,
Sep 17, 2006, 2:11:23 PM9/17/06
to
Chris F.A. Johnson wrote:

> On 2006-09-16, Martin J?rgensen wrote:
>> I have a new bash file substitution problem...
>>
>> I have a Latex-directory and I want to replace all occurences of
>> "old_string" with "new_string" both inside the .tex-files and
>> "outside". So I guess I must do a:
>>
>> 1) find . -iname *.tex | xargs grep -i old_string | something (looks
>
> find . -iname '*.tex' | xargs grep -li old_string |
> while IFS= read -r file
> do
> sed 's/old_string/new_string/' > tempfile && mv tempfile "$file"
> done
>
>> *inside* .tex-files for old_string and replaces occurences inside)
>
> Are there subdirectories? If not, you don't need find:
>
> for file in *.[[Tt][Ee][Xx]]

character lists within an extra set of brackets?

Is that a typo or a pattern matching syntax with which I'm unfamiliar?

Chris F.A. Johnson

unread,
Sep 17, 2006, 2:36:12 PM9/17/06
to
On 2006-09-17, Jon LaBadie wrote:
> Chris F.A. Johnson wrote:
>> On 2006-09-16, Martin J?rgensen wrote:
>>> I have a new bash file substitution problem...
>>>
>>> I have a Latex-directory and I want to replace all occurences of
>>> "old_string" with "new_string" both inside the .tex-files and
>>> "outside". So I guess I must do a:
>>>
>>> 1) find . -iname *.tex | xargs grep -i old_string | something (looks
>>
>> find . -iname '*.tex' | xargs grep -li old_string |
>> while IFS= read -r file
>> do
>> sed 's/old_string/new_string/' > tempfile && mv tempfile "$file"
>> done
>>
>>> *inside* .tex-files for old_string and replaces occurences inside)
>>
>> Are there subdirectories? If not, you don't need find:
>>
>> for file in *.[[Tt][Ee][Xx]]
>
> character lists within an extra set of brackets?
>
> Is that a typo or a pattern matching syntax with which I'm unfamiliar?

That's a typo.

for file in *.[Tt][Ee][Xx]

Martin Jørgensen

unread,
Sep 17, 2006, 7:08:39 PM9/17/06
to
"Chris F.A. Johnson" <cfajo...@gmail.com> writes:

> On 2006-09-16, Martin Jørgensen wrote:
>>
>> I have a new bash file substitution problem...
>>
>> I have a Latex-directory and I want to replace all occurences of
>> "old_string" with "new_string" both inside the .tex-files and
>> "outside". So I guess I must do a:
>>
>> 1) find . -iname *.tex | xargs grep -i old_string | something (looks
>
> find . -iname '*.tex' | xargs grep -li old_string |
> while IFS= read -r file
> do
> sed 's/old_string/new_string/' > tempfile && mv tempfile "$file"
> done
>
>> *inside* .tex-files for old_string and replaces occurences inside)
>
> Are there subdirectories? If not, you don't need find:
>
> for file in *.[[Tt][Ee][Xx]]
> do
> sed 's/old_string/new_string/' > tempfile && mv tempfile "$file"
> done

Thanks. Close enough for me to figure out a solution...

>> 2) find . -iname *.* | grep -i old_string | mv {} new_string (or
>> something, since old_string will also be part of some of the
>> filenames, like old_string.gif => new_string.gif)
>
> for file in *old_string*
> do
> left=${file%old_string*}
> right=${file#*old_string*}
> mv -i "$file" "${left}new_string$right"
> done

The same here... Thanks a lot...

Michal Nazarewicz

unread,
Sep 18, 2006, 7:04:48 AM9/18/06
to
hotmai...@hotmail.com (Martin Jørgensen) writes:

> I have a Latex-directory and I want to replace all occurences of
> "old_string" with "new_string" both inside the .tex-files and
> "outside". So I guess I must do a:
>
> 1) find . -iname *.tex | xargs grep -i old_string | something (looks
> *inside* .tex-files for old_string and replaces occurences inside)

#v+
find . -iname '*.tex' -print0 |\
xargs -0 sed -ie 's/old_string/new_string/g' --
#v-

providing that your find supports -iname and -print0, your xargs
supports -0 and your sed supports -i; or:

#v+
find . -name '*.[Tt][Ee][Xx]' -exec \
sed -ie 's/old_string/new_string/g' -- {} \;
#v-

providing that your sed supports -i; or:

#v+
find . -name '*.[Tt][Ee][Xx]' -exec \
perl -pie 's/old_string/new_string/g' -- {} \;
#v-

providing you have perl; or:

#v+
find . -name '*.[Tt][Ee][Xx]' -exec sh foo.sh {} \;
#v-

where foo.sh has the following content:

#v+
sed 's/old_string/new_string/g' <"$1" >tmp && mv -- tmp "$1"
#v-

> 2) find . -iname *.* | grep -i old_string | mv {} new_string (or
> something, since old_string will also be part of some of the
> filenames, like old_string.gif => new_string.gif)

#v+
find . -name '*old_string*' -print0 |\
xargs -0 rename old_string new_string
#v-

providing that your find supports -print0, your xargs supports -0 and
you have rename; or:

#v+
find . -name '*old_string*' -exec rename old_string new_string {} \;
#v-

providing that you have rename.

If you don't have rename you can change the command 'rename' with
'bash bar.sh' providing you have bash and bar.sh has the following
content:

#v+
FROM="$1"
TO="F2"
shift 2
for FILE; do
F="${FILE/$FROM/$TO}"
[ X"$F" = X"$FILE" ] || mv -- "$FILE" "$F"
done
#v-

or with 'sh bar.sh' providing bar.sh has following content:

#v+
FROM="$1"
TO="F2"
shift 2
for FILE; do
PRE="${FILE%%$FROM*}"
if [ X"$PRE" = X"$FILE" ]; then continue; fi
POST="${FILE#*$FROM}"
mv -- "$FILE" "$PRE$TO$POST"
done
#v-

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>--<jid:mina86*jabber.org>--ooO--(_)--Ooo--

Glenn Jackman

unread,
Sep 18, 2006, 10:44:16 AM9/18/06
to
At 2006-09-16 09:33AM, "Martin Jørgensen" wrote:
> I have a Latex-directory and I want to replace all occurences of
> "old_string" with "new_string" both inside the .tex-files and
> "outside". So I guess I must do a:
>
> 1) find . -iname *.tex | xargs grep -i old_string | something (looks
> *inside* .tex-files for old_string and replaces occurences inside)

perl -i -pe 's/old_string/new_string/g' *.tex



> 2) find . -iname *.* | grep -i old_string | mv {} new_string (or
> something, since old_string will also be part of some of the
> filenames, like old_string.gif => new_string.gif)

for f in *${old_string}*; do
prefix="${f%%${old_string}*}"
suffix="${f##*$old_string}"
mv "$f" "${prefix}${new_string}$suffix"
done


--
Glenn Jackman
Ulterior Designer

Martin Jørgensen

unread,
Sep 18, 2006, 7:46:10 PM9/18/06
to
Glenn Jackman <gle...@ncf.ca> writes:

Hi, thanks for the extra educative contributions, all of you. I'll study
them later and see what I can learn from them and keep subscribing
here...

0 new messages