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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shortning a text to a length of

Status
Not open for further replies.

Larshg

Programmer
Mar 1, 2001
187
DK
Hi

I want to print a text string to the screen

echo "test of a text1 string is not to long"
test of a text1 string is not to long

But I vant to restrict it to printing the first 15 karakters

echo "test of a text1 string is not to long"
test of a text1

I've tryed using
echo "test of a text1 string is not to long"|awk '{print substr($1,0,15)}'
but this only takes the first word

/Larshg
 
The ksh way:
typeset -L15 x="test of a text1 string is not to long"
echo "$x"
The awk way:
echo "test of a text1 string is not to long"|awk '{print substr($0,1,15)}'

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
and the 'cut' way :

[tt]echo "test of a text1 string is not to long"|cut -c -15[/tt]


Jean Pierre.
 
Thanks.

I've usede the "the awk way" - can you tell me if $0 in awk always is the entire string?

/Larshg
 
In awk, $0 is the entire current line.
Anyway, man awk.

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Hi,

try this command

printf "%.15s" "test of a text1 string is not to long"


man printf for more.

Ali
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top