简体   繁体   English

如何在文件中的固定数量的字符后插入新的行字符

[英]How to insert a new line character after a fixed number of characters in a file

我正在寻找一个bash或sed脚本(最好是一行代码),我可以在巨大的文本文件中使用固定数量的字符后插入一个新行字符。

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

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解决方案比折叠更高效。

Let N be a shell variable representing the count of characters after which you want a newline. 设N是一个shell变量,表示您想要换行符后的字符数。 If you want to continue the count accross lines: 如果你想继续计数:

perl -0xff -pe 's/(.{'$N'})/$1\n/sg' input

If you want to restart the count for each line, omit the -0xff argument. 如果要重新启动每行的计数,请省略-0xff参数。

Because I can't comment directly (to less reputations) a new hint to upper comments: 因为我无法直接评论(以较少的声誉)对上层评论的新提示:

I prefer the sed command (exactly what I want) and also tested the Posix-Command fold . 我更喜欢sed命令(正是我想要的)并且还测试了Posix-Command 折叠 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 sed command (with bytes as number (20 in the answer of @Kristian)) you got n lines if you count with wc . 但是原始问题的两个命令之间有一点差别:如果你有一个包含n *个字节记录的平面文件(没有任何换行符)并使用sed命令(字节为数字(@Kristian的答案为20)如果你用wc计算,你有n行。 If you use the fold command you only got n-1 lines with wc ! 如果你使用fold命令你只有n-1行与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 sed and got no one with fold 这个差异有时很重要,如果您的输入文件不包含任何换行符,则在sed的最后一行之后有一个并且没有折叠的人

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($0,1,ch) "\n" substr($0,ch)
}' file

Append an empty line after a line with exactly 42 characters 在一行恰好包含42个字符后添加一个空行

sed -ie '/^.\{42\}$/a\
' huge_text_file

This might work for you: 这可能对你有用:

echo aaaaaaaaaaaaaaaaaaaax | sed 's/./&\n/20'
aaaaaaaaaaaaaaaaaaaa
x

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在固定数量的字符或空格后插入逗号 - Insert a comma after a fixed number of characters or a space 如何在2个特定字符之间插入新行 - How to insert a new line between 2 specific characters 如何在文件的第一行插入换行符(\\ n)? - How do i insert a new line character(\n) at the first line of a file? 如何在空白行后插入字符 - how to insert a character after a blank line 如何在同一行的2个特定字符之间插入新行? - How to insert a new line between 2 specific characters on the same line? 如何将新行字符转换为制表符,然后在bash循环中插入换行符 - How to convert the new line character to tab and then insert new line character in a loop in bash Bash:如何缩短日志文件的长行,同时从每行的开头和结尾保持固定数量的字符? - Bash: How to shorten long lines of a log file whilst keeping a fixed number of characters from beginning and end of each line? 如何在bash中N个字符后的每一行中将常量字符串插入文本文件 - How to insert a constant string into a text file in every line after N characters in bash Bash脚本-如何在文本文件中匹配模式后在第二行插入变量值 - Bash script - how to insert value of variable as new line, 2 line after matching a pattern in text file 如何打印BASH中搜索到的字符后出现的n个字符? - How to print n number of characters appearing after a searched character in BASH?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM