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

unix command: cut

Reply to Message Icon

Original Message
Name: n
Date: July 24, 2003 at 13:59:08 Pacific
Subject: unix command: cut
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.


Report Offensive Message For Removal


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

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


Report Offensive Follow Up For Removal

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

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/'


Report Offensive Follow Up For Removal

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

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


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: unix command:  cut

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge