1832235 Members
2551 Online
110041 Solutions
New Discussion

How do I truncate words?

 

How do I truncate words?

Hi, happy new year to every body!!
I'm programming in shell. But I have a small problem.
I want a command or a program to truncate words, for example:

AS-CHIMBOTE ... I want to get .... CHIMBOTE
AS-RIMAC ...... I want to get .... RIMAC
AS-PIURA ...... I want to get .... PIURA
AS-ICA ........ I want to get .... ICA
AS-SANISIDRO-2 I want to get .... SANISIDRO-2
AS-VILLA-MONT-2 I want to get .... VILLA-MONT-2

I want to truncate the 3 first characters of every word.
There are words that have more than one dash(-).

Please, I need a command to truncate words.

Thank you very much!!
Christian Aguilar
6 REPLIES 6
A. Clay Stephenson
Acclaimed Contributor

Re: How do I truncate words?

echo "AS-RIMAC" | cut -c 4-
If it ain't broke, I can fix that.
Sridhar Bhaskarla
Honored Contributor

Re: How do I truncate words?

See if this works

awk '{print substr($0,4)}' your_file

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Robert Thorneycroft
Valued Contributor

Re: How do I truncate words?

You could also use sed

sed 's/...//' datafile

Regards,

Robert Thorneycroft
Rodney Hills
Honored Contributor

Re: How do I truncate words?

Within the korn shell try-

x="AS-SANISIDRO-2"
y=${x#*-}
echo $y

will display-
SANISIDRO-2

HTH

-- Rod Hills
There be dragons...
Wilfred Chau_1
Respected Contributor

Re: How do I truncate words?

sed s/^AS\-//
Robert Thorneycroft
Valued Contributor

Re: How do I truncate words?

Wilfred,
Just a minor point, whilst your command will be correct for the examples given it will not truncate the first three characters of every word as specified in the question IF any other examples do not start with AS-.

In this case your command will fail so the solution I mentioned above will be better as it will truncate the first three characters of every word, no matter what they are.

Of course if every line does start AS- your command will work without any problems however.

Regards,
Robert Thorneycroft