-1

I'm trying to use the "tr" command, and to use it's -d delete option. My purpose is to rearrange a list of files in order of date accessed.

I have a file orderedFile with the following contents:

Access: 2014-02-09 21:35:36.642598731 -0700 0) accessedfiles.cpp
Access: 2014-02-10 14:24:01.527811265 -0700 1) accessedfiles.sh
Access: 2014-02-10 14:24:03.563811320 -0700 2) onlyFilesListFile
Access: 2014-02-10 14:24:03.563811320 -0700 3) orderedFile
Access: 2014-02-10 14:23:37.623810616 -0700 4) properlyOrdered
Access: 2014-02-10 14:23:37.543810614 -0700 5) smallestToLargest
Access: 2014-02-09 21:35:36.642598731 -0700 6) strace.txt
Access: 2014-02-09 21:35:36.638598730 -0700 7) sum.cpp
Access: 2014-02-10 13:24:54.415715076 -0700 8) test
Access: 2014-02-09 21:35:36.638598730 -0700 9) testFile

I then use the command tr -d 'Access: ' to cut that Access part off of each line (this is done with a bash script on every line) and I get the following output in a new file properlyOrdered:

2014-02-09213536.642598731-07000)adfil.pp
2014-02-10142401.527811265-07001)adfil.h
2014-02-10142403.563811320-07002)onlyFilLitFil
2014-02-10142403.563811320-07003)ordrdFil
2014-02-10142337.623810616-07004)proprlyOrdrd
2014-02-10142337.543810614-07005)malltToLargt
2014-02-09213536.642598731-07006)tra.txt
2014-02-09213536.638598730-07007)um.pp
2014-02-10132454.415715076-07008)tt
2014-02-09213536.638598730-07009)ttFil

So I now have the date right up front and east to use grep on, but the file names have gone all funky and compressed. Any idea why? They will be harder to match to their full names now.

s.t
  • 13
  • 1
  • 7
  • Tip: [shellcheck](http://www.shellcheck.net) automatically points out several common problems in scripts and commands, including this one. – that other guy Feb 10 '14 at 22:37

6 Answers6

3

This is wrong command actually:

tr -d 'Access: ' 

since it will remove every occurrence of letters a,c,e,s:

You need sed:

sed 's/^ *Access: //'
anubhava
  • 761,203
  • 64
  • 569
  • 643
2

Use sed:

sed -e 's/^Access: //' orderedFile

If you just want the filenames:

sed -e 's/^.*) //' orderedFile

From the manpage for tr, you can see:

 -d, --delete
              delete characters in SET1, do not translate

So what you were doing was deleting the characters A, c, e, s, :, and (blank space), which is why you got the garbled file names.

Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
1

The command tr -d 'Access:' doesn't just remove the complete word 'Access:', it removes every instance of every letter appearing in the string 'Access:'. See man tr (http://linux.die.net/man/1/tr).

A good tool to remove complete words is sed; you could try the following:

sed 's/^Access: //' orderedFile > properlyOrdered
andypea
  • 1,343
  • 11
  • 22
1

awk alternative :

awk -F'^Access: ' '$NF=$NF' OFS='' file
Kent
  • 189,393
  • 32
  • 233
  • 301
0

tr is not suitable for this, how about cut?

cut -c 9- < file
BMW
  • 42,880
  • 12
  • 99
  • 116
0

Given that the format that you have chosen to represent the date has the quality of alphabetic ordering being the same as date/time ordering - you can sort the file without needing to chop up lines with

sort -k2,3 <filename>

(sort using fields 2 and 3).

Beano
  • 7,551
  • 3
  • 24
  • 27