string 多个字符串,截断 80 个字符的行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19804806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple strings, Truncate line at 80 characters
提问by TZPike05
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 the ones that give me problems because they vary in size on each iteration of my code. Here's my current code:
我是 awk 和 sed 的新手,我正在寻找一种将一行截断为 80 个字符的方法,但我正在使用 printf 在该行中打印几个字符串。最后两个字符串给我带来了问题,因为它们在我的代码每次迭代中的大小都不同。这是我当前的代码:
printf "%5d %3s%.2s %4s %s %s \n" "$f" "$month" "$day" "$year" "$from" "$subject"
This code is being used to create a summary of email messages that get passed through a Bash script. What I do know, is that with the spaces and requirements of my other strings, I have room for 60 characters between the $from and $subject strings.
此代码用于创建通过 Bash 脚本传递的电子邮件摘要。我所知道的是,由于我的其他字符串的空间和要求,我在 $from 和 $subject 字符串之间有 60 个字符的空间。
Any help is appreciated.
任何帮助表示赞赏。
回答by devnull
I'm looking for a way to truncate a line at 80 characters ...
我正在寻找一种在 80 个字符处截断一行的方法......
You could pipe the output to cut
:
您可以将输出通过管道传输到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
:
如果您想确保每行不超过 80 个字符(或换行以适应指定的宽度),您可以使用fold
:
printf ... | fold -w 80
回答by Aaron R.
Another way to solve this just using Bash (syntax: ${var:0:80}
), e.g.:
使用 Bash 解决这个问题的另一种方法(语法:)${var:0:80}
,例如:
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.
这会在字符串到达 之前截断它printf
。此方法还允许您分别为每个打印列指定不同的最大宽度。
回答by user5925630
I had the same issue trying to customize my bash prompt with a truncated directory name. What finaly worked was:
我在尝试使用截断的目录名称自定义 bash 提示时遇到了同样的问题。最终奏效的是:
PS1='\u@\h:`echo $(basename $PWD) | cut -c 1-15`$ '
回答by Peter Kelly
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'.
您可以使用 substr 仅抓取 from 和 subject 的第 1 个字符,因为您知道最多只能玩 60 个字符,因此您可以抓取 'from' 的第 25 个字符和 'subject' 的第 1 个 35 个字符。
#!/usr/bin/gawk -f
BEGIN {
# set ouput delimiter to comma
OFS=","
# set input delimiter to bar
FS="|" }
{
f=
month=
day=
year=
from=
subject=
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 Hyman"
12123|Jan|14|1970|[email protected]|“生日快乐” 14545|Jan|15|1970|[email protected]|“希望你的头没事” 27676|Feb|14|1970|Hyman@overthehill .com|“今晚还继续吗?” 29898|Feb|14|1970|[email protected]|“当然,如果你带巧克力来。” 34234|Feb|15|1970|[email protected]|“昨晚玩得很开心,希望你也是。等不及周末了,爱Hyman”
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
回答by ChuckCottrill
How about a C version?
C版呢?
#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] = '##代码##';
printf("%s\n", line);
}
}