Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Cut command on RHEL 6.8 compatibility issues

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 06-29-2016
Vikram Jain Vikram Jain is offline
Registered User
 
Join Date: Jun 2016
Last Activity: 23 March 2017, 2:57 PM EDT
Posts: 3
Thanks: 3
Thanked 0 Times in 0 Posts
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 -

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

Last edited by RudiC; 06-30-2016 at 04:53 AM.. Reason: Added code tags.
Sponsored Links
    #2  
Old Unix and Linux 06-29-2016
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 22 October 2017, 2:06 AM EDT
Location: San Jose, CA, USA
Posts: 10,554
Thanks: 554
Thanked 3,697 Times in 3,154 Posts
What are you trying to do when you invoke

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

Code:
echo 0123456789abcdef | cut -c 0-8

and:

Code:
echo 0123456789abcdef | cut -c 1-8

or do they produce the same output?
Sponsored Links
    #3  
Old Unix and Linux 06-30-2016
Vikram Jain Vikram Jain is offline
Registered User
 
Join Date: Jun 2016
Last Activity: 23 March 2017, 2:57 PM EDT
Posts: 3
Thanks: 3
Thanked 0 Times in 0 Posts
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

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

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


Moderator's Comments:
Cut command on RHEL 6.8 compatibility issues
Please wrap all code, files, input & output/errors in CODE tags
It makes them far easier to read and preserves spaces for indenting or fixed-width data.

Last edited by rbatte1; 06-30-2016 at 11:38 AM.. Reason: Code tags
    #4  
Old Unix and Linux 06-30-2016
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 22 October 2017, 10:11 PM EDT
Location: Amsterdam
Posts: 11,575
Thanks: 510
Thanked 3,356 Times in 2,960 Posts
The use of 0 is not according to specification. Alternatively, you can just omit it, which should work across versions

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

Last edited by Scrutinizer; 07-02-2016 at 02:28 AM..
The Following User Says Thank You to Scrutinizer For This Useful Post:
Vikram Jain (06-30-2016)
Sponsored Links
    #5  
Old Unix and Linux 06-30-2016
Vikram Jain Vikram Jain is offline
Registered User
 
Join Date: Jun 2016
Last Activity: 23 March 2017, 2:57 PM EDT
Posts: 3
Thanks: 3
Thanked 0 Times in 0 Posts
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.
Sponsored Links
    #6  
Old Unix and Linux 06-30-2016
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 22 October 2017, 2:06 AM EDT
Location: San Jose, CA, USA
Posts: 10,554
Thanks: 554
Thanked 3,697 Times in 3,154 Posts
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.
The Following 2 Users Say Thank You to Don Cragun For This Useful Post:
vgersh99 (06-30-2016), Vikram Jain (06-30-2016)
Sponsored Links
    #7  
Old Unix and Linux 06-30-2016
MadeInGermany MadeInGermany is offline Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 22 October 2017, 9:44 PM EDT
Location: Simplicity
Posts: 3,782
Thanks: 311
Thanked 1,267 Times in 1,146 Posts
An update of "cut" will overwrite your wrapper.
Much better: change your scripts. Run the following fix_cut script on your scripts:

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

Code:
fix_cut *.sh

The Following User Says Thank You to MadeInGermany For This Useful Post:
Vikram Jain (07-08-2016)
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Compatibility issues between Sun solaries & AIX commands. 46019 Shell Programming and Scripting 1 05-23-2013 07:40 AM
sed & cut command issues robrom78 Homework & Coursework Questions 1 09-29-2012 10:24 AM
Does RHEL 5 provide a command to collect RHEL system log in single compress file? nnnnnnine Red Hat 4 11-04-2011 04:08 AM
having issues in using Cut Command paoie UNIX for Dummies Questions & Answers 2 10-27-2010 08:19 AM



All times are GMT -4. The time now is 10:29 PM.