Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cut last characters 2

Status
Not open for further replies.

TSch

Technical User
Jul 12, 2001
557
DE
Hi folks,

I understand that e.g. using "cut -c1-x" you can cut a certain amount of characters from the beginning of a certain line ...

Now the question is: Can this be done backwards ?

Let's say I got the following 3 rows:

Descr=hggh12H4
Descr=sdfgsik1V34
Descr=skljjjjklh123F

What I need are the last 4 characters:

First row: 12H4
Second row: 1V34
Third row: 123F

How can this be done ?

Regards
Thomas
 
I don't know if cut can handle this, but awk can:

[tt]awk '{print substr($0,length($0)-3,4)}'[/tt]

HTH,

p5wizard
 
Hi

As far as I know, no [tt]cut[/tt] implementation can count the position from the right end. So the closest would be to use it with [tt]rev[/tt] ( probably unavailable on Unix systems ) :
Code:
rev /input/file | cut -c1-4 | rev
But I would prefer [tt]sed[/tt] :
Code:
sed 's/.*\(....\)$/\1/' /input/file


Feherke.
 
Hi folks,

both methods (awk and rev) work perfect here !

Thanks a lot for the help !

Regards
Thomas
 
This should do the trick as well

Descr=hggh12H4
typeset -lR4 last4="$Descr"
echo $last4

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top