Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 06-14-2006, 05:12 PM   #1
LQ Newbie
Registered: Jul 2005
Location: Miami
Distribution: RHES3, Fedora4
Posts: 16

Rep: Reputation: 0
possible coreutils bug (tr)

I have a really strange occurance. I'm trying to strip the leading characters in a list of filenames from a bash sctipt.


filenames are:


My script uses the following sybtax:


for Z in $Y1

X1=$( echo $Z | tr --delete 'test-' )

echo $Z
echo $X1



The output I get is:



I have created test names upto 35 and in every case the 3 gets stripped from the entire string when using 'tr'.

Has anyone else encountered this? Is there a fix?

I'm running RedHat ES3 and ES4 and get the same behavior on both machines. I have coreutils 4.5.3-28 on one system and 4.5.3-25 on the other system. RH has version 4.5.3-28 as the latest version.

I also have an AIX box and I wrote a script with compatible syntax and it does not strip the 3 in AIX.

If anyone can suggest an alternate way of triming the filename I would greatly appreciate it.

Old 06-14-2006, 05:48 PM   #2
LQ Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,067
Blog Entries: 14

Rep: Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248
Interesting. I created the 4 files you list then ran your script and did NOT have the issue you saw. This was on my RH AS 3 server. Its coreutils rpm is: coreutils-4.5.3-26 You may want to check the version of coreutils you have.

The fact you have a "-" made me think it may be misinterpreting the -3 as a flag but on testing I see nothing that uses this in tr or test (test by the way is a built in command).

Anyway a couple of ways you can do it.

With awk: ls test-* |awk -F- '{print $2}'
Tells it to use the dash as delimiter then prints everything after the dash. Good for variable length patterns. (e.g. would work for test*-* instead of just test-*)

With cut: ls test-* |cut -c6-
Tells it to print everything from the 6th position on. Good for fixed length names such as the one you have. test- = 5 positions so the 6th position would follow the pattern you want to exclude.

Note: You imply you're recreating these files. If you're not it may simply be you have a hidden character in the test-3 file name. Doing ls test-* finds the file so long as the hidden character is after the dash. Try typing "ls test-3" - if it doesn't find the file then you know it has a hidden character in its name. If it does find it then you know it doesn't.
Old 06-14-2006, 06:16 PM   #3
Registered: Oct 2004
Distribution: Slackware
Posts: 376

Rep: Reputation: 31
My tr is version 5.2.1, and does not exhibit this problem.

I tried:
echo "test-3" | tr --delete 'test-'
and the result was 3.

A bit of warning: tr will delete all characters within the character set 'test-', not simply all occurences of the string 'test-'. So the result of

echo "t-t-e-e-s-s-t-t-3-testtest" | tr --delete 'test-'
is also 3. (And, because the argument to tr is a character set rather than a string, the second t is redundant.)

Anyway, the fact that each of these filenames has already been matched with test-* by the for loop, means that whatever program you use to remove the leading 6 characters does not need to retest for the presence of the prefix (its presence is already guaranteed by the pattern matching). Piping to:

cut -c6-
should be simple enough. Just don't forget the last dash after the 6.

If the prefix might change in the future, you could always have bash calculate the new cut location by checking the length of the prefix and adding 1:

cut_location=$(( ${#prefix}+1 ))

for f in "$prefix"*
    echo "$f" | cut -c$cut_location-
Old 06-15-2006, 11:04 AM   #4
LQ Newbie
Registered: Jul 2005
Location: Miami
Distribution: RHES3, Fedora4
Posts: 16

Original Poster
Rep: Reputation: 0
Thank you all for the suggestions. I have coreutils-4.5.3-26 also, so I don't know why the 3 is getting stripped. I created test files all the way to -35 and every output with a 3 in it had the 3 stripped out.

I tried using awk with the fiels option, but I got the syntax wrong, thanks for correcting me on that. I also didn't find the cut command in my linux scripting book.

Anyway, thanks again for the suggestions. I'll try both options.



coreutils, script, tr

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Coreutils-5.2.1 error LFS 5.1 michaelbhahn Linux From Scratch 8 09-01-2007 02:36 PM
info about coreutils fastmach Linux - Newbie 2 01-02-2006 02:02 PM
coreutils version. bruse Linux - Newbie 2 08-27-2005 05:59 AM
coreutils-5.2.1-i486-1.tgz ??? elfie Slackware - Installation 3 05-05-2005 03:10 AM
coreutils-5.0 error rahul_jain_9684 Linux From Scratch 0 09-27-2003 01:51 AM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:12 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration