Multiple strings, Truncate line at 80 characters

前端 未结 5 838
无人共我
无人共我 2021-02-02 05:57

I\'m new to awk and sed, and I\'m looking for a way to truncate a line at 80 characters, but I\'m printing several strings in that line using printf. The last two strings are th

5条回答
  •  闹比i
    闹比i (楼主)
    2021-02-02 05:59

    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|jack@overthehill.com|"Happy birthday" 14545|Jan|15|1970|jill@thewell.com|"Hope your head is ok" 27676|Feb|14|1970|jack@overthehill.com|"Still on for tonight?" 29898|Feb|14|1970|jill@thewell.com|"Sure, if you bring the chocolate." 34234|Feb|15|1970|jack@overthehill.com|"Had a great time last night, hope you did too. Can't wait for the weekend, love Jack"

    Returns

    12123,Jan14,1970,jack@overthehill.com,"Happy birthday"
    14545,Jan15,1970,jill@thewell.com,"Hope your head is ok"
    27676,Feb14,1970,jack@overthehill.com,"Still on for tonight?"
    29898,Feb14,1970,jill@thewell.com,"Sure, if you bring the chocolate."
    34234,Feb15,1970,jack@overthehill.com,"Had a great time last night, hope
    

提交回复
热议问题