tr from the command line

Top Page
Attachments:
Message as email
+ (text/plain)
Delete this message
Reply to this message
Author: der.hans
Date:  
Subject: tr from the command line
Am 14. May, 2002 schwätzte Carl Parrish so:

> I've got a lot of files called *_log. I want to change them all to
> *.log. I *think* I want to use the tr command. Can anyone give me the
> syntax? my undertanding from the info and man files isn't working.


The problem with tr is that it will change every occurance, e.g.

$ echo fred_vuz_hier_log | tr _ .
fred.vuz.hier.log

What you need is something that either turns _log into .log or at least
recognizes that the _ is right before log, which is the end of the string.

$ for i in 1 2 3 4 5 6; do touch /tmp/fred/${i}_log; done
$ ls /tmp/fred
1_log 2_log 3_log 4_log 5_log 6_log
$ for i in /tmp/fred/*_log; do mv $i ${i%_log}.log; done
$ ls /tmp/fred
1.log 2.log 3.log 4.log 5.log 6.log

'%' tells bash to knock that string off the end if it exists.

Use '#' to whack something off the beginning.

$ for i in /tmp/fred/6*; do dir=`dirname $i`; log=`basename $i`; mv $i
$dir/six${log#6}; done
$ ls /tmp/fred
1.log 2.log 3.log 4.log 5.log six.log

ciao,

der.hans
--
# https://www.LuftHans.com/
# Motorraeder toeten nicht. Motorraeder werden getoetet.