Cut command on RHEL 6.8 compatibility issues

We have a lot of scripts using cut as :
cut -c 0-8 --works for cut (GNU coreutils) 5.97, but does not work for cut (GNU coreutils) 8.4.
Gives error -

cut: fields and positions are numbered from 1
Try `cut --help' for more information.

The position needs to start with 1 for later version of cut and this is causing an issue.

Is there a way where I can have multiple cut versions installed and use the older version of cut for the user which runs the script?

or any other work around without having to change the scripts?

Thanks.

What are you trying to do when you invoke

cut -c 0-8

with your old version of cut ?

With that old version of cut , is there any difference in the output produced by the two pipelines:

echo 0123456789abcdef | cut -c 0-8

and:

echo 0123456789abcdef | cut -c 1-8

or do they produce the same output?

I am trying to get a value from the 1st line of the file and check if that value is a valid date or not.
------------------------------------------------------------------
Below is the output for the cut command from new version

 $ echo 0123456789abcdef | cut -c 0-8
cut: fields and positions are numbered from 1
Try `cut --help' for more information.
$ echo 0123456789abcdef | cut -c 1-8
01234567

-------------------------------------------------------------------
With old version, both have same results:

$ echo 0123456789abcdef | cut -c 0-8
01234567
$ echo 0123456789abcdef | cut -c 1-8
01234567

The use of 0 is not according to specification. Alternatively, you can just omit it, which should work across versions

$ echo 0123456789abcdef | cut -c -8
01234567

If you cannot adjust the scripts, you could perhaps create a wrapper script for cut, so that the 0 gets stripped..

1 Like

Yes, don't want to adjust my scripts.
Wrapper for cut looks like something that would work.

could you please tell me how would I use it, as in, how would I make sure that the wrapper is called and not the cut command which causes the issue.

The only way to make sure that your wrapper is always called instead of the OS supplied utility is to move the OS supplied utility to a different location and install your wrapper in the location where your OS installed cut originally.

Of course, once you have installed this wrapper, your code might or might not work properly (depending on the quality of your wrapper) and no one else on your system will be able to look at the diagnostics produced by scripts that have bugs in the way they specify field and character ranges so they can identify and fix their code.

My personal opinion is that you should spend time fixing your scripts that call cut -c 0.... , cut -f 0... , and lots of other possible misuses of 0 that are now correctly diagnosed as errors by the new version of cut instead of debugging code to be sure that it changes all of the appropriate 0 characters in its argument list to 1 characters and doesn't change any 0 characters that are correctly specified and do not reference a character 0 or field 0.

2 Likes

An update of "cut" will overwrite your wrapper.
Much better: change your scripts. Run the following fix_cut script on your scripts:

#!/bin/sh
# fix_cut
PATH=/bin:/usr/bin
PRE="\b(cut\s+(-\S*\s+)*-[cf]\s*0*)0-"
for arg
do
  perl -ne 'exit 1 if m/'"$PRE"'/' "$arg" || {
    perl -i -pe 's/'"$PRE"'/${1}1-/g' "$arg"
  }
done 

Example: fix all .sh scripts

fix_cut *.sh
1 Like