bash 如何在文件中固定数量的字符后插入换行符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1187078/
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
How to insert a new line character after a fixed number of characters in a file
提问by rangalo
I am looking for a bash or sed script (preferably a one-liner) with which I can insert a new line character after a fixed number of characters in huge text file.
我正在寻找一个 bash 或 sed 脚本(最好是单行),我可以用它在大文本文件中的固定数量的字符后插入一个换行符。
回答by Kristian
How about something like this? Change 20 is the number of characters before the newline, and temp.text is the file to replace in..
这样的事情怎么样?更改 20 是换行符之前的字符数,temp.text 是要替换的文件。
sed -e "s/.\{20\}/&\n/g" < temp.txt
回答by Steven Penny
Here is POSIX solution:
这是POSIX解决方案:
awk '{gsub(/.{5}/,"&\n")}1' file
Or:
或者:
fold -w5 file
Input:
输入:
banana strawberry grape
Output:
输出:
banan
a str
awber
ry gr
ape
Interestingly, the Awk solution is more performant than fold.
有趣的是,Awk 解决方案比 fold 更高效。
回答by William Pursell
Let N be a shell variable representing the count of characters after which you want a newline. If you want to continue the count accross lines:
让 N 是一个 shell 变量,表示您想要换行符后的字符数。如果要继续跨行计数:
perl -0xff -pe 's/(.{'$N'})/\n/sg' input
If you want to restart the count for each line, omit the -0xff argument.
如果要重新开始每行的计数,请省略 -0xff 参数。
回答by gmu
Because I can't comment directly (to less reputations) a new hint to upper comments:
因为我无法直接评论(减少声誉)对上层评论的新提示:
I prefer the sedcommand (exactly what I want) and also tested the Posix-Command fold. But there is a little difference between both commands for the original problem: If you have a flat file with n*bytes records (without any linefeed characters) and use the sedcommand (with bytes as number (20 in the answer of @Kristian)) you got n lines if you count with wc. If you use the foldcommand you only got n-1 lines with wc! This difference is sometimes important to know, if your input file doesn't contain any newline character, you got one after the last line with sedand got no one with fold
我更喜欢sed命令(正是我想要的)并且还测试了 Posix-Command fold。但是对于原始问题,这两个命令之间有一点区别:如果您有一个包含 n*bytes 记录(没有任何换行符)的平面文件并使用sed命令(以字节为数字(@Kristian 的回答中为 20) ) 如果你用wc计算,你会得到 n 行。如果你使用fold命令,你只有 n-1 行wc!这种差异有时很重要,如果您的输入文件不包含任何换行符,则在最后一行之后使用sed获得一个,而没有使用fold
回答by potong
This might work for you:
这可能对你有用:
echo aaaaaaaaaaaaaaaaaaaax | sed 's/./&\n/20'
aaaaaaaaaaaaaaaaaaaa
x
回答by ghostdog74
if you mean you want to insert your newline after a number of characters with respect to the whole file, eg after the 30th character in the whole file
如果您的意思是要在相对于整个文件的多个字符之后插入换行符,例如在整个文件中的第 30 个字符之后
gawk 'BEGIN{ FS=""; ch=30}
{
for(i=1;i<=NF;i++){
c+=1
if (c==ch){
print ""
c=0
}else{
printf $i
}
}
print ""
}' file
if you mean insert at specific number of characters in each line eg after every 5th character
如果您的意思是在每行中插入特定数量的字符,例如每 5 个字符之后
gawk 'BEGIN{ FS=""; ch=5}
{
print substr(sed -ie '/^.\{42\}$/a\
' huge_text_file
,1,ch) "\n" substr(##代码##,ch)
}' file
回答by Chen Levy
Append an empty line after a line with exactly 42 characters
在正好为 42 个字符的行后追加一个空行
##代码##