Computing.Net > Forums > Unix > unix command: cut

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

unix command: cut

Reply to Message Icon

Name: n
Date: July 24, 2003 at 13:59:08 Pacific
OS: os
CPU/Ram: na
Comment:

Hi,

If I use command:

cut -c1-9, this will extract the first 9 characters.

What command should I use if I want to extract the last 9 characters.

Thanks.



Sponsored Link
Ads by Google

Response Number 1
Name: Dlonra
Date: July 24, 2003 at 14:40:53 Pacific
Reply:

sed 's/^.*\(.........\)$/\1/'


0

Response Number 2
Name: David Perry
Date: July 25, 2003 at 04:57:05 Pacific
Reply:

Some version of cut do not require a second value in the character range.

cut -c 10-

You could also use awk

echo 123456789abcdefghi | awk '{ print substr($0,10,9) }'

You can also use the range notation with sed.

echo 123456789abcdefghi | sed -e 's/^.\{9\}\(.*\)/\1/'


0

Response Number 3
Name: James Boothe
Date: July 25, 2003 at 08:00:27 Pacific
Reply:

You can use the ksh built-ins to strip leading or trailing patterns from a variable. To retain the last 9:

var2=${var1%${var1#?????????}}

You can't get 9 out of less than 9, so the above returns null when var1 is less than 9.  If you want the last 9 or less, then:

if [ ${#var1} -gt 9 ] ; then
   var2=${var1%${var1#?????????}}
else
   var2=$var1
fi


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: unix command: cut

where is the code of unix commands www.computing.net/answers/unix/where-is-the-code-of-unix-commands/7463.html

run unix command thr java program www.computing.net/answers/unix/run-unix-command-thr-java-program-/5887.html

UNIX Command www.computing.net/answers/unix/unix-command/1793.html