LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 06-14-2006, 04:12 PM   #1
mebaro
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.

example:

filenames are:

test-1
test-2
test-3
test-4

My script uses the following sybtax:

#!/bin/bash
Y1=test-*

for Z in $Y1
do

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

echo $Z
echo $X1

done

exit

The output I get is:

test-1
1
test-2
2
test-3

test-4
4


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, 04:48 PM   #2
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,950
Blog Entries: 5

Rep: Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755
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, 05:16 PM   #3
zhangmaike
Member
 
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:
Code:
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

Code:
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:

Code:
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:
Code:
#!/bin/bash

prefix="removeme-"
cut_location=$(( ${#prefix}+1 ))

for f in "$prefix"*
do
    echo "$f" | cut -c$cut_location-
done
 
Old 06-15-2006, 10:04 AM   #4
mebaro
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.

...max
 
  


Reply

Tags
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 01:36 PM
info about coreutils fastmach Linux - Newbie 2 01-02-2006 01:02 PM
coreutils version. bruse Linux - Newbie 2 08-27-2005 04:59 AM
coreutils-5.2.1-i486-1.tgz ??? elfie Slackware - Installation 3 05-05-2005 02:10 AM
coreutils-5.0 error rahul_jain_9684 Linux From Scratch 0 09-27-2003 12:51 AM


All times are GMT -5. The time now is 05:54 AM.

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