我有一个像日志一样的文件,有几行很长,当打开它时看起来像这样:
DATE DAY ACTIVITY
00/11/22 Fri - aaaaaa bbbbbbbbbbbb
bbbbb
01/11/22 Sat - kjbckjassakkja
我希望它看起来像:
DATE DAY ACTIVITY
00/11/22 Fri - aaaaaa bbbbbbbbbbbb
bbbbb
01/11/22 Sat - kjbckjassakkja
感谢从标题来看,我认为这条线从前一行没有换行,但由于宽度的关系,它看起来像是折叠的终端机的。由于问题被标记为bash
,那么:
#!/bin/bash
limit=39 # fold the line at the 39th character
while IFS= read -r line; do # read each line
if (( ${#line} > limit )); then # the line length exceeds the limit
s=${line%-*} # extract the substring before "-"
len=$(( ${#s} + 2 )) # length to indent
echo "${line:0:limit}" # print the 1st half
printf "%${len}s%sn" " " "${line:limit}"
# print the remaining with indentation
else
echo "$line" # shorter lines are not affected
fi
done < file
输入文件:
DATE DAY ACTIVITY
00/11/22 Fri - aaaaaa bbbbbbbbbbbbbbbbb
01/11/22 Sat - kjbckjassakkja
输出:
DATE DAY ACTIVITY
00/11/22 Fri - aaaaaa bbbbbbbbbbbb
bbbbb
01/11/22 Sat - kjbckjassakkja
Perl救急!
perl -pe '
$l = 2 + index $_, "-" if /-/;
print " " x $l unless 2 == index $_, "/";
' file
-p
逐行读取输入,并在运行代码 后打印每行$_
包含当前行- index返回
-
在当前行的位置,但仅当当前行匹配/-/
时,即它包含任何破折号 $l
将保留缩进长度,它是破折号的位置加上2x
是重复操作符- 如果第三个字符不是
/
,在打印行之前打印$l
空格。