Limit the number of characters in bash output

Hi, I need some help with this:
I'm making a script which does a couple of things with image files. The script is supposed to echo the number of each image it is processing like this:

Processing image1.jpg

The problem is with images with very long filenames, so I want to know how to limit the numer of characters of the output (maybe with sed or awk, but I prefer sed if it's possible).
I want to be able to turn this:

"Processing a-very-very-very-long-crazy-filename.jpg"

into this:

Processing a-very-very-ve...

It's important to say that this limitation will be done over a variable and not a file.
I came across this solution which seems to work but it is applied on a file and not on a variable, and I need it to be applied on a variable

I'm doing this as part of a progress bar solution. I want it to be like pacman progress bar when downloading packages to be updated except that I want it to inform each processed file and the operation percentage in one line. I already accomplished this, the only things remaining are the one I'm asking for, and the animation of the progress bar to make it look like the pacman one.

Thanks in advance. Shadow_Reaper

---------- Post updated at 11:57 PM ---------- Previous update was at 11:53 PM ----------

The image I posted was intended to show the url of the solution. I will post it through this reply so that it can be seen

[THIS WEBSITE]

/shell-programming

-and-scripting/241327-li

mit-string-length-sed [.HTML HERE]

The actual solution is the first reply but I need to be able to do this on a variable rather than a file.

Maybe a start using this:

jaysunn-> cat sub.sh 
#!/bin/bash
STRING1='a-very-very-very-long-crazy-filename.jpg'

for i in $STRING1
    do
        echo "PROCESSING:  ${i:0:14}..."
done

Produces:

jaysunn-> bash sub.sh 
PROCESSING:  a-very-very-ve...

Jaysunn

1 Like

Using ksh (or bash) you could do this:

#!/bin/ksh
STRING1='a-very-very-very-long-crazy-filename.jpg'

if [ ${#STRING1} -gt 17 ]
then
   TRIM=${STRING1#??????????????}
   SHORT=${STRING1%$TRIM}...
else
   SHORT=$STRING1
fi
echo "PROCESSING:  $SHORT"
1 Like

Or more simply, with any POSIX conforming shell (including bash and ksh ):

#!/bin/ksh
for VAR in "a-very-very-very-long-crazy-filename.jpg" "short.jpg" "medium-length.txt"
do	if [ ${#VAR} -gt 17 ]
	then	printf 'Processing %.14s...\n' "$VAR"
	else	printf 'Processing %s\n' "$VAR"
	fi
done

which produces the output:

Processing a-very-very-ve...
Processing short.jpg
Processing medium-length.txt
3 Likes

Thats nice, i've had a similiar challenge a little back, but i wanted to maintain the file extensions + extra space for the possible incremented file number as well.

So that was my solution this, just copy pasted from source code:

	string_line=$[ ${#tmp_if} + ${#tmp_of} + $tmp_border ]

	if [[ $string_line -gt $(tput cols) ]]
	then	tmp_if="${tmp_if:0:${#tmp_if}/4}...${tmp_if:(-6)}"
		tmp_of="${tmp_of:0:${#tmp_of}/4}...${tmp_of:(-6)}"
	fi

Hope this helps

1 Like

Thank you very much!! It's the easiest solution from those posted here, anyway thanks to everyone for answering and posting your solutions.