cut command Behaving Differnetly in different Version

Hi,

We have few hundered scripts using cut command in thousands of lines. On HP-UX shell script developer used

echo "ABCEFG" | cut -c -1-3

to cut first three character of the string. We recently moved to Linux and this command throws error. I think this might be due to different version of cut binary. Is there an easy way to get around with this issue. Changing in all scripts is too risky thing to do.

Best Regards,
:wall:

I think you'll have to change something in all scripts, no matter what.

  1. Change all occurrences to the POSIX conforming cut -c 1-3 , which should then work on all Unices and compatible systems (like Linux)
  2. Define a cut shell function, overriding the program, which checks what OS it's on and calls the real cut with different parameters
  3. Change the name of the cut binary to something else, and replace it with a shell script that calls cut as it should be, with the possibility of breaking some important system script

Personally, I'd go with option a.

A variant of (c) - add a shell script "cut" to some DIR and add this DIR to PATH before running your scripts.
This script may look like this (assuming your args do not have spaces):

#!/bin/sh

for arg; do
  case "$arg" in
    -c) shift;;
    -[0-9]*) nums="$arg"; shift;;
    *) args="$args $arg"; shift;;
  esac
done

nums=`echo $nums | sed 's/^-/-c/'`

/usr/bin/cut $nums $args
1 Like

Many Many thanks it worked.