Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple strings, Truncate line at 80 characters

Tags:

string

bash

awk

I'm looking for a way to truncate a line at 80 characters ...

You could pipe the output to cut:

printf ... | cut -c 1-80

If you wanted to ensure that each line isn't more than 80 characters (or wrap lines to fit in specified width), you could use fold:

printf ... | fold -w 80

Another way to solve this just using Bash (syntax: ${var:0:80}), e.g.:

printf "%5d  %3s%.2s %4s %s %s \n" "$f" "$month" "$day" "$year" "$from" "${subject::80}"

This truncates the string before it gets to printf. This method would also allow you to specify different maximum widths for each printed column individually.


I had the same issue trying to customize my bash prompt with a truncated directory name. What finaly worked was:

PS1='\u@\h:`echo $(basename $PWD) | cut -c 1-15`\$ '

You could use substr to only grab the 1st n characters of from and subject, since you know you only have a max of 60 characters to play with you could grab the 1st 25 of 'from' and the 1st 35 of 'subject'.

#!/usr/bin/gawk -f
BEGIN { 
 # set ouput delimiter to comma
 OFS=","
 #  set input delimiter to bar
 FS="|"  }

{
f=$1
month=$2
day=$3
year=$4
from=$5
subject=$6
from=substr(from,1,25) 
subject=substr(subject,1,35)
printf ("%5d,%3s%.2s,%4s,%s,%s\n",f,month,day,year,from,subject)
}

Running the above on this file

12123|Jan|14|1970|[email protected]|"Happy birthday" 14545|Jan|15|1970|[email protected]|"Hope your head is ok" 27676|Feb|14|1970|[email protected]|"Still on for tonight?" 29898|Feb|14|1970|[email protected]|"Sure, if you bring the chocolate." 34234|Feb|15|1970|[email protected]|"Had a great time last night, hope you did too. Can't wait for the weekend, love Jack"

Returns

12123,Jan14,1970,[email protected],"Happy birthday"
14545,Jan15,1970,[email protected],"Hope your head is ok"
27676,Feb14,1970,[email protected],"Still on for tonight?"
29898,Feb14,1970,[email protected],"Sure, if you bring the chocolate."
34234,Feb15,1970,[email protected],"Had a great time last night, hope

How about a C version?

#include <stdio.h>
int maxline = 80;
int main(int argc, char* argv[])
{
    char line[2048];
    if ((argc>1) && (atoi(argv[1]) > 0)) {
        maxline = atoi(argv[1]);
    }
    while (fgets(line, sizeof(line), stdin)) {
        line[maxline] = '\0';
        printf("%s\n", line);
    }
}