Reputation: 33
I'm trying to do it with multi lines
words="
What do you think about the kitten outside?
The tattered work gloves speak of the many hours of hard labor he endured throughout his life.
Hello
Hi
"
I used cut -c -17
to truncate it.
I'm trying to output it like this:
What do you think...n outside?
The tattered work... his life.
Hello
Hi
Sorry i'm still noob in bash scripting... thanks
Upvotes: 0
Views: 81
Reputation: 84551
Continuing from the comment, you can read each line separately and figure out the suffix length (if less than 10) as follows
#!/bin/bash
words="
What do you think about the kitten outside?
The tattered work gloves speak of the many hours of hard labor he endured throughout his life.
Hello
Hi
"
while read line; do # loop reading each line
len=${#line} # get line length
[ "$len" -eq 0 ] && continue # if zero len, get next line
suffix=0 # set suffix length 0
[ "$len" -gt 17 ] && suffix=$((len - 17)) # get max suffix length
[ "$suffix" -ge 10 ] && suffix=10 # if > 10, set to 10
printf "%s" "${line:0:17}" # output first 17 chars
if [ "$suffix" -gt 0 ]; then # if suffix > 0, output
printf "...%s\n" "${line: -$suffix}"
else # otherwise output \n
printf "\n"
fi
done <<< $words # feed loop with "herestring" from words
Note: [ ... ] && do_something
is just shorthand for if [ ... ]; then do_something; fi
. You can use the same shorthand with ||
as well. But, avoid chaining [ ... ] && do_something || do_something_else
as the ||
will be executed if the first part fails for any reason. (so it isn't a shortcut for if [...]; then ... else; fi
)
Example Use/Output
$ ./prefixsuffix.sh
What do you think...n outside?
The tattered work... his life.
Hello
Hi
Upvotes: 1